Passed
Pull Request — master (#42)
by Viacheslav
02:53 queued 10s
created

RearrangeTest::testNull()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 31
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 38
rs 9.424
1
<?php
2
3
namespace Swaggest\JsonDiff\Tests;
4
5
6
use Swaggest\JsonDiff\JsonDiff;
7
use Swaggest\JsonDiff\JsonPatch;
8
use Swaggest\JsonDiff\ModifiedPathDiff;
9
10
class RearrangeTest extends \PHPUnit_Framework_TestCase
11
{
12
    /**
13
     * @throws \Swaggest\JsonDiff\Exception
14
     */
15
    public function testKeepOrder()
16
    {
17
        $originalJson = <<<'JSON'
18
{
19
    "key1": [4, 1, 2, 3],
20
    "key2": 2,
21
    "key3": {
22
        "sub0": 0,
23
        "sub1": "a",
24
        "sub2": "b"
25
    },
26
    "key4": [
27
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
28
    ]
29
}
30
JSON;
31
32
        $newJson = <<<'JSON'
33
{
34
    "key5": "wat",
35
    "key1": [5, 1, 2, 3],
36
    "key4": [
37
        {"c":false, "a":2}, {"a":1, "b":true}, {"c":1, "a":3}
38
    ],
39
    "key3": {
40
        "sub3": 0,
41
        "sub2": false,
42
        "sub1": "c"
43
    }
44
}
45
JSON;
46
47
        $expected = <<<'JSON'
48
{
49
    "key1": [5, 1, 2, 3],
50
    "key3": {
51
        "sub1": "c",
52
        "sub2": false,
53
        "sub3": 0
54
    },
55
    "key4": [
56
        {"a":1, "b":true}, {"a":2, "c":false}, {"a":3, "c":1}
57
    ],
58
    "key5": "wat"
59
}
60
JSON;
61
62
        $r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS + JsonDiff::COLLECT_MODIFIED_DIFF);
63
        $this->assertSame(
64
            json_encode(json_decode($expected), JSON_PRETTY_PRINT),
65
            json_encode($r->getRearranged(), JSON_PRETTY_PRINT)
66
        );
67
        $this->assertSame('{"key3":{"sub3":0},"key4":{"1":{"c":false},"2":{"c":1}},"key5":"wat"}',
68
            json_encode($r->getAdded()));
69
        $this->assertSame(array(
70
            '/key3/sub3',
71
            '/key4/1/c',
72
            '/key4/2/c',
73
            '/key5',
74
        ), $r->getAddedPaths());
75
        $this->assertSame('{"key2":2,"key3":{"sub0":0},"key4":{"1":{"b":false}}}',
76
            json_encode($r->getRemoved()));
77
        $this->assertSame(array(
78
            '/key2',
79
            '/key3/sub0',
80
            '/key4/1/b',
81
        ), $r->getRemovedPaths());
82
83
        $this->assertSame(array(
84
            '/key1/0',
85
            '/key3/sub1',
86
            '/key3/sub2',
87
        ), $r->getModifiedPaths());
88
89
        $this->assertSame('{"key1":[4],"key3":{"sub1":"a","sub2":"b"}}', json_encode($r->getModifiedOriginal()));
90
        $this->assertSame('{"key1":[5],"key3":{"sub1":"c","sub2":false}}', json_encode($r->getModifiedNew()));
91
92
        $this->assertEquals([
93
            new ModifiedPathDiff('/key1/0', 4, 5),
94
            new ModifiedPathDiff('/key3/sub1', 'a', 'c'),
95
            new ModifiedPathDiff('/key3/sub2', 'b', false),
96
        ], $r->getModifiedDiff());
97
    }
98
99
100
    public function testRemoved()
101
    {
102
        $originalJson = <<<'JSON'
103
{
104
    "key2": 2,
105
    "key3": {
106
        "sub0": 0,
107
        "sub1": "a",
108
        "sub2": "b"
109
    },
110
    "key4": [
111
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
112
    ]
113
}
114
JSON;
115
116
        $newJson = <<<'JSON'
117
{
118
    "key3": {
119
        "sub3": 0,
120
        "sub2": false,
121
        "sub1": "c"
122
    }
123
}
124
JSON;
125
126
        $expected = <<<'JSON'
127
{
128
    "key2": 2,
129
    "key3": {
130
        "sub0": 0
131
    },
132
    "key4": [
133
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
134
    ]
135
}
136
JSON;
137
138
        $r = new JsonDiff(json_decode($originalJson), json_decode($newJson));
139
        $this->assertSame(array(
140
            '/key2',
141
            '/key3/sub0',
142
            '/key4',
143
        ), $r->getRemovedPaths());
144
145
        $this->assertSame(3, $r->getRemovedCnt());
146
147
        $this->assertSame(
148
            json_encode(json_decode($expected), JSON_PRETTY_PRINT),
149
            json_encode($r->getRemoved(), JSON_PRETTY_PRINT)
150
        );
151
152
    }
153
154
155
    public function testNull()
156
    {
157
        $originalJson = <<<'JSON'
158
{
159
    "key2": 2,
160
    "key3": null,
161
    "key4": [
162
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
163
    ]
164
}
165
JSON;
166
167
        $newJson = <<<'JSON'
168
{
169
    "key3": null
170
}
171
JSON;
172
173
        $expected = <<<'JSON'
174
{
175
    "key2": 2,
176
    "key4": [
177
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
178
    ]
179
}
180
JSON;
181
182
        $r = new JsonDiff(json_decode($originalJson), json_decode($newJson));
183
        $this->assertSame(array(
184
            '/key2',
185
            '/key4',
186
        ), $r->getRemovedPaths());
187
188
        $this->assertSame(2, $r->getRemovedCnt());
189
190
        $this->assertSame(
191
            json_encode(json_decode($expected), JSON_PRETTY_PRINT),
192
            json_encode($r->getRemoved(), JSON_PRETTY_PRINT)
193
        );
194
195
    }
196
197
    /**
198
     * @throws \Swaggest\JsonDiff\Exception
199
     */
200
    public function testDiff()
201
    {
202
        $originalJson = <<<'JSON'
203
[{"a":1,"b":true,"subs":[{"s":1},{"s":2},{"s":3}]},{"a":2,"b":false},{"a":3}]
204
JSON;
205
206
        $newJson = <<<'JSON'
207
[{"c":false,"a":2},{"a":1,"b":true,"subs":[{"s":3, "add": true},{"s":2},{"s":1}]},{"c":1,"a":3}]
208
JSON;
209
210
        $rearrangedJson = <<<'JSON'
211
[{"a":1,"b":true,"subs":[{"s":1},{"s":2},{"s":3,"add":true}]},{"a":2,"c":false},{"a":3,"c":1}]
212
JSON;
213
214
215
        $patchJson = <<<'JSON'
216
[
217
    {"value":true,"op":"add","path":"/0/subs/2/add"},
218
    {"op":"remove","path":"/1/b"},
219
    {"value":false,"op":"add","path":"/1/c"},
220
    {"value":1,"op":"add","path":"/2/c"}
221
]
222
JSON;
223
224
225
        $diff = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::REARRANGE_ARRAYS);
226
        $this->assertEquals(json_decode($patchJson), $diff->getPatch()->jsonSerialize());
227
228
        $original = json_decode($originalJson);
229
        $patch = JsonPatch::import(json_decode($patchJson));
230
        $patch->apply($original);
231
        $this->assertEquals($rearrangedJson, json_encode($original, JSON_UNESCAPED_SLASHES));
232
    }
233
234
235
}