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
|
|
|
// Test: Invalid #2 |
129
|
|
|
$field->setReturnType('array'); |
130
|
|
|
$field->setValue('["ass"]'); |
131
|
|
|
$this->assertEquals(['ass'], $field->query('->', 0)); |
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Tests query() by means of the integer Postgres String match operator: '->>' |
136
|
|
|
*/ |
137
|
|
|
public function testQueryWithMatchOnStr() |
138
|
|
|
{ |
139
|
|
|
$field = $this->sut; |
140
|
|
|
|
141
|
|
|
// Data Source: Object |
142
|
|
|
// Return Type: ARRAY |
143
|
|
|
// Operator: "->>" (String) |
144
|
|
|
$field->setReturnType('array'); |
145
|
|
|
$field->setValue($this->getFixture('object')); |
146
|
|
|
$this->assertEquals(['Subaru' => 'Impreza'], $field->query('->>', 'Subaru')); |
|
|
|
|
147
|
|
|
$this->assertEquals( |
|
|
|
|
148
|
|
|
['japanese' => ['fast' => ['Subaru' => 'Impreza'], 'slow' => ['Honda' => 'Civic']]], |
149
|
|
|
$field->query('->>', 'japanese') |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
// Data Source: Object |
153
|
|
|
// Return Type: JSON |
154
|
|
|
// Operator: "->>" (String) |
155
|
|
|
$field->setReturnType('json'); |
156
|
|
|
$field->setValue($this->getFixture('object')); |
157
|
|
|
$this->assertEquals('{"Subaru":"Impreza"}', $field->query('->>', 'Subaru')); |
|
|
|
|
158
|
|
|
$this->assertEquals( |
|
|
|
|
159
|
|
|
'{"japanese":{"fast":{"Subaru":"Impreza"},"slow":{"Honda":"Civic"}}}', |
160
|
|
|
$field->query('->>', 'japanese') |
161
|
|
|
); |
162
|
|
|
|
163
|
|
|
// Data Source: Object |
164
|
|
|
// Return Type: SilverStripe |
165
|
|
|
// Operator: "->>" (String) |
166
|
|
|
// SS Type: Varchar |
167
|
|
|
$field->setReturnType('silverstripe'); |
168
|
|
|
$field->setValue($this->getFixture('object')); |
169
|
|
|
$this->assertInternalType('array', $field->query('->>', 'Subaru')); |
|
|
|
|
170
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'Subaru')['Subaru']); |
|
|
|
|
171
|
|
|
$this->assertEquals('Impreza', $field->query('->>', 'Subaru')['Subaru']->getValue()); |
|
|
|
|
172
|
|
|
|
173
|
|
|
// Data Source: Object |
174
|
|
|
// Return Type: SilverStripe |
175
|
|
|
// Operator: "->>" (String) |
176
|
|
|
// SS Type: Boolean |
177
|
|
|
$field->setReturnType('silverstripe'); |
178
|
|
|
$field->setValue($this->getFixture('object')); |
179
|
|
|
$this->assertInternalType('array', $field->query('->>', 'beer tastes good')); |
|
|
|
|
180
|
|
|
$this->assertInstanceOf('Boolean', $field->query('->>', 'beer tastes good')['beer tastes good']); |
|
|
|
|
181
|
|
|
$this->assertEquals(1, $field->query('->>', 'beer tastes good')['beer tastes good']->getValue()); |
|
|
|
|
182
|
|
|
|
183
|
|
|
// Data Source: Object |
184
|
|
|
// Return Type: SilverStripe |
185
|
|
|
// Operator: "->>" (String) |
186
|
|
|
// SS Type: Float |
187
|
|
|
$field->setReturnType('silverstripe'); |
188
|
|
|
$field->setValue($this->getFixture('object')); |
189
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how sure are you')); |
|
|
|
|
190
|
|
|
$this->assertInstanceOf('Float', $field->query('->>', 'how sure are you')['how sure are you']); |
|
|
|
|
191
|
|
|
$this->assertEquals(99.99, $field->query('->>', 'how sure are you')['how sure are you']->getValue()); |
|
|
|
|
192
|
|
|
|
193
|
|
|
// Data Source: Object |
194
|
|
|
// Return Type: SilverStripe |
195
|
|
|
// Operator: "->>" (String) |
196
|
|
|
// SS Type: Int |
197
|
|
|
$field->setReturnType('silverstripe'); |
198
|
|
|
$field->setValue($this->getFixture('object')); |
199
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how high')); |
|
|
|
|
200
|
|
|
$this->assertInstanceOf('Int', $field->query('->>', 'how high')['how high']); |
|
|
|
|
201
|
|
|
$this->assertEquals(6, $field->query('->>', 'how high')['how high']->getValue()); |
|
|
|
|
202
|
|
|
|
203
|
|
|
// Data Source: Object |
204
|
|
|
// Return Type: SilverStripe |
205
|
|
|
// Operator: "->>" (String) |
206
|
|
|
// SS Type: N/A Nested sub-array example, expect an array |
207
|
|
|
$field->setReturnType('silverstripe'); |
208
|
|
|
$field->setValue($this->getFixture('object')); |
209
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']); |
|
|
|
|
210
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
211
|
|
|
$this->assertCount(2, $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
212
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][0]); |
|
|
|
|
213
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][1]); |
|
|
|
|
214
|
|
|
|
215
|
|
|
// Test: Empty #1 |
216
|
|
|
$field->setReturnType('array'); |
217
|
|
|
$field->setValue(''); |
218
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')); |
|
|
|
|
219
|
|
|
$this->assertCount(0, $field->query('->', 3)); |
|
|
|
|
220
|
|
|
|
221
|
|
|
// Test: Empty #2 |
222
|
|
|
$field->setReturnType('array'); |
223
|
|
|
$field->setValue('["morris"]'); |
224
|
|
|
$this->assertEquals([], $field->query('->', 17)); |
|
|
|
|
225
|
|
|
|
226
|
|
|
// Test: Invalid #1 |
227
|
|
|
$field->setReturnType('array'); |
228
|
|
|
$field->setValue('["trabant"]'); |
229
|
|
|
$this->assertEquals([], $field->query('->', 1)); |
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Tests query() by means of the Postgres path-match operator: '#>' |
234
|
|
|
*/ |
235
|
|
|
public function testQueryWithMatchOnPath() |
236
|
|
|
{ |
237
|
|
|
$field = $this->sut; |
238
|
|
|
|
239
|
|
|
// Data Source: Object |
240
|
|
|
// Return Type: ARRAY |
241
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
242
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
243
|
|
|
$field->setReturnType('array'); |
244
|
|
|
$field->setValue($this->getFixture('object')); |
245
|
|
|
$this->assertEquals( |
|
|
|
|
246
|
|
|
[['Subaru' => 'Impreza'],['Kawasaki' => 'KR1S250']], |
247
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
// Data Source: Object |
251
|
|
|
// Return Type: JSON |
252
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
253
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
254
|
|
|
$field->setReturnType('json'); |
255
|
|
|
$field->setValue($this->getFixture('object')); |
256
|
|
|
$this->assertEquals( |
|
|
|
|
257
|
|
|
'[{"Subaru":"Impreza"},{"Kawasaki":"KR1S250"}]', |
258
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
// Data Source: Object |
262
|
|
|
// Return Type: SILVERSTRIPE |
263
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
264
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
265
|
|
|
$field->setReturnType('silverstripe'); |
266
|
|
|
$field->setValue($this->getFixture('object')); |
267
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
268
|
|
|
$this->assertCount(2, $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
269
|
|
|
|
270
|
|
|
$one = $field->query('#>', '{"japanese":"fast"}')[0]; |
271
|
|
|
$two = $field->query('#>', '{"japanese":"fast"}')[1]; |
272
|
|
|
|
273
|
|
|
$this->assertInternalType('array', $one); |
|
|
|
|
274
|
|
|
$this->assertInternalType('array', $two); |
|
|
|
|
275
|
|
|
$this->assertInstanceOf('Varchar', array_values($one)[0]); |
|
|
|
|
276
|
|
|
$this->assertInstanceOf('Varchar', array_values($two)[0]); |
|
|
|
|
277
|
|
|
$this->assertEquals('Impreza', array_values($one)[0]->getValue()); |
|
|
|
|
278
|
|
|
$this->assertEquals('KR1S250', array_values($two)[0]->getValue()); |
|
|
|
|
279
|
|
|
|
280
|
|
|
// Data Source: Object |
281
|
|
|
// Return Type: ARRAY |
282
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
283
|
|
|
// Expect: Direct scalar comparison assertion |
284
|
|
|
$field->setReturnType('array'); |
285
|
|
|
$field->setValue($this->getFixture('object')); |
286
|
|
|
$this->assertEquals(['airbus'], $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
287
|
|
|
|
288
|
|
|
// Data Source: Object |
289
|
|
|
// Return Type: JSON |
290
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
291
|
|
|
// Expect: Direct scalar comparison assertion |
292
|
|
|
$field->setReturnType('json'); |
293
|
|
|
$field->setValue($this->getFixture('object')); |
294
|
|
|
$this->assertEquals('["airbus"]', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
295
|
|
|
|
296
|
|
|
// Data Source: Object |
297
|
|
|
// Return Type: SILVERSTRIPE |
298
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
299
|
|
|
// Expect: Direct scalar comparison assertion (Varchar) |
300
|
|
|
$field->setReturnType('silverstripe'); |
301
|
|
|
$field->setValue($this->getFixture('object')); |
302
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
303
|
|
|
$this->assertInstanceOf('Varchar', $field->query('#>', '{"planes":"french"}')[0]); |
|
|
|
|
304
|
|
|
$this->assertEquals('airbus', $field->query('#>', '{"planes":"french"}')[0]->getValue()); |
|
|
|
|
305
|
|
|
|
306
|
|
|
// Data Source: Object |
307
|
|
|
// Return Type: SILVERSTRIPE |
308
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
309
|
|
|
// Expect: Direct scalar comparison assertion (Float) |
310
|
|
|
$field->setReturnType('silverstripe'); |
311
|
|
|
$field->setValue($this->getFixture('object')); |
312
|
|
|
|
313
|
|
|
$res = $field->query('#>', '{"floats":"0"}'); |
314
|
|
|
|
315
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
316
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
317
|
|
|
$this->assertInstanceOf('Float', array_values($res[0])[0]); |
|
|
|
|
318
|
|
|
$this->assertEquals(99.99, array_values($res[0])[0]->getValue()); |
|
|
|
|
319
|
|
|
|
320
|
|
|
// Data Source: Object |
321
|
|
|
// Return Type: SILVERSTRIPE |
322
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
323
|
|
|
// Expect: Direct scalar comparison assertion (Int) |
324
|
|
|
$field->setReturnType('silverstripe'); |
325
|
|
|
$field->setValue($this->getFixture('object')); |
326
|
|
|
|
327
|
|
|
$res = $field->query('#>', '{"ints":"0"}'); |
328
|
|
|
|
329
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
330
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
331
|
|
|
$this->assertInstanceOf('Int', array_values($res[0])[1]); |
|
|
|
|
332
|
|
|
$this->assertEquals(6, array_values($res[0])[1]->getValue()); |
|
|
|
|
333
|
|
|
|
334
|
|
|
// Data Source: Object |
335
|
|
|
// Return Type: SILVERSTRIPE |
336
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
337
|
|
|
// Expect: Direct scalar comparison assertion (Boolean) |
338
|
|
|
$field->setReturnType('silverstripe'); |
339
|
|
|
$field->setValue($this->getFixture('object')); |
340
|
|
|
|
341
|
|
|
$res = $field->query('#>', '{"booleans":"0"}'); |
342
|
|
|
|
343
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
344
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "booleans" key is a JSON array |
|
|
|
|
345
|
|
|
$this->assertInstanceOf('Boolean', array_values($res[0])[0]); |
|
|
|
|
346
|
|
|
$this->assertEquals(1, array_values($res[0])[0]->getValue()); |
|
|
|
|
347
|
|
|
|
348
|
|
|
// #1 Empty source data |
349
|
|
|
$field->setReturnType('array'); |
350
|
|
|
$field->setValue(''); |
351
|
|
|
$this->assertEquals([], $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
352
|
|
|
|
353
|
|
|
// #2 JSON path not found |
354
|
|
|
$field->setReturnType('silverstripe'); |
355
|
|
|
$field->setValue($this->getFixture('object')); |
356
|
|
|
$this->assertNull($field->query('#>', '{"ints":"1"}')); // The "ints" key only has a single array as a value |
|
|
|
|
357
|
|
|
|
358
|
|
|
// #3 Invalid operand on RHS |
359
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
360
|
|
|
$field->setReturnType('array'); |
361
|
|
|
$field->setValue($this->getFixture('object')); |
362
|
|
|
$this->assertEquals(['Kawasaki' => 'KR1S250'], $field->query('#>', '{"japanese":"fast"')); |
|
|
|
|
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
/** |
366
|
|
|
* Tests query() by means of JSONPath expressions. |
367
|
|
|
* N.b. only a minimum no. tests should be required, given that the 3rd party lib via which this functionality |
368
|
|
|
* is derived, is itself well tested. |
369
|
|
|
*/ |
370
|
|
|
public function testQueryWithMatchOnExpr() |
371
|
|
|
{ |
372
|
|
|
$field = $this->sut; |
373
|
|
|
|
374
|
|
|
// Data Source: Object |
375
|
|
|
// Return Type: ARRAY |
376
|
|
|
// Expression: "$.." (Everything) |
|
|
|
|
377
|
|
|
// Expect: Array, obviously due to no. nodes in the source JSON |
378
|
|
|
$field->setReturnType('array'); |
379
|
|
|
$field->setValue($this->getFixture('object')); |
380
|
|
|
$this->assertInternalType('array', $field->query('$..')); |
|
|
|
|
381
|
|
|
$this->assertCount(25, $field->query('$..')); |
|
|
|
|
382
|
|
|
|
383
|
|
|
// Data Source: Object |
384
|
|
|
// Return Type: ARRAY |
385
|
|
|
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese") |
386
|
|
|
// Expect: Array |
387
|
|
|
$field->setReturnType('array'); |
388
|
|
|
$field->setValue($this->getFixture('object')); |
389
|
|
|
$this->assertCount(4, $field->query('$..japanese[*]')); |
|
|
|
|
390
|
|
|
$this->assertEquals([ |
|
|
|
|
391
|
|
|
['Subaru' => 'Impreza'], |
392
|
|
|
['Honda' => 'Civic'], |
393
|
|
|
['Kawasaki' => 'KR1S250'], |
394
|
|
|
['Honda' => 'FS1'] |
395
|
|
|
], $field->query('$..japanese[*]')); |
396
|
|
|
|
397
|
|
|
// Data Source: Object |
398
|
|
|
// Return Type: JSON |
399
|
|
|
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese") |
400
|
|
|
// Expect: JSON Array of JSON objects |
401
|
|
|
$field->setReturnType('json'); |
402
|
|
|
$field->setValue($this->getFixture('object')); |
403
|
|
|
$this->assertEquals( |
|
|
|
|
404
|
|
|
'[{"Subaru":"Impreza"},{"Honda":"Civic"},{"Kawasaki":"KR1S250"},{"Honda":"FS1"}]', |
405
|
|
|
$field->query('$..japanese[*]') |
406
|
|
|
); |
407
|
|
|
|
408
|
|
|
// Data Source: Object |
409
|
|
|
// Return Type: Array |
410
|
|
|
// Expression: "$.cars.american[*]" (All entries in the american cars node) |
411
|
|
|
// Expect: Array |
412
|
|
|
$field->setReturnType('array'); |
413
|
|
|
$field->setValue($this->getFixture('object')); |
414
|
|
|
$this->assertEquals(['buick', 'oldsmobile'], $field->query('$.cars.american[*]')); |
|
|
|
|
415
|
|
|
$this->assertEquals(['buick'], $field->query('$.cars.american[0]')); |
|
|
|
|
416
|
|
|
|
417
|
|
|
// Data Source: Object |
418
|
|
|
// Return Type: Array |
419
|
|
|
// Expression: "$.cars.american[*]" (All entries in the american cars node) |
420
|
|
|
// Expect: Array 0f SilverStripe types |
421
|
|
|
$field->setReturnType('silverstripe'); |
422
|
|
|
$field->setValue($this->getFixture('object')); |
423
|
|
|
$this->assertInternalType('array', $field->query('$.cars.american[*]')); |
|
|
|
|
424
|
|
|
$this->assertCount(2, $field->query('$.cars.american[*]')); |
|
|
|
|
425
|
|
|
$this->assertInstanceOf('Varchar', $field->query('$.cars.american[*]')[0]); |
|
|
|
|
426
|
|
|
$this->assertEquals('buick', $field->query('$.cars.american[*]')[0]->getValue()); |
|
|
|
|
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
/** |
430
|
|
|
* Get the contents of a fixture |
431
|
|
|
* |
432
|
|
|
* @param string $fixture |
433
|
|
|
* @return string |
434
|
|
|
*/ |
435
|
|
|
private function getFixture($fixture) |
436
|
|
|
{ |
437
|
|
|
$files = $this->fixtures; |
438
|
|
|
return file_get_contents($files[$fixture]); |
439
|
|
|
} |
440
|
|
|
|
441
|
|
|
} |
442
|
|
|
|
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.