|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package silverstripe-jsontext |
|
5
|
|
|
* @subpackage fields |
|
6
|
|
|
* @author Russell Michell <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use JSONText\Fields; |
|
10
|
|
|
use JSONText\Exceptions; |
|
11
|
|
|
|
|
12
|
|
|
class JSONTextTest extends SapphireTest |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $fixtures = [ |
|
18
|
|
|
'array_simple' => 'tests/fixtures/json/array_simple.json', |
|
19
|
|
|
'hash_simple' => 'tests/fixtures/json/hash_simple.json', |
|
20
|
|
|
'invalid' => 'tests/fixtures/json/invalid.json', |
|
21
|
|
|
'hash_deep' => 'tests/fixtures/json/hash_deep.json', |
|
22
|
|
|
'hash_dupes' => 'tests/fixtures/json/hash_duplicated.json', |
|
23
|
|
|
'empty' => 'tests/fixtures/json/empty.json' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* JSONTextTest constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* Modify fixtures to be able to run on PHP <5.6 without use of constant in class property |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct() |
|
32
|
|
|
{ |
|
33
|
|
|
foreach($this->fixtures as $name => $path) { |
|
34
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function testgetValueAsIterable() |
|
39
|
|
|
{ |
|
40
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
41
|
|
|
$field->setValue($this->getFixture('invalid')); |
|
42
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
43
|
|
|
$this->assertEquals(['chinese' => 'great wall'], $field->getValueAsIterable()); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
46
|
|
|
$field->setValue($this->getFixture('empty')); |
|
47
|
|
|
$this->assertEquals([], $field->getValueAsIterable()); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
View Code Duplication |
public function testFirst_AsArray() |
|
|
|
|
|
|
51
|
|
|
{ |
|
52
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
53
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
54
|
|
|
$field->setReturnType('array'); |
|
55
|
|
|
$this->assertEquals([0 => 'great wall'], $field->first()); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
58
|
|
|
$field->setValue($this->getFixture('empty')); |
|
59
|
|
|
$field->setReturnType('array'); |
|
60
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
|
|
61
|
|
|
$this->assertCount(0, $field->first()); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
View Code Duplication |
public function testFirst_AsJson() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
67
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
68
|
|
|
$field->setReturnType('json'); |
|
69
|
|
|
$this->assertEquals('["great wall"]', $field->first()); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
72
|
|
|
$field->setValue($this->getFixture('empty')); |
|
73
|
|
|
$field->setReturnType('json'); |
|
74
|
|
|
$this->assertInternalType('string', $field->first()); |
|
|
|
|
|
|
75
|
|
|
$this->assertEquals('[]', $field->first()); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
View Code Duplication |
public function testLast_AsArray() |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
81
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
82
|
|
|
$field->setReturnType('array'); |
|
83
|
|
|
$this->assertEquals([6 => 'morris'], $field->last()); |
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
86
|
|
|
$field->setValue($this->getFixture('empty')); |
|
87
|
|
|
$field->setReturnType('array'); |
|
88
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
|
|
89
|
|
|
$this->assertCount(0, $field->first()); |
|
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
public function testLast_AsJson() |
|
|
|
|
|
|
93
|
|
|
{ |
|
94
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
95
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
96
|
|
|
$field->setReturnType('json'); |
|
97
|
|
|
$this->assertEquals('{"6":"morris"}', $field->last()); |
|
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
100
|
|
|
$field->setValue($this->getFixture('empty')); |
|
101
|
|
|
$field->setReturnType('json'); |
|
102
|
|
|
$this->assertInternalType('string', $field->first()); |
|
|
|
|
|
|
103
|
|
|
$this->assertEquals('[]', $field->first()); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function testNth_AsArray() |
|
107
|
|
|
{ |
|
108
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
109
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
110
|
|
|
$field->setReturnType('array'); |
|
111
|
|
|
$this->assertEquals([0 => 'great wall'], $field->nth(0)); |
|
|
|
|
|
|
112
|
|
|
$this->assertEquals([2 => 'trabant'], $field->nth(2)); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
115
|
|
|
$field->setValue($this->getFixture('empty')); |
|
116
|
|
|
$field->setReturnType('array'); |
|
117
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
|
|
118
|
|
|
$this->assertCount(0, $field->first()); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
121
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
122
|
|
|
$field->setValue($this->getFixture('hash_simple')); |
|
123
|
|
|
$field->setReturnType('array'); |
|
124
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->nth('2')); |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function testNth_AsJson() |
|
128
|
|
|
{ |
|
129
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
130
|
|
|
$field->setValue($this->getFixture('array_simple')); |
|
131
|
|
|
$field->setReturnType('json'); |
|
132
|
|
|
$this->assertEquals('["great wall"]', $field->nth(0)); |
|
|
|
|
|
|
133
|
|
|
$this->assertEquals('{"2":"trabant"}', $field->nth(2)); |
|
|
|
|
|
|
134
|
|
|
|
|
135
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
136
|
|
|
$field->setValue($this->getFixture('empty')); |
|
137
|
|
|
$field->setReturnType('json'); |
|
138
|
|
|
$this->assertInternalType('string', $field->first()); |
|
|
|
|
|
|
139
|
|
|
$this->assertEquals('[]', $field->first()); |
|
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
142
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
143
|
|
|
$field->setValue($this->getFixture('hash_simple')); |
|
144
|
|
|
$field->setReturnType('json'); |
|
145
|
|
|
$this->assertEquals('{"british":["vauxhall","morris"]}', $field->nth('2')); |
|
|
|
|
|
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Tests query() by means of the integer Postgres operator: '->' |
|
150
|
|
|
*/ |
|
151
|
|
|
public function testQuery_MatchOnKeyAsInt_AsArray() |
|
152
|
|
|
{ |
|
153
|
|
|
// Hashed |
|
154
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
155
|
|
|
$field->setValue($this->getFixture('hash_simple')); |
|
156
|
|
|
$field->setReturnType('array'); |
|
157
|
|
|
|
|
158
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->', 5)); |
|
|
|
|
|
|
159
|
|
|
$this->assertEquals(['american' => ['buick', 'oldsmobile', 'ford']], $field->query('->', 1)); |
|
|
|
|
|
|
160
|
|
|
$this->assertEquals([], $field->query('->', '6')); // strict handling |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
// Empty |
|
163
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
164
|
|
|
$field->setValue($this->getFixture('empty')); |
|
165
|
|
|
$field->setReturnType('array'); |
|
166
|
|
|
$this->assertEquals([], $field->query('->', 42)); |
|
|
|
|
|
|
167
|
|
|
|
|
168
|
|
|
// Nested |
|
169
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
170
|
|
|
$field->setValue($this->getFixture('hash_deep')); |
|
171
|
|
|
$field->setReturnType('array'); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->', 7)); |
|
|
|
|
|
|
174
|
|
|
$this->assertEquals([], $field->query('->', '7')); // Attempt to match a string using the int matcher |
|
|
|
|
|
|
175
|
|
|
$this->assertEquals([0 => 'buick'], $field->query('->', 2)); |
|
|
|
|
|
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* Tests query() by means of the string Postgres operator: '->>' |
|
180
|
|
|
*/ |
|
181
|
|
|
public function testQuery_MatchOnKeyAsStr_AsArray() |
|
182
|
|
|
{ |
|
183
|
|
|
// Hashed |
|
184
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
185
|
|
|
$field->setValue($this->getFixture('hash_simple')); |
|
186
|
|
|
$field->setReturnType('array'); |
|
187
|
|
|
|
|
188
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->>', 'british')); |
|
|
|
|
|
|
189
|
|
|
$this->assertEquals([], $field->query('->', '6')); // strict handling |
|
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
// Empty |
|
192
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
193
|
|
|
$field->setValue($this->getFixture('empty')); |
|
194
|
|
|
$field->setReturnType('array'); |
|
195
|
|
|
$this->assertEquals([], $field->query('->>', 'british')); |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
// Nested |
|
198
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
199
|
|
|
$field->setValue($this->getFixture('hash_deep')); |
|
200
|
|
|
$field->setReturnType('array'); |
|
201
|
|
|
|
|
202
|
|
|
$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->>', 'planes')); |
|
|
|
|
|
|
203
|
|
|
$this->assertEquals([], $field->query('->', '7')); // Attempt to match a string using the int matcher |
|
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* Tests query() by means of the string Postgres operator: '->>' |
|
208
|
|
|
*/ |
|
209
|
|
|
public function testQuery_MatchOnKeyAsStr_AsJSON() |
|
210
|
|
|
{ |
|
211
|
|
|
// Hashed |
|
212
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
213
|
|
|
$field->setValue($this->getFixture('hash_simple')); |
|
214
|
|
|
$field->setReturnType('json'); |
|
215
|
|
|
|
|
216
|
|
|
$this->assertEquals('{"british":["vauxhall","morris"]}', $field->query('->>', 'british')); |
|
|
|
|
|
|
217
|
|
|
$this->assertEquals('[]', $field->query('->', '6')); // strict handling |
|
|
|
|
|
|
218
|
|
|
|
|
219
|
|
|
// Empty |
|
220
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
221
|
|
|
$field->setValue($this->getFixture('empty')); |
|
222
|
|
|
$field->setReturnType('json'); |
|
223
|
|
|
$this->assertEquals('[]', $field->query('->>', 'british')); |
|
|
|
|
|
|
224
|
|
|
|
|
225
|
|
|
// Nested |
|
226
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
227
|
|
|
$field->setValue($this->getFixture('hash_deep')); |
|
228
|
|
|
$field->setReturnType('json'); |
|
229
|
|
|
|
|
230
|
|
|
$this->assertEquals('{"planes":{"russian":["antonov","mig"],"french":"airbus"}}', $field->query('->>', 'planes')); |
|
|
|
|
|
|
231
|
|
|
$this->assertEquals('[]', $field->query('->', '7')); // Attempt to match a string using the int matcher |
|
|
|
|
|
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
/** |
|
235
|
|
|
* Tests query() by means of path-matching using the Postgres path match operator: '#>' |
|
236
|
|
|
*/ |
|
237
|
|
|
public function testQuery_MatchPath_AsArray() |
|
238
|
|
|
{ |
|
239
|
|
|
// Hashed |
|
240
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
241
|
|
|
$field->setValue($this->getFixture('hash_deep')); |
|
242
|
|
|
$field->setReturnType('array'); |
|
243
|
|
|
|
|
244
|
|
|
$this->assertEquals(["fast" => ["Kawasaki" => "KR1S250"],"slow" => ["Honda" => "FS150"]], $field->query('#>', '{"bikes":"japanese"}')); |
|
|
|
|
|
|
245
|
|
|
|
|
246
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
247
|
|
|
$field->query('#>', '{"bikes","japanese"}'); // Bad JSON |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Tests query() by means of path-matching using the Postgres path match operator: '#>' |
|
252
|
|
|
*/ |
|
253
|
|
|
public function testQuery_MatchPath_AsJSON() |
|
254
|
|
|
{ |
|
255
|
|
|
// Hashed |
|
256
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
257
|
|
|
$field->setValue($this->getFixture('hash_deep')); |
|
258
|
|
|
$field->setReturnType('json'); |
|
259
|
|
|
|
|
260
|
|
|
$this->assertEquals('{"fast":{"Kawasaki":"KR1S250"},"slow":{"Honda":"FS150"}}', $field->query('#>', '{"bikes":"japanese"}')); |
|
|
|
|
|
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Tests query() by means of path-matching using the Postgres path match operator: '#>' but where duplicate keys exist |
|
265
|
|
|
* for different parent structures in source data |
|
266
|
|
|
*/ |
|
267
|
|
|
/* public function testQuery_MatchPathDuplicate_AsArray() |
|
|
|
|
|
|
268
|
|
|
{ |
|
269
|
|
|
// Hashed |
|
270
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
271
|
|
|
$field->setValue($this->getFixture('hash_dupes')); |
|
272
|
|
|
$field->setReturnType('array'); |
|
273
|
|
|
|
|
274
|
|
|
$this->assertEquals([["Kawasaki" => "KR1S250"],["Subaru" => "Impreza"]], $field->query('#>', '{"japanese":"fast"}')); |
|
275
|
|
|
}*/ |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Get the contents of a fixture |
|
279
|
|
|
* |
|
280
|
|
|
* @return string |
|
281
|
|
|
*/ |
|
282
|
|
|
private function getFixture($fixture) |
|
283
|
|
|
{ |
|
284
|
|
|
$files = $this->fixtures; |
|
285
|
|
|
return file_get_contents($files[$fixture]); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
} |
|
289
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.