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

Issue6Test::testDoubleRemove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
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 testDefault()
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->assertJsonStringEqualsJsonString(<<<'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": "/1"
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
    public function testOriginal()
53
    {
54
        $originalJson = '[{"name":"a"},{"name":"b"},{"name":"c"}]';
55
        $newJson = '[{"name":"b"}]';
56
        $diff = new JsonDiff(json_decode($originalJson), json_decode($newJson));
57
58
        $patchJson = json_decode(json_encode($diff->getPatch()->jsonSerialize()), true);
59
60
        $original = json_decode($originalJson);
61
        $patch = JsonPatch::import($patchJson);
62
        $patch->apply($original);
63
        $this->assertEquals($original, json_decode($newJson));
64
    }
65
66
67
    public function testDoubleInverseRemove()
68
    {
69
        $json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
70
        $json2 = json_decode('[{"name":"b"}]');
71
72
        $patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/2"},{"op":"remove","path":"/0"}]'));
73
74
        $json1a = $json1;
75
        $patch->apply($json1a);
76
        $this->assertEquals(json_encode($json2), json_encode($json1a));
77
    }
78
79
    public function testDoubleRemove()
80
    {
81
        $json1 = json_decode('[{"name":"a"},{"name":"b"},{"name":"c"}]');
82
        $json2 = json_decode('[{"name":"b"}]');
83
84
        $patch = JsonPatch::import(json_decode('[{"op":"remove","path":"/0"},{"op":"remove","path":"/1"}]'));
85
86
        $json1a = $json1;
87
        $patch->apply($json1a);
88
        $this->assertEquals(json_encode($json2), json_encode($json1a));
89
    }
90
}