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

JsonPatchTest::testApplyNonExistentLevelOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
c 1
b 0
f 1
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Swaggest\JsonDiff\Tests;
4
5
use Swaggest\JsonDiff\Exception;
6
use Swaggest\JsonDiff\JsonDiff;
7
use Swaggest\JsonDiff\JsonPatch;
8
9
class JsonPatchTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @throws Exception
13
     */
14
    public function testImportExport()
15
    {
16
        $data = json_decode(<<<'JSON'
17
[
18
  { "op": "replace", "path": "/baz", "value": "boo" },
19
  { "op": "add", "path": "/hello", "value": ["world"] },
20
  { "op": "remove", "path": "/foo"}
21
]
22
JSON
23
        );
24
        $patch = JsonPatch::import($data);
25
26
        $exported = JsonPatch::export($patch);
27
28
        $diff = new JsonDiff($data, $exported);
29
        $this->assertSame(0, $diff->getDiffCnt());
30
    }
31
32
33
    public function testNull()
34
    {
35
        $originalJson = <<<'JSON'
36
{
37
    "key2": 2,
38
    "key3": null,
39
    "key4": [
40
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
41
    ]
42
}
43
JSON;
44
45
        $newJson = <<<'JSON'
46
{
47
    "key3": null
48
}
49
JSON;
50
51
        $expected = <<<'JSON'
52
{
53
    "key2": 2,
54
    "key4": [
55
        {"a":1, "b":true}, {"a":2, "b":false}, {"a":3}
56
    ]
57
}
58
JSON;
59
60
        $r = new JsonDiff(json_decode($originalJson), json_decode($newJson), JsonDiff::JSON_URI_FRAGMENT_ID);
61
        $this->assertSame(array(
62
            '#/key2',
63
            '#/key4',
64
        ), $r->getRemovedPaths());
65
66
        $this->assertSame(2, $r->getRemovedCnt());
67
68
        $this->assertSame(
69
            json_encode(json_decode($expected), JSON_PRETTY_PRINT),
70
            json_encode($r->getRemoved(), JSON_PRETTY_PRINT)
71
        );
72
73
    }
74
75
    public function testMissingOp()
76
    {
77
        $this->setExpectedException(get_class(new Exception()), 'Missing "op" in operation data');
78
        JsonPatch::import(array((object)array('path' => '/123')));
79
    }
80
81
    public function testMissingPath()
82
    {
83
        $this->setExpectedException(get_class(new Exception()), 'Missing "path" in operation data');
84
        JsonPatch::import(array((object)array('op' => 'wat')));
85
    }
86
87
    public function testInvalidOp()
88
    {
89
        $this->setExpectedException(get_class(new Exception()), 'Unknown "op": wat');
90
        JsonPatch::import(array((object)array('op' => 'wat', 'path' => '/123')));
91
    }
92
93
    public function testMissingFrom()
94
    {
95
        $this->setExpectedException(get_class(new Exception()), 'Missing "from" in operation data');
96
        JsonPatch::import(array((object)array('op' => 'copy', 'path' => '/123')));
97
    }
98
99
    public function testMissingValue()
100
    {
101
        $this->setExpectedException(get_class(new Exception()), 'Missing "value" in operation data');
102
        JsonPatch::import(array(array('op' => 'add', 'path' => '/123')));
103
    }
104
105
    public function testApply()
106
    {
107
        $p = JsonPatch::import(array(array('op' => 'copy', 'path' => '/1', 'from' => '/0')));
108
        $original = array('AAA');
109
        $p->apply($original);
110
        $this->assertSame(array('AAA', 'AAA'), $original);
111
    }
112
113
    public function testApplyContinueOnError()
114
    {
115
        $p = new JsonPatch();
116
        $p->op(new JsonPatch\Test('/missing', 1));
117
        $p->op(new JsonPatch\Copy('/1', '/0'));
118
        $p->op(new JsonPatch\Test('/missing2', null));
119
        $original = array('AAA');
120
        $errors = $p->apply($original, false);
121
        $this->assertSame(array('AAA', 'AAA'), $original);
122
        $this->assertSame('Key not found: missing', $errors[0]->getMessage());
123
        $this->assertSame('Key not found: missing2', $errors[1]->getMessage());
124
    }
125
126
127
    public function testApplyNonExistentLevelTwo()
128
    {
129
        $data = new \stdClass();
130
        $p = new JsonPatch();
131
        $p->op(new JsonPatch\Add('/some/path', 22));
132
        $p->apply($data, false);
133
        $this->assertEquals(new \stdClass(), $data);
134
    }
135
136
    public function testApplyNonExistentLevelOne()
137
    {
138
        $data = new \stdClass();
139
        $p = new JsonPatch();
140
        $p->op(new JsonPatch\Add('/some', 22));
141
        $p->apply($data);
142
        $this->assertEquals((object)array('some' => 22), $data);
143
    }
144
145
}