|
2 | 2 |
|
3 | 3 | namespace blancks\JsonPatchTest; |
4 | 4 |
|
5 | | -use blancks\JsonPatch\exceptions\{ |
6 | | - FastJsonPatchException, |
| 5 | +use blancks\JsonPatch\exceptions\{FastJsonPatchException, |
7 | 6 | InvalidPatchException, |
8 | 7 | InvalidPatchOperationException, |
9 | 8 | InvalidPatchPathException, |
10 | | - UnknownPathException |
11 | | -}; |
| 9 | + MalformedPathException, |
| 10 | + UnknownPathException}; |
12 | 11 | use blancks\JsonPatch\json\{ |
13 | 12 | accessors\ArrayAccessor, |
14 | 13 | accessors\ArrayAccessorAwareTrait, |
|
47 | 46 | #[CoversClass(FastJsonPatch::class)] |
48 | 47 | #[CoversClass(FastJsonPatchException::class)] |
49 | 48 | #[UsesClass(InvalidPatchException::class)] |
| 49 | +#[UsesClass(MalformedPathException::class)] |
50 | 50 | #[UsesClass(UnknownPathException::class)] |
51 | 51 | #[UsesClass(InvalidPatchOperationException::class)] |
52 | 52 | #[UsesClass(InvalidPatchPathException::class)] |
|
76 | 76 | #[UsesClass(Test::class)] |
77 | 77 | final class FastJsonPatchTest extends JsonPatchCompliance |
78 | 78 | { |
79 | | - public function testValidPatch(): void |
80 | | - { |
81 | | - $FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar"}'); |
82 | | - $this->assertTrue($FastJsonPatch->isValidPatch('[{"op":"test","path":"/foo","value":"bar"}]')); |
83 | | - } |
84 | | - |
85 | | - public function testInvalidPatch(): void |
| 79 | + /** |
| 80 | + * @return array<string, array{string|PatchOperationList, bool}> |
| 81 | + */ |
| 82 | + public static function isValidPatchProvider(): array |
86 | 83 | { |
87 | | - $FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar"}'); |
88 | | - $this->assertFalse($FastJsonPatch->isValidPatch('{"op":"test","path":"/foo","value":"bar"}')); |
89 | | - $this->assertFalse($FastJsonPatch->isValidPatch('[{"op":"add"}]')); |
| 84 | + return [ |
| 85 | + 'string patch - valid' => [ |
| 86 | + '[{"op":"test","path":"/foo","value":"bar"}]', |
| 87 | + true, |
| 88 | + ], |
| 89 | + 'string patch - valid (despite test not matching)' => [ |
| 90 | + '[{"op":"test","path":"/foo","value":"not this"}]', |
| 91 | + true, |
| 92 | + ], |
| 93 | + 'string patch - valid (despite unknown path)' => [ |
| 94 | + '[{"op":"test","path":"/nonexistent-path","value":"any"}]', |
| 95 | + true, |
| 96 | + ], |
| 97 | + 'string patch - invalid (is not list)' => [ |
| 98 | + '{"op":"test","path":"/foo","value":"bar"}', |
| 99 | + false, |
| 100 | + ], |
| 101 | + 'string patch - invalid (missing parameter for op)' => [ |
| 102 | + '[{"op":"add"}]', |
| 103 | + false, |
| 104 | + ], |
| 105 | + 'string patch - invalid (unknown op)' => [ |
| 106 | + '[{"op":"unknown","path":"/foo","value":"bar"}]', |
| 107 | + false, |
| 108 | + ], |
| 109 | + 'string patch - invalid (invalid path)' => [ |
| 110 | + '[{"op":"remove","path":"not a path"}]', |
| 111 | + false, |
| 112 | + ], |
| 113 | + 'DTO patch - valid' => [ |
| 114 | + new PatchOperationList(new Test(path: '/foo', value: 'bar')), |
| 115 | + true, |
| 116 | + ], |
| 117 | + 'DTO patch - valid (despite test not matching)' => [ |
| 118 | + new PatchOperationList(new Test(path: '/foo', value: 'not this')), |
| 119 | + true, |
| 120 | + ], |
| 121 | + 'DTO patch - valid (despite unknown path)' => [ |
| 122 | + new PatchOperationList(new Test(path: '/nonexistent-path', value: 'any')), |
| 123 | + true, |
| 124 | + ], |
| 125 | + 'DTO patch - invalid (invalid path)' => [ |
| 126 | + new PatchOperationList(new Remove(path: 'not a path')), |
| 127 | + false, |
| 128 | + ], |
| 129 | + ]; |
90 | 130 | } |
91 | 131 |
|
92 | | - public function testUnknownPatchOperation(): void |
| 132 | + #[DataProvider('isValidPatchProvider')] |
| 133 | + public function testIsValidPatch(string|PatchOperationList $patch, bool $expect): void |
93 | 134 | { |
94 | 135 | $FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar"}'); |
95 | | - $this->assertFalse($FastJsonPatch->isValidPatch('[{"op":"unknown","path":"/foo","value":"bar"}]')); |
| 136 | + $this->assertSame($expect, $FastJsonPatch->isValidPatch($patch)); |
96 | 137 | } |
97 | 138 |
|
98 | 139 | /** |
|
0 commit comments