1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package silverstripe-jsontext |
5
|
|
|
* @subpackage fields |
6
|
|
|
* @author Russell Michell <[email protected]> |
7
|
|
|
* @todo Add tests where source data is a JSON array, not just a JSON object |
8
|
|
|
* |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
use JSONText\Fields\JSONText; |
13
|
|
|
use JSONText\Exceptions; |
14
|
|
|
|
15
|
|
|
class JSONTextQueryTest extends SapphireTest |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected $fixtures = [ |
21
|
|
|
'array' => 'tests/fixtures/json/array.json', |
22
|
|
|
'object' => 'tests/fixtures/json/object.json', |
23
|
|
|
'invalid' => 'tests/fixtures/json/invalid.json' |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \JSONText\Fields\JSONText |
28
|
|
|
*/ |
29
|
|
|
protected $sut; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* JSONTextTest constructor. |
33
|
|
|
* |
34
|
|
|
* Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
View Code Duplication |
foreach($this->fixtures as $name => $path) { |
|
|
|
|
39
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Setup the System Under Test for this test suite. |
45
|
|
|
*/ |
46
|
|
|
public function setUp() |
47
|
|
|
{ |
48
|
|
|
parent::setUp(); |
49
|
|
|
|
50
|
|
|
$this->sut = JSONText::create('MyJSON'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Tests query() by means of the integer Postgres Int match operator: '->' |
55
|
|
|
* |
56
|
|
|
* @todo Use same source data instead of repeating.. |
57
|
|
|
*/ |
58
|
|
|
public function testQueryWithMatchOnInt() |
59
|
|
|
{ |
60
|
|
|
$field = $this->sut; |
61
|
|
|
|
62
|
|
|
// Data Source: Array |
63
|
|
|
// Return Type: ARRAY |
64
|
|
|
// Operator: "->" (Int) |
65
|
|
|
$field->setReturnType('array'); |
66
|
|
|
$field->setValue($this->getFixture('array')); |
67
|
|
|
$this->assertEquals([2 => 'trabant'], $field->query('->', 2)); |
|
|
|
|
68
|
|
|
|
69
|
|
|
// Data Source: Array |
70
|
|
|
// Return Type: JSON |
71
|
|
|
// Operator: "->" (Int) |
72
|
|
|
$field->setReturnType('json'); |
73
|
|
|
$field->setValue($this->getFixture('array')); |
74
|
|
|
$this->assertEquals('{"2":"trabant"}', $field->query('->', 2)); |
|
|
|
|
75
|
|
|
$this->assertEquals('{"5":101}', $field->query('->', 5)); |
|
|
|
|
76
|
|
|
|
77
|
|
|
// Data Source: Array |
78
|
|
|
// Return Type: SILVERSTRIPE |
79
|
|
|
// Operator: "->" (Int) |
80
|
|
|
// SS Type: Float |
81
|
|
|
$field->setReturnType('silverstripe'); |
82
|
|
|
$field->setValue($this->getFixture('array')); |
83
|
|
|
$this->assertInternalType('array', $field->query('->', 3)); |
|
|
|
|
84
|
|
|
$this->assertInstanceOf('Float', $field->query('->', 3)[3]); |
|
|
|
|
85
|
|
|
$this->assertEquals(44.6, $field->query('->', 3)[3]->getValue()); |
|
|
|
|
86
|
|
|
|
87
|
|
|
// Data Source: Array |
88
|
|
|
// Return Type: SILVERSTRIPE |
89
|
|
|
// Operator: "->" (Int) |
90
|
|
|
// SS Type: Boolean |
91
|
|
|
$field->setReturnType('silverstripe'); |
92
|
|
|
$field->setValue($this->getFixture('array')); |
93
|
|
|
$this->assertInternalType('array', $field->query('->', 1)); |
|
|
|
|
94
|
|
|
$this->assertInstanceOf('Boolean', $field->query('->', 1)[1]); |
|
|
|
|
95
|
|
|
$this->assertEquals(1, $field->query('->', 1)[1]->getValue()); |
|
|
|
|
96
|
|
|
|
97
|
|
|
// Data Source: Array |
98
|
|
|
// Return Type: SILVERSTRIPE |
99
|
|
|
// Operator: "->" (Int) |
100
|
|
|
// SS Type: Int |
101
|
|
|
$field->setReturnType('silverstripe'); |
102
|
|
|
$field->setValue($this->getFixture('array')); |
103
|
|
|
$this->assertInternalType('array', $field->query('->', 5)); |
|
|
|
|
104
|
|
|
$this->assertInstanceOf('Int', $field->query('->', 5)[5]); |
|
|
|
|
105
|
|
|
$this->assertEquals(101, $field->query('->', 5)[5]->getValue()); |
|
|
|
|
106
|
|
|
|
107
|
|
|
// Data Source: Array |
108
|
|
|
// Return Type: SILVERSTRIPE |
109
|
|
|
// Operator: "->" (Int) |
110
|
|
|
// SS Type: Varchar |
111
|
|
|
$field->setReturnType('silverstripe'); |
112
|
|
|
$field->setValue($this->getFixture('array')); |
113
|
|
|
$this->assertInternalType('array', $field->query('->', 4)); |
|
|
|
|
114
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->', 4)[4]); |
|
|
|
|
115
|
|
|
$this->assertEquals('buick', $field->query('->', 4)[4]->getValue()); |
|
|
|
|
116
|
|
|
|
117
|
|
|
// Test: Empty #1 |
118
|
|
|
$field->setReturnType('array'); |
119
|
|
|
$field->setValue(''); |
120
|
|
|
$this->assertInternalType('array', $field->query('->', 3)); |
|
|
|
|
121
|
|
|
$this->assertCount(0, $field->query('->', 3)); |
|
|
|
|
122
|
|
|
|
123
|
|
|
// Test: Invalid #1 |
124
|
|
|
$field->setReturnType('array'); |
125
|
|
|
$field->setValue('["morris"]'); |
126
|
|
|
$this->assertEquals([], $field->query('->', 17)); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Tests query() by means of the integer Postgres String match operator: '->>' |
131
|
|
|
*/ |
132
|
|
|
public function testQueryWithMatchOnStr() |
133
|
|
|
{ |
134
|
|
|
$field = $this->sut; |
135
|
|
|
|
136
|
|
|
// Data Source: Object |
137
|
|
|
// Return Type: ARRAY |
138
|
|
|
// Operator: "->>" (String) |
139
|
|
|
$field->setReturnType('array'); |
140
|
|
|
$field->setValue($this->getFixture('object')); |
141
|
|
|
$this->assertEquals(['Subaru' => 'Impreza'], $field->query('->>', 'Subaru')); |
|
|
|
|
142
|
|
|
$this->assertEquals( |
|
|
|
|
143
|
|
|
['japanese' => ['fast' => ['Subaru' => 'Impreza'], 'slow' => ['Honda' => 'Civic']]], |
144
|
|
|
$field->query('->>', 'japanese') |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
// Data Source: Object |
148
|
|
|
// Return Type: JSON |
149
|
|
|
// Operator: "->>" (String) |
150
|
|
|
$field->setReturnType('json'); |
151
|
|
|
$field->setValue($this->getFixture('object')); |
152
|
|
|
$this->assertEquals('{"Subaru":"Impreza"}', $field->query('->>', 'Subaru')); |
|
|
|
|
153
|
|
|
$this->assertEquals( |
|
|
|
|
154
|
|
|
'{"japanese":{"fast":{"Subaru":"Impreza"},"slow":{"Honda":"Civic"}}}', |
155
|
|
|
$field->query('->>', 'japanese') |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
// Data Source: Object |
159
|
|
|
// Return Type: SilverStripe |
160
|
|
|
// Operator: "->>" (String) |
161
|
|
|
// SS Type: Varchar |
162
|
|
|
$field->setReturnType('silverstripe'); |
163
|
|
|
$field->setValue($this->getFixture('object')); |
164
|
|
|
$this->assertInternalType('array', $field->query('->>', 'Subaru')); |
|
|
|
|
165
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'Subaru')['Subaru']); |
|
|
|
|
166
|
|
|
$this->assertEquals('Impreza', $field->query('->>', 'Subaru')['Subaru']->getValue()); |
|
|
|
|
167
|
|
|
|
168
|
|
|
// Data Source: Object |
169
|
|
|
// Return Type: SilverStripe |
170
|
|
|
// Operator: "->>" (String) |
171
|
|
|
// SS Type: Boolean |
172
|
|
|
$field->setReturnType('silverstripe'); |
173
|
|
|
$field->setValue($this->getFixture('object')); |
174
|
|
|
$this->assertInternalType('array', $field->query('->>', 'beer tastes good')); |
|
|
|
|
175
|
|
|
$this->assertInstanceOf('Boolean', $field->query('->>', 'beer tastes good')['beer tastes good']); |
|
|
|
|
176
|
|
|
$this->assertEquals(1, $field->query('->>', 'beer tastes good')['beer tastes good']->getValue()); |
|
|
|
|
177
|
|
|
|
178
|
|
|
// Data Source: Object |
179
|
|
|
// Return Type: SilverStripe |
180
|
|
|
// Operator: "->>" (String) |
181
|
|
|
// SS Type: Float |
182
|
|
|
$field->setReturnType('silverstripe'); |
183
|
|
|
$field->setValue($this->getFixture('object')); |
184
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how sure are you')); |
|
|
|
|
185
|
|
|
$this->assertInstanceOf('Float', $field->query('->>', 'how sure are you')['how sure are you']); |
|
|
|
|
186
|
|
|
$this->assertEquals(99.99, $field->query('->>', 'how sure are you')['how sure are you']->getValue()); |
|
|
|
|
187
|
|
|
|
188
|
|
|
// Data Source: Object |
189
|
|
|
// Return Type: SilverStripe |
190
|
|
|
// Operator: "->>" (String) |
191
|
|
|
// SS Type: Int |
192
|
|
|
$field->setReturnType('silverstripe'); |
193
|
|
|
$field->setValue($this->getFixture('object')); |
194
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how high')); |
|
|
|
|
195
|
|
|
$this->assertInstanceOf('Int', $field->query('->>', 'how high')['how high']); |
|
|
|
|
196
|
|
|
$this->assertEquals(6, $field->query('->>', 'how high')['how high']->getValue()); |
|
|
|
|
197
|
|
|
|
198
|
|
|
// Data Source: Object |
199
|
|
|
// Return Type: SilverStripe |
200
|
|
|
// Operator: "->>" (String) |
201
|
|
|
// SS Type: N/A Nested sub-array example, expect an array |
202
|
|
|
$field->setReturnType('silverstripe'); |
203
|
|
|
$field->setValue($this->getFixture('object')); |
204
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']); |
|
|
|
|
205
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
206
|
|
|
$this->assertCount(2, $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
207
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][0]); |
|
|
|
|
208
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][1]); |
|
|
|
|
209
|
|
|
|
210
|
|
|
// Test: Empty #1 |
211
|
|
|
$field->setReturnType('array'); |
212
|
|
|
$field->setValue(''); |
213
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')); |
|
|
|
|
214
|
|
|
$this->assertCount(0, $field->query('->', 3)); |
|
|
|
|
215
|
|
|
|
216
|
|
|
// Test: Empty #2 |
217
|
|
|
$field->setReturnType('array'); |
218
|
|
|
$field->setValue('["morris"]'); |
219
|
|
|
$this->assertEquals([], $field->query('->', 17)); |
|
|
|
|
220
|
|
|
|
221
|
|
|
// Test: Invalid #1 |
222
|
|
|
$field->setReturnType('array'); |
223
|
|
|
$field->setValue('["trabant"]'); |
224
|
|
|
$this->assertEquals([], $field->query('->', 1)); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Tests query() by means of the Postgres path-match operator: '#>' |
229
|
|
|
*/ |
230
|
|
|
public function testQueryWithMatchOnPath() |
231
|
|
|
{ |
232
|
|
|
$field = $this->sut; |
233
|
|
|
|
234
|
|
|
// Data Source: Object |
235
|
|
|
// Return Type: ARRAY |
236
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
237
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
238
|
|
|
$field->setReturnType('array'); |
239
|
|
|
$field->setValue($this->getFixture('object')); |
240
|
|
|
$this->assertEquals( |
|
|
|
|
241
|
|
|
[['Subaru' => 'Impreza'],['Kawasaki' => 'KR1S250']], |
242
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
// Data Source: Object |
246
|
|
|
// Return Type: JSON |
247
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
248
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
249
|
|
|
$field->setReturnType('json'); |
250
|
|
|
$field->setValue($this->getFixture('object')); |
251
|
|
|
$this->assertEquals( |
|
|
|
|
252
|
|
|
'[{"Subaru":"Impreza"},{"Kawasaki":"KR1S250"}]', |
253
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
254
|
|
|
); |
255
|
|
|
|
256
|
|
|
// Data Source: Object |
257
|
|
|
// Return Type: SILVERSTRIPE |
258
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
259
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
260
|
|
|
$field->setReturnType('silverstripe'); |
261
|
|
|
$field->setValue($this->getFixture('object')); |
262
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
263
|
|
|
$this->assertCount(2, $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
264
|
|
|
|
265
|
|
|
$one = $field->query('#>', '{"japanese":"fast"}')[0]; |
266
|
|
|
$two = $field->query('#>', '{"japanese":"fast"}')[1]; |
267
|
|
|
|
268
|
|
|
$this->assertInternalType('array', $one); |
|
|
|
|
269
|
|
|
$this->assertInternalType('array', $two); |
|
|
|
|
270
|
|
|
$this->assertInstanceOf('Varchar', array_values($one)[0]); |
|
|
|
|
271
|
|
|
$this->assertInstanceOf('Varchar', array_values($two)[0]); |
|
|
|
|
272
|
|
|
$this->assertEquals('Impreza', array_values($one)[0]->getValue()); |
|
|
|
|
273
|
|
|
$this->assertEquals('KR1S250', array_values($two)[0]->getValue()); |
|
|
|
|
274
|
|
|
|
275
|
|
|
// Data Source: Object |
276
|
|
|
// Return Type: ARRAY |
277
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
278
|
|
|
// Expect: Direct scalar comparison assertion |
279
|
|
|
$field->setReturnType('array'); |
280
|
|
|
$field->setValue($this->getFixture('object')); |
281
|
|
|
$this->assertEquals(['airbus'], $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
282
|
|
|
|
283
|
|
|
// Data Source: Object |
284
|
|
|
// Return Type: JSON |
285
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
286
|
|
|
// Expect: Direct scalar comparison assertion |
287
|
|
|
$field->setReturnType('json'); |
288
|
|
|
$field->setValue($this->getFixture('object')); |
289
|
|
|
$this->assertEquals('["airbus"]', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
290
|
|
|
|
291
|
|
|
// Data Source: Object |
292
|
|
|
// Return Type: SILVERSTRIPE |
293
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
294
|
|
|
// Expect: Direct scalar comparison assertion (Varchar) |
295
|
|
|
$field->setReturnType('silverstripe'); |
296
|
|
|
$field->setValue($this->getFixture('object')); |
297
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
298
|
|
|
$this->assertInstanceOf('Varchar', $field->query('#>', '{"planes":"french"}')[0]); |
|
|
|
|
299
|
|
|
$this->assertEquals('airbus', $field->query('#>', '{"planes":"french"}')[0]->getValue()); |
|
|
|
|
300
|
|
|
|
301
|
|
|
// Data Source: Object |
302
|
|
|
// Return Type: SILVERSTRIPE |
303
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
304
|
|
|
// Expect: Direct scalar comparison assertion (Float) |
305
|
|
|
$field->setReturnType('silverstripe'); |
306
|
|
|
$field->setValue($this->getFixture('object')); |
307
|
|
|
|
308
|
|
|
$res = $field->query('#>', '{"floats":"0"}'); |
309
|
|
|
|
310
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
311
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
312
|
|
|
$this->assertInstanceOf('Float', array_values($res[0])[0]); |
|
|
|
|
313
|
|
|
$this->assertEquals(99.99, array_values($res[0])[0]->getValue()); |
|
|
|
|
314
|
|
|
|
315
|
|
|
// Data Source: Object |
316
|
|
|
// Return Type: SILVERSTRIPE |
317
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
318
|
|
|
// Expect: Direct scalar comparison assertion (Int) |
319
|
|
|
$field->setReturnType('silverstripe'); |
320
|
|
|
$field->setValue($this->getFixture('object')); |
321
|
|
|
|
322
|
|
|
$res = $field->query('#>', '{"ints":"0"}'); |
323
|
|
|
|
324
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
325
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
326
|
|
|
$this->assertInstanceOf('Int', array_values($res[0])[1]); |
|
|
|
|
327
|
|
|
$this->assertEquals(6, array_values($res[0])[1]->getValue()); |
|
|
|
|
328
|
|
|
|
329
|
|
|
// Data Source: Object |
330
|
|
|
// Return Type: SILVERSTRIPE |
331
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
332
|
|
|
// Expect: Direct scalar comparison assertion (Boolean) |
333
|
|
|
$field->setReturnType('silverstripe'); |
334
|
|
|
$field->setValue($this->getFixture('object')); |
335
|
|
|
|
336
|
|
|
$res = $field->query('#>', '{"booleans":"0"}'); |
337
|
|
|
|
338
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
339
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "booleans" key is a JSON array |
|
|
|
|
340
|
|
|
$this->assertInstanceOf('Boolean', array_values($res[0])[0]); |
|
|
|
|
341
|
|
|
$this->assertEquals(1, array_values($res[0])[0]->getValue()); |
|
|
|
|
342
|
|
|
|
343
|
|
|
// #1 Empty source data |
344
|
|
|
$field->setReturnType('array'); |
345
|
|
|
$field->setValue(''); |
346
|
|
|
$this->assertEquals([], $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
347
|
|
|
|
348
|
|
|
// #2 JSON path not found |
349
|
|
|
$field->setReturnType('silverstripe'); |
350
|
|
|
$field->setValue($this->getFixture('object')); |
351
|
|
|
$this->assertNull($field->query('#>', '{"ints":"1"}')); // The "ints" key only has a single array as a value |
|
|
|
|
352
|
|
|
|
353
|
|
|
// #3 Invalid operand on RHS |
354
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
355
|
|
|
$field->setReturnType('array'); |
356
|
|
|
$field->setValue($this->getFixture('object')); |
357
|
|
|
$this->assertEquals(['Kawasaki' => 'KR1S250'], $field->query('#>', '{"japanese":"fast"')); |
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Tests query() by means of JSONPath expressions. |
362
|
|
|
* N.b. only a minimum no. tests should be required, given that the 3rd party lib via which this functionality |
363
|
|
|
* is derived, is itself well tested. |
364
|
|
|
*/ |
365
|
|
|
public function testQueryWithMatchOnExpr() |
366
|
|
|
{ |
367
|
|
|
$field = $this->sut; |
368
|
|
|
|
369
|
|
|
// Data Source: Object |
370
|
|
|
// Return Type: ARRAY |
371
|
|
|
// Expression: "$.." (Everything) |
|
|
|
|
372
|
|
|
// Expect: Array, obviously due to no. nodes in the source JSON |
373
|
|
|
$field->setReturnType('array'); |
374
|
|
|
$field->setValue($this->getFixture('object')); |
375
|
|
|
$this->assertInternalType('array', $field->query('$..')); |
|
|
|
|
376
|
|
|
$this->assertCount(25, $field->query('$..')); |
|
|
|
|
377
|
|
|
|
378
|
|
|
// Data Source: Object |
379
|
|
|
// Return Type: ARRAY |
380
|
|
|
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese") |
381
|
|
|
// Expect: Array |
382
|
|
|
$field->setReturnType('array'); |
383
|
|
|
$field->setValue($this->getFixture('object')); |
384
|
|
|
$this->assertCount(4, $field->query('$..japanese[*]')); |
|
|
|
|
385
|
|
|
$this->assertEquals([ |
|
|
|
|
386
|
|
|
['Subaru' => 'Impreza'], |
387
|
|
|
['Honda' => 'Civic'], |
388
|
|
|
['Kawasaki' => 'KR1S250'], |
389
|
|
|
['Honda' => 'FS1'] |
390
|
|
|
], $field->query('$..japanese[*]')); |
391
|
|
|
|
392
|
|
|
// Data Source: Object |
393
|
|
|
// Return Type: JSON |
394
|
|
|
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese") |
395
|
|
|
// Expect: JSON Array of JSON objects |
396
|
|
|
$field->setReturnType('json'); |
397
|
|
|
$field->setValue($this->getFixture('object')); |
398
|
|
|
$this->assertEquals( |
|
|
|
|
399
|
|
|
'[{"Subaru":"Impreza"},{"Honda":"Civic"},{"Kawasaki":"KR1S250"},{"Honda":"FS1"}]', |
400
|
|
|
$field->query('$..japanese[*]') |
401
|
|
|
); |
402
|
|
|
|
403
|
|
|
// Data Source: Object |
404
|
|
|
// Return Type: Array |
405
|
|
|
// Expression: "$.cars.american[*]" (All entries in the american cars node) |
406
|
|
|
// Expect: Array |
407
|
|
|
$field->setReturnType('array'); |
408
|
|
|
$field->setValue($this->getFixture('object')); |
409
|
|
|
$this->assertEquals(['buick', 'oldsmobile'], $field->query('$.cars.american[*]')); |
|
|
|
|
410
|
|
|
$this->assertEquals(['buick'], $field->query('$.cars.american[0]')); |
|
|
|
|
411
|
|
|
|
412
|
|
|
// Data Source: Object |
413
|
|
|
// Return Type: Array |
414
|
|
|
// Expression: "$.cars.american[*]" (All entries in the american cars node) |
415
|
|
|
// Expect: Array 0f SilverStripe types |
416
|
|
|
$field->setReturnType('silverstripe'); |
417
|
|
|
$field->setValue($this->getFixture('object')); |
418
|
|
|
$this->assertInternalType('array', $field->query('$.cars.american[*]')); |
|
|
|
|
419
|
|
|
$this->assertCount(2, $field->query('$.cars.american[*]')); |
|
|
|
|
420
|
|
|
$this->assertInstanceOf('Varchar', $field->query('$.cars.american[*]')[0]); |
|
|
|
|
421
|
|
|
$this->assertEquals('buick', $field->query('$.cars.american[*]')[0]->getValue()); |
|
|
|
|
422
|
|
|
} |
423
|
|
|
|
424
|
|
|
/** |
425
|
|
|
* Tests query() by means of passing bad argument combinations. |
426
|
|
|
*/ |
427
|
|
|
public function testQueryWithInvalidArgs() |
428
|
|
|
{ |
429
|
|
|
$field = $this->sut; |
430
|
|
|
|
431
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
432
|
|
|
$field->setValue('["trabant"]'); |
433
|
|
|
$field->query('$.cars.american[*]', 'foo'); // Cannot pass multiple args when in JSONPath context |
434
|
|
|
|
435
|
|
|
$this->setExpectedException(null); |
|
|
|
|
436
|
|
|
$field->setValue(''); |
437
|
|
|
$field->query('$.cars.american[*]', 'foo'); // Still shouldn't, but routine only kicks in _After_ checks made in setValue() |
438
|
|
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Get the contents of a fixture |
442
|
|
|
* |
443
|
|
|
* @param string $fixture |
444
|
|
|
* @return string |
445
|
|
|
*/ |
446
|
|
|
private function getFixture($fixture) |
447
|
|
|
{ |
448
|
|
|
$files = $this->fixtures; |
449
|
|
|
return file_get_contents($files[$fixture]); |
450
|
|
|
} |
451
|
|
|
|
452
|
|
|
} |
453
|
|
|
|
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.