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

MergePatchTest::doTest()   A

Complexity

Conditions 5
Paths 24

Size

Total Lines 40
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 28
c 1
b 0
f 0
nc 24
nop 1
dl 0
loc 40
rs 9.1608
1
<?php
2
3
namespace Swaggest\JsonDiff\Tests;
4
5
6
use Swaggest\JsonDiff\Exception;
7
use Swaggest\JsonDiff\JsonDiff;
8
use Swaggest\JsonDiff\JsonMergePatch;
9
10
class MergePatchTest extends \PHPUnit_Framework_TestCase
11
{
12
13
    public function testSimple()
14
    {
15
        $case = json_decode(<<<'JSON'
16
  {
17
    "doc": {},
18
    "patch": {
19
      "a": {
20
        "bb": {
21
          "ccc": null
22
        }
23
      }
24
    },
25
    "expected": {
26
      "a": {
27
        "bb": {}
28
      }
29
    }
30
  }
31
JSON
32
        );
33
        $doc = $case->doc;
34
        $patch = $case->patch;
35
        $expected = $case->expected;
36
37
        JsonMergePatch::apply($doc, $patch);
38
39
        $this->assertEquals($expected, $doc);
40
    }
41
42
    public function test13()
43
    {
44
        $case = json_decode(<<<'JSON'
45
{
46
    "doc": [
47
        1,
48
        2
49
    ],
50
    "patch": {
51
        "a": "b",
52
        "c": null
53
    },
54
    "expected": {
55
        "a": "b"
56
    }
57
}
58
JSON
59
        );
60
        $doc = $case->doc;
61
        $patch = $case->patch;
62
        $expected = $case->expected;
63
64
        JsonMergePatch::apply($doc, $patch);
65
66
        $this->assertEquals($expected, $doc);
67
68
    }
69
70
    public function testGetMergePatch()
71
    {
72
        $case = json_decode(<<<'JSON'
73
{
74
    "doc": {
75
        "a": {
76
            "b": "c"
77
        }
78
    },
79
    "patch": {
80
        "a": {
81
            "b": "d",
82
            "c": null
83
        }
84
    },
85
    "expected": {
86
        "a": {
87
            "b": "d"
88
        }
89
    }
90
}
91
JSON
92
        );
93
        $doc = $case->doc;
94
        $expected = $case->expected;
95
        $diff = new JsonDiff($doc, $expected);
96
        $mergePatch = $diff->getMergePatch();
97
        JsonMergePatch::apply($doc, $mergePatch);
98
99
        $this->assertEquals($expected, $doc, 'Apply created failed: ' . json_encode(
100
                [
101
                    "test" => $case,
102
                    "patch" => $mergePatch,
103
                    "result" => $doc,
104
                ], JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT));
105
106
    }
107
108
109
    public function testGetMergePatchOfArray()
110
    {
111
        $case = json_decode(<<<'JSON'
112
{
113
    "doc": {
114
        "a": "b"
115
    },
116
    "patch": [
117
        "c"
118
    ],
119
    "expected": [
120
        "c"
121
    ]
122
}
123
JSON
124
        );
125
        $doc = $case->doc;
126
        $expected = $case->expected;
127
        $diff = new JsonDiff($doc, $expected);
128
        $mergePatch = $diff->getMergePatch();
129
        JsonMergePatch::apply($doc, $mergePatch);
130
131
        $this->assertEquals($expected, $doc, 'Apply created failed: ' . json_encode(
132
                [
133
                    "test" => $case,
134
                    "patch" => $mergePatch,
135
                    "result" => $doc,
136
                ], JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT));
137
138
    }
139
140
141
    public function testSame()
142
    {
143
        $case = json_decode(<<<'JSON'
144
{
145
    "doc": {
146
        "a": {
147
            "b": "d"
148
        }
149
    },
150
    "patch": {
151
        "a": {
152
            "b": "d"
153
        }
154
    },
155
    "expected": {
156
        "a": {
157
            "b": "d"
158
        }
159
    }
160
}
161
JSON
162
        );
163
        $doc = $case->doc;
164
        $expected = $case->expected;
165
        $diff = new JsonDiff($doc, $expected);
166
        //print_r($diff->getPatch());
167
        $mergePatch = $diff->getMergePatch();
168
        JsonMergePatch::apply($doc, $mergePatch);
169
170
        $this->assertEquals($expected, $doc, 'Apply created failed: ' . json_encode(
171
                [
172
                    "test" => $case,
173
                    "patch" => $mergePatch,
174
                    "result" => $doc,
175
                ], JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT));
176
177
    }
178
179
    public function testArraySwap()
180
    {
181
        $case = json_decode(<<<'JSON'
182
{
183
    "doc": [
184
        "a",
185
        "b"
186
    ],
187
    "patch": [
188
        "c",
189
        "d"
190
    ],
191
    "expected": [
192
        "c",
193
        "d"
194
    ]
195
}
196
JSON
197
        );
198
        $doc = $case->doc;
199
        $expected = $case->expected;
200
        $diff = new JsonDiff($doc, $expected);
201
        //print_r($diff->getPatch());
202
        $mergePatch = $diff->getMergePatch();
203
        JsonMergePatch::apply($doc, $mergePatch);
204
205
        $this->assertEquals($expected, $doc, 'Apply created failed: ' . json_encode(
206
                [
207
                    "test" => $case,
208
                    "patch" => $mergePatch,
209
                    "result" => $doc,
210
                ], JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT));
211
212
213
    }
214
215
216
    /**
217
     * @dataProvider specTestsProvider
218
     */
219
    public function testSpec($case)
220
    {
221
        $this->doTest($case);
222
    }
223
224
    public function specTestsProvider()
225
    {
226
        return $this->provider(__DIR__ . '/../assets/merge-patch.json');
227
    }
228
229
    protected function provider($path)
230
    {
231
        $cases = json_decode(file_get_contents($path));
232
233
        $testCases = array();
234
        foreach ($cases as $i => $case) {
235
            if (!isset($case->comment)) {
236
                $comment = 'unknown' . $i;
237
            } else {
238
                $comment = $case->comment;
239
            }
240
241
            $testCases[$comment] = array(
242
                'case' => $case,
243
            );
244
        }
245
        return $testCases;
246
    }
247
248
    protected function doTest($case)
249
    {
250
        $case = clone $case;
251
252
        if (!is_object($case->doc)) {
253
            $doc = $case->doc;
254
        } else {
255
            $doc = clone $case->doc;
256
        }
257
        $patch = $case->patch;
258
        $expected = isset($case->expected) ? $case->expected : null;
259
        $jsonOptions = JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT;
260
261
262
        JsonMergePatch::apply($doc, $patch);
263
        $this->assertEquals($expected, $doc, 'Apply failed: ' . json_encode(
264
                [
265
                    "test" => $case,
266
                    "result" => $doc,
267
                ], $jsonOptions));
268
269
        if (!is_object($case->doc)) {
270
            $doc = $case->doc;
271
        } else {
272
            $doc = clone $case->doc;
273
        }
274
        try {
275
            $diff = new JsonDiff($doc, $expected);
276
            $mergePatch = $diff->getMergePatch();
277
        } catch (Exception $exception) {
278
            $mergePatch = $exception->getMessage();
279
        }
280
        JsonMergePatch::apply($doc, $mergePatch);
281
282
        $this->assertEquals($expected, $doc, 'Apply created failed: ' . json_encode(
283
                [
284
                    "test" => $case,
285
                    "patch" => $mergePatch,
286
                    "result" => $doc,
287
                ], $jsonOptions));
288
289
290
    }
291
292
    public function testComplex()
293
    {
294
        $original = json_decode(file_get_contents(__DIR__ . '/../assets/original.json'));
295
        $new = json_decode(file_get_contents(__DIR__ . '/../assets/new.json'));
296
297
        $diff = new JsonDiff($original, $new);
298
        $mergePatch = $diff->getMergePatch();
299
        $mergePatchJson = json_encode($mergePatch, JSON_UNESCAPED_SLASHES + JSON_PRETTY_PRINT);
300
301
        $this->assertEquals(file_get_contents(__DIR__ . '/../assets/merge.json') , $mergePatchJson);
302
303
        JsonMergePatch::apply($original, $mergePatch);
304
        $this->assertEquals($new, $original);
305
    }
306
307
308
}