-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathJSONTextQueryTest.php
executable file
·458 lines (398 loc) · 17.9 KB
/
JSONTextQueryTest.php
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* @package silverstripe-jsontext
* @subpackage fields
* @author Russell Michell <[email protected]>
* @todo Add tests where source data is a JSON array, not just a JSON object
*
*
*/
use PhpTek\JSONText\ORM\FieldType\JSONText;
use PhpTek\JSONText\Exception\JSONTextException;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\ORM\FieldType\DBBoolean;
use SilverStripe\ORM\FieldType\DBFloat;
use SilverStripe\ORM\FieldType\DBVarchar;
use SilverStripe\ORM\FieldType\DBInt;
class JSONTextQueryTest extends SapphireTest
{
/**
* @var array
*/
protected $fixtures = [
'array' => 'fixtures/json/array.json',
'object' => 'fixtures/json/object.json',
];
/**
* @var JSONText
*/
protected $sut;
/**
* JSONTextTest constructor.
*
* Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows
*/
public function __construct()
{
foreach($this->fixtures as $name => $path) {
$this->fixtures[$name] = realpath(__DIR__) . '/' . $path;
}
parent::__construct();
}
/**
* Setup the System Under Test for this test suite.
*/
public function setUp()
{
parent::setUp();
$this->sut = JSONText::create('MyJSON');
}
/**
* Tests query() by means of the integer Postgres Int match operator: '->'
*
* @todo Use same source data instead of repeating..
*/
public function testQueryWithMatchOnInt()
{
$field = $this->sut;
// Data Source: Array
// Return Type: ARRAY
// Operator: "->" (Int)
$field->setReturnType('array');
$field->setValue($this->getFixture('array'));
$this->assertEquals([2 => 'trabant'], $field->query('->', 2));
// Data Source: Array
// Return Type: JSON
// Operator: "->" (Int)
$field->setReturnType('json');
$field->setValue($this->getFixture('array'));
$this->assertEquals('{"2":"trabant"}', $field->query('->', 2));
$this->assertEquals('{"5":101}', $field->query('->', 5));
// Data Source: Array
// Return Type: SILVERSTRIPE
// Operator: "->" (Int)
// SS Type: Float
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('array'));
$this->assertInternalType('array', $field->query('->', 3));
$this->assertInstanceOf(DBFloat::class, $field->query('->', 3)[3]);
$this->assertEquals(44.6, $field->query('->', 3)[3]->getValue());
// Data Source: Array
// Return Type: SILVERSTRIPE
// Operator: "->" (Int)
// SS Type: Boolean
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('array'));
$this->assertInternalType('array', $field->query('->', 1));
$this->assertInstanceOf(DBBoolean::class, $field->query('->', 1)[1]);
$this->assertEquals(1, $field->query('->', 1)[1]->getValue());
// Data Source: Array
// Return Type: SILVERSTRIPE
// Operator: "->" (Int)
// SS Type: Int
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('array'));
$this->assertInternalType('array', $field->query('->', 5));
$this->assertInstanceOf(DBInt::class, $field->query('->', 5)[5]);
$this->assertEquals(101, $field->query('->', 5)[5]->getValue());
// Data Source: Array
// Return Type: SILVERSTRIPE
// Operator: "->" (Int)
// SS Type: Varchar
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('array'));
$this->assertInternalType('array', $field->query('->', 4));
$this->assertInstanceOf(DBVarchar::class, $field->query('->', 4)[4]);
$this->assertEquals('buick', $field->query('->', 4)[4]->getValue());
// Test: Empty #1
$field->setReturnType('array');
$field->setValue('');
$this->assertInternalType('array', $field->query('->', 3));
$this->assertCount(0, $field->query('->', 3));
// Test: Invalid #1
$field->setReturnType('array');
$field->setValue('["morris"]');
$this->assertEquals([], $field->query('->', 17));
}
/**
* Tests query() by means of the integer Postgres String match operator: '->>'
*/
public function testQueryWithMatchOnStr()
{
$field = $this->sut;
// Data Source: Object
// Return Type: ARRAY
// Operator: "->>" (String)
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertEquals(['Subaru' => 'Impreza'], $field->query('->>', 'Subaru'));
$this->assertEquals(
['japanese' => ['fast' => ['Subaru' => 'Impreza'], 'slow' => ['Honda' => 'Civic']]],
$field->query('->>', 'japanese')
);
// Data Source: Object
// Return Type: JSON
// Operator: "->>" (String)
$field->setReturnType('json');
$field->setValue($this->getFixture('object'));
$this->assertEquals('{"Subaru":"Impreza"}', $field->query('->>', 'Subaru'));
$this->assertEquals(
'{"japanese":{"fast":{"Subaru":"Impreza"},"slow":{"Honda":"Civic"}}}',
$field->query('->>', 'japanese')
);
// Data Source: Object
// Return Type: SilverStripe
// Operator: "->>" (String)
// SS Type: Varchar
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('->>', 'Subaru'));
$this->assertInstanceOf(DBVarchar::class, $field->query('->>', 'Subaru')['Subaru']);
$this->assertEquals('Impreza', $field->query('->>', 'Subaru')['Subaru']->getValue());
// Data Source: Object
// Return Type: SilverStripe
// Operator: "->>" (String)
// SS Type: Boolean
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('->>', 'beer tastes good'));
$this->assertInstanceOf(DBBoolean::class, $field->query('->>', 'beer tastes good')['beer tastes good']);
$this->assertEquals(1, $field->query('->>', 'beer tastes good')['beer tastes good']->getValue());
// Data Source: Object
// Return Type: SilverStripe
// Operator: "->>" (String)
// SS Type: Float
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('->>', 'how sure are you'));
$this->assertInstanceOf(DBFloat::class, $field->query('->>', 'how sure are you')['how sure are you']);
$this->assertEquals(99.99, $field->query('->>', 'how sure are you')['how sure are you']->getValue());
// Data Source: Object
// Return Type: SilverStripe
// Operator: "->>" (String)
// SS Type: Int
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('->>', 'how high'));
$this->assertInstanceOf(DBInt::class, $field->query('->>', 'how high')['how high']);
$this->assertEquals(6, $field->query('->>', 'how high')['how high']->getValue());
// Data Source: Object
// Return Type: SilverStripe
// Operator: "->>" (String)
// SS Type: N/A Nested sub-array example, expect an array
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']);
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']['russian']);
$this->assertCount(2, $field->query('->>', 'planes')['planes']['russian']);
$this->assertInstanceOf(DBVarchar::class, $field->query('->>', 'planes')['planes']['russian'][0]);
$this->assertInstanceOf(DBVarchar::class, $field->query('->>', 'planes')['planes']['russian'][1]);
// Test: Empty #1
$field->setReturnType('array');
$field->setValue('');
$this->assertInternalType('array', $field->query('->>', 'planes'));
$this->assertCount(0, $field->query('->', 3));
// Test: Empty #2
$field->setReturnType('array');
$field->setValue('["morris"]');
$this->assertEquals([], $field->query('->', 17));
// Test: Invalid #1
$field->setReturnType('array');
$field->setValue('["trabant"]');
$this->assertEquals([], $field->query('->', 1));
}
/**
* Tests query() by means of the Postgres path-match operator: '#>'
*/
public function testQueryWithMatchOnPath()
{
$field = $this->sut;
// Data Source: Object
// Return Type: ARRAY
// Operator: "#>" (Path)
// Expect: Array due to duplicate keys in different parts of the source data
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertEquals(
[['Subaru' => 'Impreza'],['Kawasaki' => 'KR1S250']],
$field->query('#>', '{"japanese":"fast"}')
);
// Data Source: Object
// Return Type: JSON
// Operator: "#>" (Path)
// Expect: Array due to duplicate keys in different parts of the source data
$field->setReturnType('json');
$field->setValue($this->getFixture('object'));
$this->assertEquals(
'[{"Subaru":"Impreza"},{"Kawasaki":"KR1S250"}]',
$field->query('#>', '{"japanese":"fast"}')
);
// Data Source: Object
// Return Type: SILVERSTRIPE
// Operator: "#>" (Path)
// Expect: Array due to duplicate keys in different parts of the source data
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('#>', '{"japanese":"fast"}'));
$this->assertCount(2, $field->query('#>', '{"japanese":"fast"}'));
$one = $field->query('#>', '{"japanese":"fast"}')[0];
$two = $field->query('#>', '{"japanese":"fast"}')[1];
$this->assertInternalType('array', $one);
$this->assertInternalType('array', $two);
$this->assertInstanceOf(DBVarchar::class, array_values($one)[0]);
$this->assertInstanceOf(DBVarchar::class, array_values($two)[0]);
$this->assertEquals('Impreza', array_values($one)[0]->getValue());
$this->assertEquals('KR1S250', array_values($two)[0]->getValue());
// Data Source: Object
// Return Type: ARRAY
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertEquals(['airbus'], $field->query('#>', '{"planes":"french"}'));
// Data Source: Object
// Return Type: JSON
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion
$field->setReturnType('json');
$field->setValue($this->getFixture('object'));
$this->assertEquals('["airbus"]', $field->query('#>', '{"planes":"french"}'));
// Data Source: Object
// Return Type: SILVERSTRIPE
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion (Varchar)
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('#>', '{"planes":"french"}'));
$this->assertInstanceOf(DBVarchar::class, $field->query('#>', '{"planes":"french"}')[0]);
$this->assertEquals('airbus', $field->query('#>', '{"planes":"french"}')[0]->getValue());
// Data Source: Object
// Return Type: SILVERSTRIPE
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion (Float)
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$res = $field->query('#>', '{"floats":"0"}');
$this->assertInternalType('array', $res);
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array
$this->assertInstanceOf(DBFloat::class, array_values($res[0])[0]);
$this->assertEquals(99.99, array_values($res[0])[0]->getValue());
// Data Source: Object
// Return Type: SILVERSTRIPE
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion (Int)
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$res = $field->query('#>', '{"ints":"0"}');
$this->assertInternalType('array', $res);
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array
$this->assertInstanceOf(DBInt::class, array_values($res[0])[1]);
$this->assertEquals(6, array_values($res[0])[1]->getValue());
// Data Source: Object
// Return Type: SILVERSTRIPE
// Operator: "#>" (Path)
// Expect: Direct scalar comparison assertion (Boolean)
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$res = $field->query('#>', '{"booleans":"0"}');
$this->assertInternalType('array', $res);
$this->assertInternalType('array', $res[0]); // Why? Because value of "booleans" key is a JSON array
$this->assertInstanceOf(DBBoolean::class, array_values($res[0])[0]);
$this->assertEquals(1, array_values($res[0])[0]->getValue());
// #1 Empty source data
$field->setReturnType('array');
$field->setValue('');
$this->assertEquals([], $field->query('#>', '{"japanese":"fast"}'));
// #2 JSON path not found
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertNull($field->query('#>', '{"ints":"1"}')); // The "ints" key only has a single array as a value
// #3 Invalid operand on RHS
$this->setExpectedException(JSONTextException::class);
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertEquals(['Kawasaki' => 'KR1S250'], $field->query('#>', '{"japanese":"fast"'));
}
/**
* Tests query() by means of JSONPath expressions.
* N.b. only a minimum no. tests should be required, given that the 3rd party lib via which this functionality
* is derived, is itself well tested.
*/
public function testQueryWithMatchOnExpr()
{
$field = $this->sut;
// Data Source: Object
// Return Type: ARRAY
// Expression: "$.." (Everything)
// Expect: Array, obviously due to no. nodes in the source JSON
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('$..'));
$this->assertCount(25, $field->query('$..'));
// Data Source: Object
// Return Type: ARRAY
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese")
// Expect: Array
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertCount(4, $field->query('$..japanese[*]'));
$this->assertEquals([
['Subaru' => 'Impreza'],
['Honda' => 'Civic'],
['Kawasaki' => 'KR1S250'],
['Honda' => 'FS1']
], $field->query('$..japanese[*]'));
// Data Source: Object
// Return Type: JSON
// Expression: "$..japanese[*]" (An array of children of all keys matching "japanese")
// Expect: JSON Array of JSON objects
$field->setReturnType('json');
$field->setValue($this->getFixture('object'));
$this->assertEquals(
'[{"Subaru":"Impreza"},{"Honda":"Civic"},{"Kawasaki":"KR1S250"},{"Honda":"FS1"}]',
$field->query('$..japanese[*]')
);
// Data Source: Object
// Return Type: Array
// Expression: "$.cars.american[*]" (All entries in the american cars node)
// Expect: Array
$field->setReturnType('array');
$field->setValue($this->getFixture('object'));
$this->assertEquals(['buick', 'oldsmobile'], $field->query('$.cars.american[*]'));
$this->assertEquals(['buick'], $field->query('$.cars.american[0]'));
// Data Source: Object
// Return Type: Array
// Expression: "$.cars.american[*]" (All entries in the american cars node)
// Expect: Array 0f SilverStripe types
$field->setReturnType('silverstripe');
$field->setValue($this->getFixture('object'));
$this->assertInternalType('array', $field->query('$.cars.american[*]'));
$this->assertCount(2, $field->query('$.cars.american[*]'));
$this->assertInstanceOf(DBVarchar::class, $field->query('$.cars.american[*]')[0]);
$this->assertEquals('buick', $field->query('$.cars.american[*]')[0]->getValue());
}
/**
* Tests query() by means of passing bad argument combinations.
*/
public function testQueryWithInvalidArgs()
{
$field = $this->sut;
$this->setExpectedException(JSONTextException::class);
$field->setValue('["trabant"]');
$field->query('$.cars.american[*]', 'foo'); // Cannot pass multiple args when in JSONPath context
$this->setExpectedException(null);
$field->setValue('');
$field->query('$.cars.american[*]', 'foo'); // Still shouldn't, but routine only kicks in _After_ checks made in setValue()
}
/**
* Get the contents of a fixture
*
* @param string $fixture
* @return string
*/
private function getFixture($fixture)
{
$files = $this->fixtures;
return file_get_contents($files[$fixture]);
}
}