forked from swaggest/json-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJsonPointerTest.php
139 lines (107 loc) · 4.16 KB
/
JsonPointerTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Swaggest\JsonDiff\Tests;
use Swaggest\JsonDiff\Exception;
use Swaggest\JsonDiff\JsonPointer;
use Swaggest\JsonDiff\JsonPointerException;
class JsonPointerTest extends \PHPUnit_Framework_TestCase
{
/**
* @throws Exception
*/
public function testProcess()
{
$json = new \stdClass();
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!', JsonPointer::SKIP_IF_ISSET);
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello again!');
$this->assertSame('{"l1":{"l2":{"l3":"hello again!"}}}', json_encode($json));
JsonPointer::add($json, ['l1', 'l2', 'l3'], 'hello!');
$this->assertSame('{"l1":{"l2":{"l3":"hello!"}}}', json_encode($json));
$this->assertSame('{"l3":"hello!"}', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2'))));
try {
$this->assertSame('null', json_encode(JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/non-existent'))));
} catch (JsonPointerException $exception) {
$this->assertSame('Key not found: non-existent', $exception->getMessage());
}
JsonPointer::remove($json, ['l1', 'l2']);
$this->assertSame('{"l1":{}}', json_encode($json));
JsonPointer::add($json, JsonPointer::splitPath('/l1/l2/0/0'), 0);
JsonPointer::add($json, JsonPointer::splitPath('#/l1/l2/1/1'), 1);
$this->assertSame('{"l1":{"l2":[[0],{"1":1}]}}', json_encode($json));
$this->assertSame(1, JsonPointer::get($json, JsonPointer::splitPath('/l1/l2/1/1')));
$this->assertSame(1, JsonPointer::getByPointer($json, '/l1/l2/1/1'));
}
/**
* @throws Exception
*/
public function testNumericKey()
{
$json = json_decode('{"l1":{"200":1}}');
$this->assertSame(1, JsonPointer::get($json, JsonPointer::splitPath('/l1/200')));
}
public function testEscapeSegment()
{
$segment = '/project/{username}/{project}';
$this->assertSame('~1project~1%7Busername%7D~1%7Bproject%7D', JsonPointer::escapeSegment($segment, true));
}
public function testBuildPath()
{
$pathItems = ['key1', '/project/{username}/{project}', 'key2'];
$this->assertSame('/key1/~1project~1{username}~1{project}/key2',
JsonPointer::buildPath($pathItems));
$this->assertSame('#/key1/~1project~1%7Busername%7D~1%7Bproject%7D/key2',
JsonPointer::buildPath($pathItems, true));
}
public function testGetSetDeleteObject()
{
$s = new Sample();
$s->one = new Sample();
$s->one->two = 2;
$this->assertEquals(2, JsonPointer::get($s, ['one', 'two']));
JsonPointer::add($s, ['one', 'two'], 22);
$this->assertEquals(22, JsonPointer::get($s, ['one', 'two']));
$this->assertEquals(22, $s->one->two);
JsonPointer::remove($s, ['one', 'two']);
try {
JsonPointer::get($s, ['one', 'two']);
$this->fail('Exception expected');
} catch (JsonPointerException $e) {
$this->assertEquals('Key not found: two', $e->getMessage());
}
$this->assertEquals(null, $s->one->two);
}
public function testSequentialArrayIsPreserved()
{
$json = [ 'l1' => [ 0 => 'foo', 1 => 'bar', 2 => 'baz' ] ];
JsonPointer::remove($json, ['l1', '1'], JsonPointer::TOLERATE_ASSOCIATIVE_ARRAYS);
$this->assertSame('{"l1":["foo","baz"]}', json_encode($json));
}
}
class Sample
{
public $declared;
private $_data = [];
public function __isset($name)
{
return isset($this->_data[$name]);
}
public function &__get($name)
{
if (array_key_exists($name, $this->_data)) {
return $this->_data[$name];
} else {
$tmp = null;
return $tmp;;
}
}
public function __set($name, $value)
{
$this->_data[$name] = $value;
}
public function __unset($name)
{
unset($this->_data[$name]);
}
}