Skip to content

Commit 3747bce

Browse files
committed
feat: isValidPatch should also accept PatchOperationList
Internally, this is already implemented - it just required a change to the typehint for `isValidPatch`. I've refactored the existing `isValidPatch` tests to use a DataProvider to make the overlap between examples for strings and DTOs clearer. As part of this I added a couple of new examples of valid / invalid string patches. This is because some of the existing string cases (missing params / structural issues) cannot happen with DTOs.
1 parent ab22ffa commit 3747bce

2 files changed

Lines changed: 59 additions & 18 deletions

File tree

src/FastJsonPatch.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,10 @@ public function apply(string|PatchOperationList $patch): void
166166

167167
/**
168168
* Tells if the json patch is syntactically valid
169-
* @param string $patch
169+
* @param string|PatchOperationList $patch
170170
* @return bool
171171
*/
172-
public function isValidPatch(string $patch): bool
172+
public function isValidPatch(string|PatchOperationList $patch): bool
173173
{
174174
try {
175175
foreach ($this->patchIterator($patch) as $op => $p) {

tests/FastJsonPatchTest.php

Lines changed: 57 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22

33
namespace blancks\JsonPatchTest;
44

5-
use blancks\JsonPatch\exceptions\{
6-
FastJsonPatchException,
5+
use blancks\JsonPatch\exceptions\{FastJsonPatchException,
76
InvalidPatchException,
87
InvalidPatchOperationException,
98
InvalidPatchPathException,
10-
UnknownPathException
11-
};
9+
MalformedPathException,
10+
UnknownPathException};
1211
use blancks\JsonPatch\json\{
1312
accessors\ArrayAccessor,
1413
accessors\ArrayAccessorAwareTrait,
@@ -47,6 +46,7 @@
4746
#[CoversClass(FastJsonPatch::class)]
4847
#[CoversClass(FastJsonPatchException::class)]
4948
#[UsesClass(InvalidPatchException::class)]
49+
#[UsesClass(MalformedPathException::class)]
5050
#[UsesClass(UnknownPathException::class)]
5151
#[UsesClass(InvalidPatchOperationException::class)]
5252
#[UsesClass(InvalidPatchPathException::class)]
@@ -76,23 +76,64 @@
7676
#[UsesClass(Test::class)]
7777
final class FastJsonPatchTest extends JsonPatchCompliance
7878
{
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
8683
{
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+
];
90130
}
91131

92-
public function testUnknownPatchOperation(): void
132+
#[DataProvider('isValidPatchProvider')]
133+
public function testIsValidPatch(string|PatchOperationList $patch, bool $expect): void
93134
{
94135
$FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar"}');
95-
$this->assertFalse($FastJsonPatch->isValidPatch('[{"op":"unknown","path":"/foo","value":"bar"}]'));
136+
$this->assertSame($expect, $FastJsonPatch->isValidPatch($patch));
96137
}
97138

98139
/**

0 commit comments

Comments
 (0)