1+ <?php
2+
3+ namespace Swaggest \JsonDiff \Tests \Issues ;
4+
5+ use Swaggest \JsonDiff \JsonDiff ;
6+ use Swaggest \JsonDiff \JsonPatch ;
7+
8+
9+ /**
10+ * @see https://github.com/swaggest/json-diff/issues/6
11+ */
12+ class Issue6Test extends \PHPUnit_Framework_TestCase
13+ {
14+ public function testIssue6 ()
15+ {
16+ $ json1 = json_decode ('[{"name":"a"},{"name":"b"},{"name":"c"}] ' );
17+ $ json2 = json_decode ('[{"name":"b"}] ' );
18+
19+ $ diff = new JsonDiff ($ json1 , $ json2 );
20+ $ patch = $ diff ->getPatch ();
21+
22+ $ this ->assertSame (<<<'JSON'
23+ [
24+ {
25+ "value": "a",
26+ "op": "test",
27+ "path": "/0/name"
28+ },
29+ {
30+ "value": "b",
31+ "op": "replace",
32+ "path": "/0/name"
33+ },
34+ {
35+ "op": "remove",
36+ "path": "/1"
37+ },
38+ {
39+ "op": "remove",
40+ "path": "/2"
41+ }
42+ ]
43+ JSON
44+ , json_encode ($ patch , JSON_PRETTY_PRINT + JSON_UNESCAPED_SLASHES ));
45+
46+ $ json1a = $ json1 ;
47+ $ patch ->apply ($ json1a );
48+
49+ $ this ->assertEquals ($ json2 , $ json1a );
50+ }
51+
52+
53+ public function testIssue6Remove ()
54+ {
55+ $ json1 = json_decode ('[{"name":"a"},{"name":"b"},{"name":"c"}] ' );
56+ $ json2 = json_decode ('[{"name":"b"}] ' );
57+
58+ $ patch = JsonPatch::import (json_decode ('[{"op":"remove","path":"/0"},{"op":"remove","path":"/2"}] ' ));
59+
60+ $ json1a = $ json1 ;
61+ $ patch ->apply ($ json1a );
62+ $ this ->assertEquals (json_encode ($ json2 ), json_encode ($ json1a ));
63+ /*
64+ Failed asserting that two strings are equal.
65+ Expected :'[{"name":"b"}]'
66+ Actual :'{"1":{"name":"b"}}'
67+ */
68+ }
69+ }
0 commit comments