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