|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Remorhaz\JSON\Pointer\Test\Pointer; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use Remorhaz\JSON\Data\Value\EncodedJson\NodeValueFactory; |
| 7 | +use Remorhaz\JSON\Pointer\Processor\Processor; |
| 8 | +use Remorhaz\JSON\Pointer\Query\QueryFactory; |
| 9 | + |
| 10 | +/** |
| 11 | + * @coversNothing |
| 12 | + */ |
| 13 | +class AcceptanceTest extends TestCase |
| 14 | +{ |
| 15 | + |
| 16 | + /** |
| 17 | + * @param string $text |
| 18 | + * @param mixed $data |
| 19 | + * @param mixed $result |
| 20 | + * @dataProvider providerSelectExistingData |
| 21 | + */ |
| 22 | + public function testSelect_ExistingData_SelectsMatchingResult(string $text, $data, $result) |
| 23 | + { |
| 24 | + $query = QueryFactory::create()->createQuery($text); |
| 25 | + $document = NodeValueFactory::create()->createValue($data); |
| 26 | + $readData = Processor::create() |
| 27 | + ->select($query, $document) |
| 28 | + ->encode(); |
| 29 | + |
| 30 | + $this->assertSame($result, $readData); |
| 31 | + } |
| 32 | + |
| 33 | + public function providerSelectExistingData(): array |
| 34 | + { |
| 35 | + return [ |
| 36 | + 'rootProperty' => ['/a', '{"a":1,"b":2}', '1'], |
| 37 | + 'rootNumericProperty' => ['/1', '{"1":2,"b":3}', '2'], |
| 38 | + 'rootNegativeNumericProperty' => ['/-1', '{"-1":2,"b":3}', '2'], |
| 39 | + 'nestedProperty' => ['/a/b', '{"a":{"b":1},"c":2}', '1'], |
| 40 | + 'rootIndex' => ['/1', '[1,2]', '2'], |
| 41 | + 'nestedIndex' => ['/0/1', '[[1,2],3]', '2'], |
| 42 | + 'rootScalar' => ['', '"abc"', '"abc"'], |
| 43 | + 'rootNull' => ['', 'null', 'null'], |
| 44 | + 'nestedNull' => ['/a', '{"a":null}', 'null'], |
| 45 | + ]; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @param string $text |
| 50 | + * @param mixed $data |
| 51 | + * @dataProvider providerSelectNonExistingData |
| 52 | + */ |
| 53 | + public function testSelect_NonExistingData_ResultNotExists(string $text, $data) |
| 54 | + { |
| 55 | + $query = QueryFactory::create()->createQuery($text); |
| 56 | + $document = NodeValueFactory::create()->createValue($data); |
| 57 | + $actualValue = Processor::create() |
| 58 | + ->select($query, $document) |
| 59 | + ->exists(); |
| 60 | + |
| 61 | + self::assertFalse($actualValue); |
| 62 | + } |
| 63 | + |
| 64 | + public function providerSelectNonExistingData(): array |
| 65 | + { |
| 66 | + return [ |
| 67 | + 'nonExistingRootProperty' => ['/a', '{"b":1}'], |
| 68 | + 'nonExistingNestedProperty' => ['/a/b', '{"a":{"c":1}}'], |
| 69 | + 'nonExistingRootNumericIndex' => ['/1', '[1]'], |
| 70 | + 'nonExistingNestedNumericIndex' => ['/0/1', '[[1],2]'], |
| 71 | + 'rootArrayProperty' => ['/a', '[]'], |
| 72 | + 'nestedArrayProperty' => ['/a/b', '{"a":[]}'], |
| 73 | + 'rootScalarKey' => ['/a', '1'], |
| 74 | + 'nestedScalarKey' => ['/a/b', '{"a":1}'], |
| 75 | + 'nonExistingRootNonNumericIndex' => ['/a', '[1]'], |
| 76 | + 'nonExistingNestedNonNumericIndex' => ['/0/a', '[[1],2]'], |
| 77 | + 'rootNextIndex' => ['/-', '[1,2]'], |
| 78 | + 'nestedNextIndex' => ['/a/-', '{"a":[1,2]}'], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @param string $data |
| 84 | + * @param string $query |
| 85 | + * @param string $value |
| 86 | + * @param string $expectedData |
| 87 | + * @dataProvider providerAddableQuery |
| 88 | + */ |
| 89 | + public function testAdd_AddableQuery_DataAdded(string $data, string $query, string $value, string $expectedData) |
| 90 | + { |
| 91 | + $query = QueryFactory::create()->createQuery($query); |
| 92 | + $nodeValueFactory = NodeValueFactory::create(); |
| 93 | + $document = $nodeValueFactory->createValue($data); |
| 94 | + $replacement = $nodeValueFactory->createValue($value); |
| 95 | + $actualData = Processor::create() |
| 96 | + ->add($query, $document, $replacement) |
| 97 | + ->encode(); |
| 98 | + |
| 99 | + $this->assertEquals($expectedData, $actualData); |
| 100 | + } |
| 101 | + |
| 102 | + public function providerAddableQuery(): array |
| 103 | + { |
| 104 | + return [ |
| 105 | + 'Property exists' => ['{"a":"b"}', '/a', '"c"', '{"a":"c"}'], |
| 106 | + 'Property not exists' => ['{"a":"b"}', '/c', '"d"', '{"a":"b","c":"d"}'], |
| 107 | + 'Element exists' => ['[1,3]', '/1', '2', '[1,2,3]'], |
| 108 | + 'Next element not exists' => ['[1,2]', '/2', '3', '[1,2,3]'], |
| 109 | + 'New element' => ['[1,2]', '/-', '3', '[1,2,3]'], |
| 110 | + |
| 111 | + ]; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @param string $data |
| 116 | + * @param string $text |
| 117 | + * @param string $value |
| 118 | + * @dataProvider providerAddNonExistingSelection |
| 119 | + */ |
| 120 | + public function testAdd_NonAddableQuery_ResultNotExists(string $data, string $text, string $value) |
| 121 | + { |
| 122 | + $query = QueryFactory::create()->createQuery($text); |
| 123 | + $nodeValueFactory = NodeValueFactory::create(); |
| 124 | + $document = $nodeValueFactory->createValue($data); |
| 125 | + $replacement = $nodeValueFactory->createValue($value); |
| 126 | + $result = Processor::create()->add($query, $document, $replacement); |
| 127 | + |
| 128 | + self::assertFalse($result->exists()); |
| 129 | + } |
| 130 | + |
| 131 | + public function providerAddNonExistingSelection(): array |
| 132 | + { |
| 133 | + return [ |
| 134 | + 'Not next element not exists' => ['[1,2]', "/3", '4'], |
| 135 | + 'Invalid element index' => ['[1,2]', "/a", '3'], |
| 136 | + 'Parent index not exists' => ['[1,2]', "/2/0", '4'], |
| 137 | + 'Parent new index' => ['[1,2]', "/-/0", '4'], |
| 138 | + 'Parent property not exists' => ['{"a":"b"}', "/c/d", '"e"'], |
| 139 | + 'Scalar parent' => ['"a"', "/a", '"b"'], |
| 140 | + ]; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * @param string $text |
| 145 | + * @param $data |
| 146 | + * @param $expectedData |
| 147 | + * @dataProvider providerRemoveExistingData |
| 148 | + */ |
| 149 | + public function testRemove_ExistingData_Removed(string $text, string $data, string $expectedData) |
| 150 | + { |
| 151 | + $query = QueryFactory::create()->createQuery($text); |
| 152 | + $document = NodeValueFactory::create()->createValue($data); |
| 153 | + $actualValue = Processor::create() |
| 154 | + ->delete($query, $document) |
| 155 | + ->encode(); |
| 156 | + |
| 157 | + $this->assertSame($expectedData, $actualValue); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @return array |
| 162 | + * @todo Shorten dataset list. |
| 163 | + */ |
| 164 | + public function providerRemoveExistingData(): array |
| 165 | + { |
| 166 | + return [ |
| 167 | + 'rootProperty' => [ |
| 168 | + '/a', |
| 169 | + '{"a":1,"b":2}', |
| 170 | + '{"b":2}', |
| 171 | + ], |
| 172 | + 'rootNumericProperty' => [ |
| 173 | + '/1', |
| 174 | + '{"1":2,"b":3}', |
| 175 | + '{"b":3}', |
| 176 | + ], |
| 177 | + 'rootNegativeNumericProperty' => [ |
| 178 | + '/-1', |
| 179 | + '{"-1":2,"b":3}', |
| 180 | + '{"b":3}', |
| 181 | + ], |
| 182 | + 'nestedProperty' => [ |
| 183 | + '/a/b', |
| 184 | + '{"a":{"b":1},"c":2}', |
| 185 | + '{"a":{},"c":2}', |
| 186 | + ], |
| 187 | + 'rootIndex' => ['/1', '[1,2]', '[1]'], |
| 188 | + 'nestedIndex' => ['/0/1', '[[1,2],3]', '[[1],3]'], |
| 189 | + 'nestedNull' => [ |
| 190 | + '/a', |
| 191 | + '{"a":null}', |
| 192 | + '{}', |
| 193 | + ], |
| 194 | + ]; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * @param string $text |
| 199 | + * @param string $data |
| 200 | + * @dataProvider providerRemoveNonExistingData |
| 201 | + */ |
| 202 | + public function testRemove_NonExistingData_ResultNotExists(string $text, string $data) |
| 203 | + { |
| 204 | + $query = QueryFactory::create()->createQuery($text); |
| 205 | + $document = NodeValueFactory::create()->createValue($data); |
| 206 | + $actualValue = Processor::create() |
| 207 | + ->delete($query, $document) |
| 208 | + ->exists(); |
| 209 | + |
| 210 | + self::assertFalse($actualValue); |
| 211 | + } |
| 212 | + |
| 213 | + public function providerRemoveNonExistingData(): array |
| 214 | + { |
| 215 | + return [ |
| 216 | + 'LocatorPointsToWholeDocument' => ['', '{"a":"b"}'], |
| 217 | + 'NonExistingElement' => ['/0/2', '[[1,[2,[3]]]]'], |
| 218 | + 'LocatorContainsScalar' => ['/a/b', '{"a":"b"}'], |
| 219 | + 'LocatorContainsNewIndex' => ['/0/1/-', '[[1,[2,[3]]]]'], |
| 220 | + ]; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * @param mixed $data |
| 225 | + * @param string $text |
| 226 | + * @param string $value |
| 227 | + * @param string $expectedData |
| 228 | + * @dataProvider providerReplaceValueExists |
| 229 | + */ |
| 230 | + public function testReplace_ValueExists_DataReplaced( |
| 231 | + string $data, |
| 232 | + string $text, |
| 233 | + string $value, |
| 234 | + string $expectedData |
| 235 | + ) { |
| 236 | + $query = QueryFactory::create()->createQuery($text); |
| 237 | + $nodeValueFactory = NodeValueFactory::create(); |
| 238 | + $document = $nodeValueFactory->createValue($data); |
| 239 | + $replacement = $nodeValueFactory->createValue($value); |
| 240 | + $actualData = Processor::create() |
| 241 | + ->replace($query, $document, $replacement) |
| 242 | + ->encode(); |
| 243 | + |
| 244 | + self::assertSame($expectedData, $actualData); |
| 245 | + } |
| 246 | + |
| 247 | + public function providerReplaceValueExists(): array |
| 248 | + { |
| 249 | + return [ |
| 250 | + 'element' => ['[1]', "/0", '2', '[2]'], |
| 251 | + 'property' => ['{"a":"b"}', "/a", '"c"', '{"a":"c"}'], |
| 252 | + 'root' => ['"a"', "", '"b"', '"b"'], |
| 253 | + ]; |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * @param string $data |
| 258 | + * @param string $text |
| 259 | + * @param string $value |
| 260 | + * @dataProvider providerReplaceValueNotExists |
| 261 | + */ |
| 262 | + public function testReplace_ValueNotExists_ResultNotExists( |
| 263 | + string $data, |
| 264 | + string $text, |
| 265 | + string $value |
| 266 | + ) { |
| 267 | + $query = QueryFactory::create()->createQuery($text); |
| 268 | + $nodeValueFactory = NodeValueFactory::create(); |
| 269 | + $document = $nodeValueFactory->createValue($data); |
| 270 | + $replacement = $nodeValueFactory->createValue($value); |
| 271 | + $actualData = Processor::create() |
| 272 | + ->replace($query, $document, $replacement) |
| 273 | + ->exists(); |
| 274 | + |
| 275 | + self::assertFalse($actualData); |
| 276 | + } |
| 277 | + |
| 278 | + public function providerReplaceValueNotExists(): array |
| 279 | + { |
| 280 | + return [ |
| 281 | + 'ElementNotExists' => ['[1]', '/1', '2'], |
| 282 | + 'PropertyNotExists' => ['{"a":"b"}', '/c', '"d"'], |
| 283 | + 'ScalarSelection' => ['"a"', '/a', '"b"'], |
| 284 | + 'propertyReference' => ['[1]', "/a", '2'], |
| 285 | + 'newIndexReference' => ['[1]', "/-", '2'], |
| 286 | + ]; |
| 287 | + } |
| 288 | +} |
0 commit comments