|
| 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