2525use PHPUnit \Framework \Attributes \DataProvider ;
2626use PHPUnit \Framework \Assert ;
2727use PHPUnit \Framework \Attributes \UsesClass ;
28+ use stdClass ;
2829use Throwable ;
2930
3031#[CoversClass(PatchOperationList::class)]
@@ -51,6 +52,10 @@ class PatchOperationListTest extends JsonPatchCompliance
5152 */
5253 public static function validEncodeDecodeProvider (): array
5354 {
55+ $ objValue = new stdClass ();
56+ $ objValue ->type = 'foo ' ;
57+ $ objValue ->list = ['bar ' , 'baz ' ];
58+
5459 return [
5560 'empty list ' => [
5661 [],
@@ -82,6 +87,20 @@ public static function validEncodeDecodeProvider(): array
8287 ]
8388 JSON,
8489 ],
90+ 'operations with object values ' => [
91+ [
92+ new Add (path: '/bar ' , value: $ objValue ),
93+ new Replace (path: '/bar ' , value: $ objValue ),
94+ new Test (path: '/bar ' , value: $ objValue ),
95+ ],
96+ <<<'JSON'
97+ [
98+ {"op":"add","path":"/bar","value":{"type":"foo","list":["bar","baz"]}},
99+ {"op":"replace","path":"/bar","value":{"type":"foo","list":["bar","baz"]}},
100+ {"op":"test","path":"/bar","value":{"type":"foo","list":["bar","baz"]}}
101+ ]
102+ JSON,
103+ ]
85104 ];
86105 }
87106
@@ -150,13 +169,13 @@ public function decode(string $json, array $options = []): mixed
150169 Assert::assertSame (
151170 [
152171 'json ' => '[fake] ' ,
153- 'options ' => [' associative ' => true ],
172+ 'options ' => [],
154173 ],
155174 get_defined_vars (),
156175 'JSONHandler should have been called with expected args ' ,
157176 );
158177 return [
159- ['op ' => 'remove ' , 'path ' => '/some/path ' ],
178+ ( object ) ['op ' => 'remove ' , 'path ' => '/some/path ' ],
160179 ];
161180 }
162181 },
@@ -170,23 +189,6 @@ public function decode(string $json, array $options = []): mixed
170189
171190 public function testItCanDecodeWithCustomOperationClasses (): void
172191 {
173- $ appendOperation = new class ('/greeting ' , ' World ' ) extends PatchOperation {
174- public function __construct (
175- public readonly string $ path ,
176- public readonly string $ suffix ,
177- ) {
178- parent ::__construct ('append ' );
179- }
180- };
181- $ customAddOperation = new class ('/greeting ' , 'Hello ' ) extends PatchOperation {
182- public function __construct (
183- public readonly string $ path ,
184- public readonly mixed $ value ,
185- ) {
186- parent ::__construct ('add ' );
187- }
188- };
189-
190192 $ result = PatchOperationList::fromJson (
191193 <<<'JSON'
192194 [
@@ -196,15 +198,15 @@ public function __construct(
196198 ]
197199 JSON,
198200 customClasses: [
199- 'add ' => $ customAddOperation ::class,
200- 'append ' => $ appendOperation ::class,
201+ 'add ' => CustomAdd ::class,
202+ 'append ' => Append ::class,
201203 ],
202204 );
203205
204206 $ this ->assertEquals (
205207 new PatchOperationList (
206- $ customAddOperation ,
207- $ appendOperation ,
208+ new CustomAdd ( ' /greeting ' , ' Hello ' ) ,
209+ new Append ( ' /greeting ' , ' World ' ) ,
208210 new Copy (path: '/whatever ' , from: '/greeting ' ),
209211 ),
210212 $ result ,
@@ -220,13 +222,18 @@ public static function invalidJsonProvider(): array
220222 'json is not a list (example 1) ' => [
221223 '{"some": "field"} ' ,
222224 InvalidPatchException::class,
223- 'Invalid patch structure (expected list, got array ) ' ,
225+ 'Invalid patch structure (expected list, got stdClass ) ' ,
224226 ],
225227 'json is not a list (example 2) ' => [
226228 'true ' ,
227229 InvalidPatchException::class,
228230 'Invalid patch structure (expected list, got bool) ' ,
229231 ],
232+ 'json is not a list (example 3) ' => [
233+ '{"2": {"op": "add", "path": "/some/path", "value": "World"}} ' ,
234+ InvalidPatchException::class,
235+ 'Invalid patch structure (expected list, got stdClass) ' ,
236+ ],
230237 'unknown operation ' => [
231238 '[{"op": "scramble", "path": "/anywhere"}] ' ,
232239 InvalidPatchOperationException::class,
0 commit comments