Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d212c2e
Add `RecursiveDataStructureTraverser` helper to manipulate nested data
petitphp Jun 26, 2023
7292638
add pluck command to cache
petitphp Jun 26, 2023
8e1b63d
add pluck command to transient
petitphp Jun 26, 2023
6e286af
add patch command to cache
petitphp Jul 1, 2023
bd13459
add patch command to transient
petitphp Aug 23, 2023
05424ec
Add missing `expiration` option for patch cache command
petitphp Aug 23, 2023
ca02d28
Fix cs
petitphp Aug 28, 2023
15ad011
Update Behat features tests
petitphp Aug 31, 2023
ba1f610
Add temporary log to debug failing tests in github action
petitphp Aug 31, 2023
6b8d91c
Small CS fixes for WPCS v3
petitphp Sep 7, 2023
a70e2d7
Fix failing functional tests
petitphp Sep 15, 2023
5d0e6df
Add tests for RecursiveDataStructureTraverser.php
petitphp Sep 18, 2023
dd9eec3
Fix wrong command in docblock
petitphp Sep 19, 2023
d174717
Fix CS issue
petitphp Sep 19, 2023
ae6d0a6
Reuse has_stdin() from framework
schlessera Nov 14, 2023
b74bc47
Fix PHPCS issues
schlessera Nov 14, 2023
d28445f
Use `wp eval` to set the transient
petitphp Dec 4, 2023
b1afe2e
split `patch` feature tests and add additionnal checks with `transien…
petitphp Dec 8, 2023
8d9ba42
split `patch` feature tests for site transient and add additionnal ch…
petitphp Dec 11, 2023
7d3a483
Add test cases for invalid keys
petitphp Dec 11, 2023
d061301
Refactor `pluck` feature tests to use `wp eval` to set the transient
petitphp Dec 11, 2023
cf568fb
new feature test for calling cache pluck command with invalid key
petitphp Dec 12, 2023
3fa7fe1
add the new commands to the `composer.json`
petitphp Apr 26, 2024
119c991
use string interpolation for building message
petitphp Apr 29, 2024
2d17185
explicitly call update methods for clearer code
petitphp Apr 29, 2024
9eca4d9
fix tests
petitphp Apr 29, 2024
de71744
Merge branch 'main' into feature/pluck-patch-cache-transient
schlessera May 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add tests for RecursiveDataStructureTraverser.php
  • Loading branch information
petitphp authored and schlessera committed Aug 4, 2024
commit 5d0e6df3853aeb6d670a727328e9e7164d3e58ce
21 changes: 21 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="vendor/autoload.php"
backupGlobals="false"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true">
<testsuite>
<directory suffix="Test.php">tests</directory>
</testsuite>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
6 changes: 3 additions & 3 deletions src/Cache/RecursiveDataStructureTraverser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class RecursiveDataStructureTraverser {
/**
* RecursiveDataStructureTraverser constructor.
*
* @param mixed $data The data to read/manipulate by reference.
* @param string|int $key The key/property the data belongs to.
* @param static $parent_instance
* @param mixed $data The data to read/manipulate by reference.
* @param string|int $key The key/property the data belongs to.
* @param static|null $parent_instance The parent instance of the traverser.
*/
public function __construct( &$data, $key = null, $parent_instance = null ) {
$this->data =& $data;
Expand Down
162 changes: 162 additions & 0 deletions tests/RecursiveDataStructureTraverserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
<?php

namespace WP_CLI\Cache\Tests;

use WP_CLI\Cache\RecursiveDataStructureTraverser;
use WP_CLI\Tests\TestCase;

class RecursiveDataStructureTraverserTest extends TestCase {

/** @test */
public function it_can_get_a_top_level_array_value() {
$array = array(
'foo' => 'bar',
);

$traverser = new RecursiveDataStructureTraverser( $array );

$this->assertEquals( 'bar', $traverser->get( 'foo' ) );
}

/** @test */
public function it_can_get_a_top_level_object_value() {
$object = (object) array(
'foo' => 'bar',
);

$traverser = new RecursiveDataStructureTraverser( $object );

$this->assertEquals( 'bar', $traverser->get( 'foo' ) );
}

/** @test */
public function it_can_get_a_nested_array_value() {
$array = array(
'foo' => array(
'bar' => array(
'baz' => 'value',
),
),
);

$traverser = new RecursiveDataStructureTraverser( $array );

$this->assertEquals( 'value', $traverser->get( array( 'foo', 'bar', 'baz' ) ) );
}

/** @test */
public function it_can_get_a_nested_object_value() {
$object = (object) array(
'foo' => (object) array(
'bar' => 'baz',
),
);

$traverser = new RecursiveDataStructureTraverser( $object );

$this->assertEquals( 'baz', $traverser->get( array( 'foo', 'bar' ) ) );
}

/** @test */
public function it_can_set_a_nested_array_value() {
$array = array(
'foo' => array(
'bar' => 'baz',
),
);
$this->assertEquals( 'baz', $array['foo']['bar'] );

$traverser = new RecursiveDataStructureTraverser( $array );
$traverser->update( array( 'foo', 'bar' ), 'new' );

$this->assertEquals( 'new', $array['foo']['bar'] );
}

/** @test */
public function it_can_set_a_nested_object_value() {
$object = (object) array(
'foo' => (object) array(
'bar' => 'baz',
),
);
$this->assertEquals( 'baz', $object->foo->bar );

$traverser = new RecursiveDataStructureTraverser( $object );
$traverser->update( array( 'foo', 'bar' ), 'new' );

$this->assertEquals( 'new', $object->foo->bar );
}

/** @test */
public function it_can_update_an_integer_object_value() {
$object = (object) array(
'test_mode' => 0,
);
$this->assertEquals( 0, $object->test_mode );

$traverser = new RecursiveDataStructureTraverser( $object );
$traverser->update( array( 'test_mode' ), 1 );

$this->assertEquals( 1, $object->test_mode );
}

/** @test */
public function it_can_delete_a_nested_array_value() {
$array = array(
'foo' => array(
'bar' => 'baz',
),
);
$this->assertArrayHasKey( 'bar', $array['foo'] );

$traverser = new RecursiveDataStructureTraverser( $array );
$traverser->delete( array( 'foo', 'bar' ) );

$this->assertArrayNotHasKey( 'bar', $array['foo'] );
}

/** @test */
public function it_can_delete_a_nested_object_value() {
$object = (object) array(
'foo' => (object) array(
'bar' => 'baz',
),
);
$this->assertObjectHasAttribute( 'bar', $object->foo );

$traverser = new RecursiveDataStructureTraverser( $object );
$traverser->delete( array( 'foo', 'bar' ) );

$this->assertObjectNotHasAttribute( 'bar', $object->foo );
}

/** @test */
public function it_can_insert_a_key_into_a_nested_array() {
$array = array(
'foo' => array(
'bar' => 'baz',
),
);

$traverser = new RecursiveDataStructureTraverser( $array );
$traverser->insert( array( 'foo', 'new' ), 'new value' );

$this->assertArrayHasKey( 'new', $array['foo'] );
$this->assertEquals( 'new value', $array['foo']['new'] );
}

/** @test */
public function it_throws_an_exception_when_attempting_to_create_a_key_on_an_invalid_type() {
$data = 'a string';
$traverser = new RecursiveDataStructureTraverser( $data );

try {
$traverser->insert( array( 'key' ), 'value' );
} catch ( \Exception $e ) {
$this->assertSame( 'a string', $data );
return;
}

$this->fail( 'Failed to assert that an exception was thrown when inserting a key into a string.' );
}
}