Skip to content

Commit 5d0e6df

Browse files
petitphpschlessera
authored andcommitted
Add tests for RecursiveDataStructureTraverser.php
1 parent a70e2d7 commit 5d0e6df

File tree

3 files changed

+186
-3
lines changed

3 files changed

+186
-3
lines changed

phpunit.xml.dist

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
beStrictAboutCoversAnnotation="true"
7+
beStrictAboutOutputDuringTests="true"
8+
beStrictAboutTestsThatDoNotTestAnything="true"
9+
beStrictAboutTodoAnnotatedTests="true"
10+
colors="true"
11+
verbose="true">
12+
<testsuite>
13+
<directory suffix="Test.php">tests</directory>
14+
</testsuite>
15+
16+
<filter>
17+
<whitelist processUncoveredFilesFromWhitelist="true">
18+
<directory suffix=".php">src</directory>
19+
</whitelist>
20+
</filter>
21+
</phpunit>

src/Cache/RecursiveDataStructureTraverser.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class RecursiveDataStructureTraverser {
2424
/**
2525
* RecursiveDataStructureTraverser constructor.
2626
*
27-
* @param mixed $data The data to read/manipulate by reference.
28-
* @param string|int $key The key/property the data belongs to.
29-
* @param static $parent_instance
27+
* @param mixed $data The data to read/manipulate by reference.
28+
* @param string|int $key The key/property the data belongs to.
29+
* @param static|null $parent_instance The parent instance of the traverser.
3030
*/
3131
public function __construct( &$data, $key = null, $parent_instance = null ) {
3232
$this->data =& $data;
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
namespace WP_CLI\Cache\Tests;
4+
5+
use WP_CLI\Cache\RecursiveDataStructureTraverser;
6+
use WP_CLI\Tests\TestCase;
7+
8+
class RecursiveDataStructureTraverserTest extends TestCase {
9+
10+
/** @test */
11+
public function it_can_get_a_top_level_array_value() {
12+
$array = array(
13+
'foo' => 'bar',
14+
);
15+
16+
$traverser = new RecursiveDataStructureTraverser( $array );
17+
18+
$this->assertEquals( 'bar', $traverser->get( 'foo' ) );
19+
}
20+
21+
/** @test */
22+
public function it_can_get_a_top_level_object_value() {
23+
$object = (object) array(
24+
'foo' => 'bar',
25+
);
26+
27+
$traverser = new RecursiveDataStructureTraverser( $object );
28+
29+
$this->assertEquals( 'bar', $traverser->get( 'foo' ) );
30+
}
31+
32+
/** @test */
33+
public function it_can_get_a_nested_array_value() {
34+
$array = array(
35+
'foo' => array(
36+
'bar' => array(
37+
'baz' => 'value',
38+
),
39+
),
40+
);
41+
42+
$traverser = new RecursiveDataStructureTraverser( $array );
43+
44+
$this->assertEquals( 'value', $traverser->get( array( 'foo', 'bar', 'baz' ) ) );
45+
}
46+
47+
/** @test */
48+
public function it_can_get_a_nested_object_value() {
49+
$object = (object) array(
50+
'foo' => (object) array(
51+
'bar' => 'baz',
52+
),
53+
);
54+
55+
$traverser = new RecursiveDataStructureTraverser( $object );
56+
57+
$this->assertEquals( 'baz', $traverser->get( array( 'foo', 'bar' ) ) );
58+
}
59+
60+
/** @test */
61+
public function it_can_set_a_nested_array_value() {
62+
$array = array(
63+
'foo' => array(
64+
'bar' => 'baz',
65+
),
66+
);
67+
$this->assertEquals( 'baz', $array['foo']['bar'] );
68+
69+
$traverser = new RecursiveDataStructureTraverser( $array );
70+
$traverser->update( array( 'foo', 'bar' ), 'new' );
71+
72+
$this->assertEquals( 'new', $array['foo']['bar'] );
73+
}
74+
75+
/** @test */
76+
public function it_can_set_a_nested_object_value() {
77+
$object = (object) array(
78+
'foo' => (object) array(
79+
'bar' => 'baz',
80+
),
81+
);
82+
$this->assertEquals( 'baz', $object->foo->bar );
83+
84+
$traverser = new RecursiveDataStructureTraverser( $object );
85+
$traverser->update( array( 'foo', 'bar' ), 'new' );
86+
87+
$this->assertEquals( 'new', $object->foo->bar );
88+
}
89+
90+
/** @test */
91+
public function it_can_update_an_integer_object_value() {
92+
$object = (object) array(
93+
'test_mode' => 0,
94+
);
95+
$this->assertEquals( 0, $object->test_mode );
96+
97+
$traverser = new RecursiveDataStructureTraverser( $object );
98+
$traverser->update( array( 'test_mode' ), 1 );
99+
100+
$this->assertEquals( 1, $object->test_mode );
101+
}
102+
103+
/** @test */
104+
public function it_can_delete_a_nested_array_value() {
105+
$array = array(
106+
'foo' => array(
107+
'bar' => 'baz',
108+
),
109+
);
110+
$this->assertArrayHasKey( 'bar', $array['foo'] );
111+
112+
$traverser = new RecursiveDataStructureTraverser( $array );
113+
$traverser->delete( array( 'foo', 'bar' ) );
114+
115+
$this->assertArrayNotHasKey( 'bar', $array['foo'] );
116+
}
117+
118+
/** @test */
119+
public function it_can_delete_a_nested_object_value() {
120+
$object = (object) array(
121+
'foo' => (object) array(
122+
'bar' => 'baz',
123+
),
124+
);
125+
$this->assertObjectHasAttribute( 'bar', $object->foo );
126+
127+
$traverser = new RecursiveDataStructureTraverser( $object );
128+
$traverser->delete( array( 'foo', 'bar' ) );
129+
130+
$this->assertObjectNotHasAttribute( 'bar', $object->foo );
131+
}
132+
133+
/** @test */
134+
public function it_can_insert_a_key_into_a_nested_array() {
135+
$array = array(
136+
'foo' => array(
137+
'bar' => 'baz',
138+
),
139+
);
140+
141+
$traverser = new RecursiveDataStructureTraverser( $array );
142+
$traverser->insert( array( 'foo', 'new' ), 'new value' );
143+
144+
$this->assertArrayHasKey( 'new', $array['foo'] );
145+
$this->assertEquals( 'new value', $array['foo']['new'] );
146+
}
147+
148+
/** @test */
149+
public function it_throws_an_exception_when_attempting_to_create_a_key_on_an_invalid_type() {
150+
$data = 'a string';
151+
$traverser = new RecursiveDataStructureTraverser( $data );
152+
153+
try {
154+
$traverser->insert( array( 'key' ), 'value' );
155+
} catch ( \Exception $e ) {
156+
$this->assertSame( 'a string', $data );
157+
return;
158+
}
159+
160+
$this->fail( 'Failed to assert that an exception was thrown when inserting a key into a string.' );
161+
}
162+
}

0 commit comments

Comments
 (0)