|
5 | 5 | use Swaggest\JsonDiff\Exception;
|
6 | 6 | use Swaggest\JsonDiff\JsonDiff;
|
7 | 7 | use Swaggest\JsonDiff\JsonPatch;
|
| 8 | +use Swaggest\JsonDiff\JsonPatch\OpPath; |
8 | 9 | use Swaggest\JsonDiff\MissingFieldException;
|
9 | 10 | use Swaggest\JsonDiff\PatchTestOperationFailedException;
|
| 11 | +use Swaggest\JsonDiff\PathException; |
10 | 12 | use Swaggest\JsonDiff\UnknownOperationException;
|
11 | 13 |
|
12 | 14 | class JsonPatchTest extends \PHPUnit_Framework_TestCase
|
@@ -174,4 +176,88 @@ public function testTestOperationFailed()
|
174 | 176 | $this->assertSame($actualValue, $testError->getActualValue());
|
175 | 177 | }
|
176 | 178 |
|
| 179 | + public function testPathExceptionContinueOnError() |
| 180 | + { |
| 181 | + $actualValue = 'xyz'; |
| 182 | + $data = array('abc' => $actualValue); |
| 183 | + $patch = new JsonPatch(); |
| 184 | + |
| 185 | + $operation1 = new JsonPatch\Test('/abc', 'def'); |
| 186 | + $patch->op($operation1); |
| 187 | + |
| 188 | + $operation2 = new JsonPatch\Move('/target', '/source'); |
| 189 | + $patch->op($operation2); |
| 190 | + |
| 191 | + $errors = $patch->apply($data, false); |
| 192 | + |
| 193 | + $this->assertInstanceOf(PatchTestOperationFailedException::class, $errors[0]); |
| 194 | + $this->assertSame($operation1, $errors[0]->getOperation()); |
| 195 | + |
| 196 | + $this->assertInstanceOf(PathException::class, $errors[1]); |
| 197 | + $this->assertSame($operation2, $errors[1]->getOperation()); |
| 198 | + $this->assertSame('from', $errors[1]->getField()); |
| 199 | + } |
| 200 | + |
| 201 | + public function pathExceptionProvider() { |
| 202 | + return [ |
| 203 | + 'splitPath_path' => [ |
| 204 | + new JsonPatch\Copy('invalid/path', '/valid/from'), |
| 205 | + 'Path must start with "/": invalid/path', |
| 206 | + 'path' |
| 207 | + ], |
| 208 | + 'splitPath_from' => [ |
| 209 | + new JsonPatch\Copy('/valid/path', 'invalid/from'), |
| 210 | + 'Path must start with "/": invalid/from', |
| 211 | + 'from' |
| 212 | + ], |
| 213 | + 'add' => [ |
| 214 | + new JsonPatch\Add('/some/path', 22), |
| 215 | + 'Non-existent path item: some', |
| 216 | + 'path' |
| 217 | + ], |
| 218 | + 'get_from' => [ |
| 219 | + new JsonPatch\Copy('/target', '/source'), |
| 220 | + 'Key not found: source', |
| 221 | + 'from' |
| 222 | + ], |
| 223 | + 'get_path' => [ |
| 224 | + new JsonPatch\Replace('/some/path', 23), |
| 225 | + 'Key not found: some', |
| 226 | + 'path' |
| 227 | + ], |
| 228 | + 'remove_from' => [ |
| 229 | + new JsonPatch\Move('/target', '/source'), |
| 230 | + 'Key not found: source', |
| 231 | + 'from' |
| 232 | + ], |
| 233 | + 'remove_path' => [ |
| 234 | + new JsonPatch\Remove('/some/path'), |
| 235 | + 'Key not found: some', |
| 236 | + 'path' |
| 237 | + ] |
| 238 | + ]; |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * @param OpPath $operation |
| 243 | + * @param string $expectedMessage |
| 244 | + * @param string $expectedField |
| 245 | + * |
| 246 | + * @dataProvider pathExceptionProvider |
| 247 | + */ |
| 248 | + public function testPathException(OpPath $operation, $expectedMessage, $expectedField) { |
| 249 | + $data = new \stdClass(); |
| 250 | + $patch = new JsonPatch(); |
| 251 | + |
| 252 | + $patch->op($operation); |
| 253 | + |
| 254 | + try { |
| 255 | + $patch->apply($data ); |
| 256 | + $this->fail('PathException expected'); |
| 257 | + } catch (Exception $ex) { |
| 258 | + $this->assertInstanceOf(PathException::class, $ex); |
| 259 | + $this->assertEquals($expectedMessage, $ex->getMessage()); |
| 260 | + $this->assertEquals($expectedField, $ex->getField()); |
| 261 | + } |
| 262 | + } |
177 | 263 | }
|
0 commit comments