forked from cebe/php-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonPointerTest.php
More file actions
176 lines (157 loc) · 5.9 KB
/
Copy pathJsonPointerTest.php
File metadata and controls
176 lines (157 loc) · 5.9 KB
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
use cebe\openapi\json\JsonPointer;
use cebe\openapi\json\JsonReference;
class JsonPointerTest extends \PHPUnit\Framework\TestCase
{
public function encodeDecodeData()
{
return [
['~0', '~'],
['~1', '/'],
['something', 'something'],
['~01', '~1'],
['~1~0', '/~'],
['~0~1', '~/'],
['~0~0', '~~'],
['~1~1', '//'],
['some~1path~1', 'some/path/'],
['1some0~11path0~1', '1some0/1path0/'],
['1some0~11path~00', '1some0/1path~0'],
];
}
/**
* @dataProvider encodeDecodeData
*/
public function testEncode($encoded, $decoded)
{
$this->assertEquals($encoded, JsonPointer::encode($decoded));
}
/**
* @dataProvider encodeDecodeData
*/
public function testDecode($encoded, $decoded)
{
$this->assertEquals($decoded, JsonPointer::decode($encoded));
}
/**
* @link https://tools.ietf.org/html/rfc6901#section-5
*/
public function rfcJsonDocument()
{
return <<<JSON
{
"foo": ["bar", "baz"],
"": 0,
"a/b": 1,
"c%d": 2,
"e^f": 3,
"g|h": 4,
"i\\\\j": 5,
"k\"l": 6,
" ": 7,
"m~n": 8
}
JSON;
}
/**
* @link https://tools.ietf.org/html/rfc6901#section-5
*/
public function rfcExamples()
{
$return = [
["" , "#" , json_decode($this->rfcJsonDocument())],
["/foo" , "#/foo" , ["bar", "baz"]],
["/foo/0", "#/foo/0", "bar"],
["/" , "#/" , 0],
["/a~1b" , "#/a~1b" , 1],
["/c%d" , "#/c%25d", 2],
["/e^f" , "#/e%5Ef", 3],
["/g|h" , "#/g%7Ch", 4],
["/i\\j" , "#/i%5Cj", 5],
["/k\"l" , "#/k%22l", 6],
["/ " , "#/%20" , 7],
["/m~0n" , "#/m~0n" , 8],
];
foreach ($return as $example) {
$example[3] = $this->rfcJsonDocument();
yield $example;
}
}
public function allExamples()
{
yield from $this->rfcExamples();
yield ["/a#b" , "#/a%23b" , 16, '{"a#b": 16}'];
}
/**
* @dataProvider allExamples
*/
public function testUriEncoding($jsonPointer, $uriJsonPointer, $expectedEvaluation)
{
$pointer = new JsonPointer($jsonPointer);
$this->assertSame($jsonPointer, $pointer->getPointer());
$this->assertSame($uriJsonPointer, JsonReference::createFromUri('', $pointer)->getReference());
$reference = JsonReference::createFromReference($uriJsonPointer);
$this->assertSame($jsonPointer, $reference->getJsonPointer()->getPointer());
$this->assertSame('', $reference->getDocumentUri());
$this->assertSame($uriJsonPointer, $reference->getReference());
$reference = JsonReference::createFromReference("somefile.json$uriJsonPointer");
$this->assertSame($jsonPointer, $reference->getJsonPointer()->getPointer());
$this->assertSame("somefile.json", $reference->getDocumentUri());
$this->assertSame("somefile.json$uriJsonPointer", $reference->getReference());
}
/**
* @dataProvider rfcExamples
*/
public function testEvaluation($jsonPointer, $uriJsonPointer, $expectedEvaluation)
{
$document = json_decode($this->rfcJsonDocument());
$pointer = new JsonPointer($jsonPointer);
$this->assertEquals($expectedEvaluation, $pointer->evaluate($document));
$document = json_decode($this->rfcJsonDocument());
$reference = JsonReference::createFromReference($uriJsonPointer);
$this->assertEquals($expectedEvaluation, $reference->getJsonPointer()->evaluate($document));
}
public function testEvaluationCases()
{
$document = (object) [
"" => (object) [
"" => 42
]
];
$pointer = new JsonPointer('//');
$this->assertSame(42, $pointer->evaluate($document));
$document = [
"1" => null,
];
$pointer = new JsonPointer('/1');
$this->assertNull($pointer->evaluate($document));
$document = (object) [
"k" => null,
];
$pointer = new JsonPointer('/k');
$this->assertNull($pointer->evaluate($document));
}
public function testParent()
{
$this->assertNull((new JsonPointer(''))->parent());
$this->assertSame('', (new JsonPointer('/'))->parent()->getPointer());
$this->assertSame('/', (new JsonPointer('//'))->parent()->getPointer());
$this->assertSame('', (new JsonPointer('/some'))->parent()->getPointer());
$this->assertSame('/some', (new JsonPointer('/some/path'))->parent()->getPointer());
$this->assertSame('', (new JsonPointer('/a~1b'))->parent()->getPointer());
$this->assertSame('/a~1b', (new JsonPointer('/a~1b/path'))->parent()->getPointer());
$this->assertSame('/some', (new JsonPointer('/some/a~1b'))->parent()->getPointer());
}
public function testAppend()
{
$this->assertSame('/some', (new JsonPointer(''))->append('some')->getPointer());
$this->assertSame('/~1some', (new JsonPointer(''))->append('/some')->getPointer());
$this->assertSame('/~0some', (new JsonPointer(''))->append('~some')->getPointer());
$this->assertSame('/path/some', (new JsonPointer('/path'))->append('some')->getPointer());
$this->assertSame('/path/~1some', (new JsonPointer('/path'))->append('/some')->getPointer());
$this->assertSame('/path/~0some', (new JsonPointer('/path'))->append('~some')->getPointer());
$this->assertSame('/a~1b/some', (new JsonPointer('/a~1b'))->append('some')->getPointer());
$this->assertSame('/a~1b/~1some', (new JsonPointer('/a~1b'))->append('/some')->getPointer());
$this->assertSame('/a~1b/~0some', (new JsonPointer('/a~1b'))->append('~some')->getPointer());
}
}