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
|
|
|
* @todo Split tests into discreet types |
9
|
|
|
* @todo $this->clearFixtures(); in setUp() |
10
|
|
|
* @todo Use same source data instead of repeating for each test |
11
|
|
|
* @todo See the PHPUnit @dataProvider annotaton ala |
12
|
|
|
* |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @dataProvider additionProvider |
18
|
|
|
*/ |
19
|
|
|
/*public function testAdd($a, $b, $expected) |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$this->assertEquals($expected, $a + $b); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function additionProvider() |
25
|
|
|
{ |
26
|
|
|
return [ |
27
|
|
|
[0, 0, 0], |
28
|
|
|
[0, 1, 1], |
29
|
|
|
[1, 0, 1], |
30
|
|
|
[1, 1, 3] |
31
|
|
|
]; |
32
|
|
|
}*/ |
33
|
|
|
|
34
|
|
|
use JSONText\Fields; |
35
|
|
|
use JSONText\Exceptions; |
36
|
|
|
|
37
|
|
|
class JSONTextQueryTest extends SapphireTest |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $fixtures = [ |
43
|
|
|
'array' => 'tests/fixtures/json/array.json', |
44
|
|
|
'object' => 'tests/fixtures/json/object.json', |
45
|
|
|
'invalid' => 'tests/fixtures/json/invalid.json' |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* JSONTextTest constructor. |
50
|
|
|
* |
51
|
|
|
* Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
52
|
|
|
*/ |
53
|
|
|
public function __construct() |
54
|
|
|
{ |
55
|
|
View Code Duplication |
foreach($this->fixtures as $name => $path) { |
|
|
|
|
56
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Tests query() by means of the integer Postgres Int match operator: '->' |
62
|
|
|
* |
63
|
|
|
* @todo Use same source data instead of repeating.. |
64
|
|
|
*/ |
65
|
|
|
public function testQueryWithMatchOnInt() |
66
|
|
|
{ |
67
|
|
|
// Data Source: Array |
68
|
|
|
// Return Type: ARRAY |
69
|
|
|
// Operator: "->" (Int) |
70
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
71
|
|
|
$field->setReturnType('array'); |
72
|
|
|
$field->setValue($this->getFixture('array')); |
73
|
|
|
$this->assertEquals([2 => 'trabant'], $field->query('->', 2)); |
|
|
|
|
74
|
|
|
|
75
|
|
|
// Data Source: Array |
76
|
|
|
// Return Type: JSON |
77
|
|
|
// Operator: "->" (Int) |
78
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
79
|
|
|
$field->setReturnType('json'); |
80
|
|
|
$field->setValue($this->getFixture('array')); |
81
|
|
|
$this->assertEquals('{"2":"trabant"}', $field->query('->', 2)); |
|
|
|
|
82
|
|
|
$this->assertEquals('{"5":101}', $field->query('->', 5)); |
|
|
|
|
83
|
|
|
|
84
|
|
|
// Data Source: Array |
85
|
|
|
// Return Type: SILVERSTRIPE |
86
|
|
|
// Operator: "->" (Int) |
87
|
|
|
// SS Type: Float |
88
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
89
|
|
|
$field->setReturnType('silverstripe'); |
90
|
|
|
$field->setValue($this->getFixture('array')); |
91
|
|
|
$this->assertInternalType('array', $field->query('->', 3)); |
|
|
|
|
92
|
|
|
$this->assertInstanceOf('Float', $field->query('->', 3)[3]); |
|
|
|
|
93
|
|
|
$this->assertEquals(44.6, $field->query('->', 3)[3]->getValue()); |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Data Source: Array |
96
|
|
|
// Return Type: SILVERSTRIPE |
97
|
|
|
// Operator: "->" (Int) |
98
|
|
|
// SS Type: Boolean |
99
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
100
|
|
|
$field->setReturnType('silverstripe'); |
101
|
|
|
$field->setValue($this->getFixture('array')); |
102
|
|
|
$this->assertInternalType('array', $field->query('->', 1)); |
|
|
|
|
103
|
|
|
$this->assertInstanceOf('Boolean', $field->query('->', 1)[1]); |
|
|
|
|
104
|
|
|
$this->assertEquals(1, $field->query('->', 1)[1]->getValue()); |
|
|
|
|
105
|
|
|
|
106
|
|
|
// Data Source: Array |
107
|
|
|
// Return Type: SILVERSTRIPE |
108
|
|
|
// Operator: "->" (Int) |
109
|
|
|
// SS Type: Int |
110
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
111
|
|
|
$field->setReturnType('silverstripe'); |
112
|
|
|
$field->setValue($this->getFixture('array')); |
113
|
|
|
$this->assertInternalType('array', $field->query('->', 5)); |
|
|
|
|
114
|
|
|
$this->assertInstanceOf('Int', $field->query('->', 5)[5]); |
|
|
|
|
115
|
|
|
$this->assertEquals(101, $field->query('->', 5)[5]->getValue()); |
|
|
|
|
116
|
|
|
|
117
|
|
|
// Data Source: Array |
118
|
|
|
// Return Type: SILVERSTRIPE |
119
|
|
|
// Operator: "->" (Int) |
120
|
|
|
// SS Type: Varchar |
121
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
122
|
|
|
$field->setReturnType('silverstripe'); |
123
|
|
|
$field->setValue($this->getFixture('array')); |
124
|
|
|
$this->assertInternalType('array', $field->query('->', 4)); |
|
|
|
|
125
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->', 4)[4]); |
|
|
|
|
126
|
|
|
$this->assertEquals('buick', $field->query('->', 4)[4]->getValue()); |
|
|
|
|
127
|
|
|
|
128
|
|
|
// Test: Empty #1 |
129
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
130
|
|
|
$field->setReturnType('array'); |
131
|
|
|
$field->setValue(''); |
132
|
|
|
$this->assertInternalType('array', $field->query('->', 3)); |
|
|
|
|
133
|
|
|
$this->assertCount(0, $field->query('->', 3)); |
|
|
|
|
134
|
|
|
|
135
|
|
|
// Test: Empty #2 |
136
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
137
|
|
|
$field->setReturnType('array'); |
138
|
|
|
$field->setValue('["morris"]'); |
139
|
|
|
$this->assertEquals([], $field->query('->', 17)); |
|
|
|
|
140
|
|
|
|
141
|
|
|
// Test: Invalid #1 |
142
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
143
|
|
|
$field->setReturnType('array'); |
144
|
|
|
$field->setValue('["trabant"]'); |
145
|
|
|
$this->assertEquals([], $field->query('->', 1)); |
|
|
|
|
146
|
|
|
|
147
|
|
|
// Test: Invalid #2 |
148
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
149
|
|
|
$field->setReturnType('array'); |
150
|
|
|
$field->setValue('{'); |
151
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
152
|
|
|
$field->query('->', 3); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Tests query() by means of the integer Postgres String match operator: '->>' |
157
|
|
|
*/ |
158
|
|
|
public function testQueryWithMatchOnStr() |
159
|
|
|
{ |
160
|
|
|
// Data Source: Object |
161
|
|
|
// Return Type: ARRAY |
162
|
|
|
// Operator: "->>" (String) |
163
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
164
|
|
|
$field->setReturnType('array'); |
165
|
|
|
$field->setValue($this->getFixture('object')); |
166
|
|
|
$this->assertEquals(['Subaru' => 'Impreza'], $field->query('->>', 'Subaru')); |
|
|
|
|
167
|
|
|
$this->assertEquals( |
|
|
|
|
168
|
|
|
['japanese' => ['fast' => ['Subaru' => 'Impreza'], 'slow' => ['Honda' => 'Civic']]], |
169
|
|
|
$field->query('->>', 'japanese') |
170
|
|
|
); |
171
|
|
|
|
172
|
|
|
// Data Source: Object |
173
|
|
|
// Return Type: JSON |
174
|
|
|
// Operator: "->>" (String) |
175
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
176
|
|
|
$field->setReturnType('json'); |
177
|
|
|
$field->setValue($this->getFixture('object')); |
178
|
|
|
$this->assertEquals('{"Subaru":"Impreza"}', $field->query('->>', 'Subaru')); |
|
|
|
|
179
|
|
|
$this->assertEquals( |
|
|
|
|
180
|
|
|
'{"japanese":{"fast":{"Subaru":"Impreza"},"slow":{"Honda":"Civic"}}}', |
181
|
|
|
$field->query('->>', 'japanese') |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
// Data Source: Object |
185
|
|
|
// Return Type: SilverStripe |
186
|
|
|
// Operator: "->>" (String) |
187
|
|
|
// SS Type: Varchar |
188
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
189
|
|
|
$field->setReturnType('silverstripe'); |
190
|
|
|
$field->setValue($this->getFixture('object')); |
191
|
|
|
$this->assertInternalType('array', $field->query('->>', 'Subaru')); |
|
|
|
|
192
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'Subaru')['Subaru']); |
|
|
|
|
193
|
|
|
$this->assertEquals('Impreza', $field->query('->>', 'Subaru')['Subaru']->getValue()); |
|
|
|
|
194
|
|
|
|
195
|
|
|
// Data Source: Object |
196
|
|
|
// Return Type: SilverStripe |
197
|
|
|
// Operator: "->>" (String) |
198
|
|
|
// SS Type: Boolean |
199
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
200
|
|
|
$field->setReturnType('silverstripe'); |
201
|
|
|
$field->setValue($this->getFixture('object')); |
202
|
|
|
$this->assertInternalType('array', $field->query('->>', 'beer tastes good')); |
|
|
|
|
203
|
|
|
$this->assertInstanceOf('Boolean', $field->query('->>', 'beer tastes good')['beer tastes good']); |
|
|
|
|
204
|
|
|
$this->assertEquals(1, $field->query('->>', 'beer tastes good')['beer tastes good']->getValue()); |
|
|
|
|
205
|
|
|
|
206
|
|
|
// Data Source: Object |
207
|
|
|
// Return Type: SilverStripe |
208
|
|
|
// Operator: "->>" (String) |
209
|
|
|
// SS Type: Float |
210
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
211
|
|
|
$field->setReturnType('silverstripe'); |
212
|
|
|
$field->setValue($this->getFixture('object')); |
213
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how sure are you')); |
|
|
|
|
214
|
|
|
$this->assertInstanceOf('Float', $field->query('->>', 'how sure are you')['how sure are you']); |
|
|
|
|
215
|
|
|
$this->assertEquals(99.99, $field->query('->>', 'how sure are you')['how sure are you']->getValue()); |
|
|
|
|
216
|
|
|
|
217
|
|
|
// Data Source: Object |
218
|
|
|
// Return Type: SilverStripe |
219
|
|
|
// Operator: "->>" (String) |
220
|
|
|
// SS Type: Int |
221
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
222
|
|
|
$field->setReturnType('silverstripe'); |
223
|
|
|
$field->setValue($this->getFixture('object')); |
224
|
|
|
$this->assertInternalType('array', $field->query('->>', 'how high')); |
|
|
|
|
225
|
|
|
$this->assertInstanceOf('Int', $field->query('->>', 'how high')['how high']); |
|
|
|
|
226
|
|
|
$this->assertEquals(6, $field->query('->>', 'how high')['how high']->getValue()); |
|
|
|
|
227
|
|
|
|
228
|
|
|
// Data Source: Object |
229
|
|
|
// Return Type: SilverStripe |
230
|
|
|
// Operator: "->>" (String) |
231
|
|
|
// SS Type: N/A Nested sub-array example, expect an array |
232
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
233
|
|
|
$field->setReturnType('silverstripe'); |
234
|
|
|
$field->setValue($this->getFixture('object')); |
235
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']); |
|
|
|
|
236
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
237
|
|
|
$this->assertCount(2, $field->query('->>', 'planes')['planes']['russian']); |
|
|
|
|
238
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][0]); |
|
|
|
|
239
|
|
|
$this->assertInstanceOf('Varchar', $field->query('->>', 'planes')['planes']['russian'][1]); |
|
|
|
|
240
|
|
|
|
241
|
|
|
// Test: Empty #1 |
242
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
243
|
|
|
$field->setReturnType('array'); |
244
|
|
|
$field->setValue(''); |
245
|
|
|
$this->assertInternalType('array', $field->query('->>', 'planes')); |
|
|
|
|
246
|
|
|
$this->assertCount(0, $field->query('->', 3)); |
|
|
|
|
247
|
|
|
|
248
|
|
|
// Test: Empty #2 |
249
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
250
|
|
|
$field->setReturnType('array'); |
251
|
|
|
$field->setValue('["morris"]'); |
252
|
|
|
$this->assertEquals([], $field->query('->', 17)); |
|
|
|
|
253
|
|
|
|
254
|
|
|
// Test: Invalid #1 |
255
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
256
|
|
|
$field->setReturnType('array'); |
257
|
|
|
$field->setValue('["trabant"]'); |
258
|
|
|
$this->assertEquals([], $field->query('->', 1)); |
|
|
|
|
259
|
|
|
|
260
|
|
|
// Test: Invalid #2 |
261
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
262
|
|
|
$field->setReturnType('array'); |
263
|
|
|
$field->setValue('{'); |
264
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
265
|
|
|
$field->query('->', 3); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Tests query() by means of the Postgres path-match operator: '#>' |
270
|
|
|
*/ |
271
|
|
|
public function testQueryWithMatchOnPath() |
272
|
|
|
{ |
273
|
|
|
// Data Source: Object |
274
|
|
|
// Return Type: ARRAY |
275
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
276
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
277
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
278
|
|
|
$field->setReturnType('array'); |
279
|
|
|
$field->setValue($this->getFixture('object')); |
280
|
|
|
$this->assertEquals( |
|
|
|
|
281
|
|
|
[['Subaru' => 'Impreza'],['Kawasaki' => 'KR1S250']], |
282
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
283
|
|
|
); |
284
|
|
|
|
285
|
|
|
// Data Source: Object |
286
|
|
|
// Return Type: JSON |
287
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
288
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
289
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
290
|
|
|
$field->setReturnType('json'); |
291
|
|
|
$field->setValue($this->getFixture('object')); |
292
|
|
|
$this->assertEquals( |
|
|
|
|
293
|
|
|
'[{"Subaru":"Impreza"},{"Kawasaki":"KR1S250"}]', |
294
|
|
|
$field->query('#>', '{"japanese":"fast"}') |
295
|
|
|
); |
296
|
|
|
|
297
|
|
|
// Data Source: Object |
298
|
|
|
// Return Type: SILVERSTRIPE |
299
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
300
|
|
|
// Expect: Array due to duplicate keys in different parts of the source data |
301
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
302
|
|
|
$field->setReturnType('silverstripe'); |
303
|
|
|
$field->setValue($this->getFixture('object')); |
304
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
305
|
|
|
$this->assertCount(2, $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
306
|
|
|
|
307
|
|
|
$one = $field->query('#>', '{"japanese":"fast"}')[0]; |
308
|
|
|
$two = $field->query('#>', '{"japanese":"fast"}')[1]; |
309
|
|
|
|
310
|
|
|
$this->assertInternalType('array', $one); |
|
|
|
|
311
|
|
|
$this->assertInternalType('array', $two); |
|
|
|
|
312
|
|
|
$this->assertInstanceOf('Varchar', array_values($one)[0]); |
|
|
|
|
313
|
|
|
$this->assertInstanceOf('Varchar', array_values($two)[0]); |
|
|
|
|
314
|
|
|
$this->assertEquals('Impreza', array_values($one)[0]->getValue()); |
|
|
|
|
315
|
|
|
$this->assertEquals('KR1S250', array_values($two)[0]->getValue()); |
|
|
|
|
316
|
|
|
|
317
|
|
|
// Data Source: Object |
318
|
|
|
// Return Type: ARRAY |
319
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
320
|
|
|
// Expect: Direct scalar comparison assertion |
321
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
322
|
|
|
$field->setReturnType('array'); |
323
|
|
|
$field->setValue($this->getFixture('object')); |
324
|
|
|
$this->assertEquals(['airbus'], $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
325
|
|
|
|
326
|
|
|
// Data Source: Object |
327
|
|
|
// Return Type: JSON |
328
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
329
|
|
|
// Expect: Direct scalar comparison assertion |
330
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
331
|
|
|
$field->setReturnType('json'); |
332
|
|
|
$field->setValue($this->getFixture('object')); |
333
|
|
|
$this->assertEquals('["airbus"]', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
334
|
|
|
|
335
|
|
|
// Data Source: Object |
336
|
|
|
// Return Type: SILVERSTRIPE |
337
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
338
|
|
|
// Expect: Direct scalar comparison assertion (Varchar) |
339
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
340
|
|
|
$field->setReturnType('silverstripe'); |
341
|
|
|
$field->setValue($this->getFixture('object')); |
342
|
|
|
$this->assertInternalType('array', $field->query('#>', '{"planes":"french"}')); |
|
|
|
|
343
|
|
|
$this->assertInstanceOf('Varchar', $field->query('#>', '{"planes":"french"}')[0]); |
|
|
|
|
344
|
|
|
$this->assertEquals('airbus', $field->query('#>', '{"planes":"french"}')[0]->getValue()); |
|
|
|
|
345
|
|
|
|
346
|
|
|
// Data Source: Object |
347
|
|
|
// Return Type: SILVERSTRIPE |
348
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
349
|
|
|
// Expect: Direct scalar comparison assertion (Float) |
350
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
351
|
|
|
$field->setReturnType('silverstripe'); |
352
|
|
|
$field->setValue($this->getFixture('object')); |
353
|
|
|
|
354
|
|
|
$res = $field->query('#>', '{"floats":"0"}'); |
355
|
|
|
|
356
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
357
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
358
|
|
|
$this->assertInstanceOf('Float', array_values($res[0])[0]); |
|
|
|
|
359
|
|
|
$this->assertEquals(99.99, array_values($res[0])[0]->getValue()); |
|
|
|
|
360
|
|
|
|
361
|
|
|
// Data Source: Object |
362
|
|
|
// Return Type: SILVERSTRIPE |
363
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
364
|
|
|
// Expect: Direct scalar comparison assertion (Int) |
365
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
366
|
|
|
$field->setReturnType('silverstripe'); |
367
|
|
|
$field->setValue($this->getFixture('object')); |
368
|
|
|
|
369
|
|
|
$res = $field->query('#>', '{"ints":"0"}'); |
370
|
|
|
|
371
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
372
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "floats" key is a JSON array |
|
|
|
|
373
|
|
|
$this->assertInstanceOf('Int', array_values($res[0])[1]); |
|
|
|
|
374
|
|
|
$this->assertEquals(6, array_values($res[0])[1]->getValue()); |
|
|
|
|
375
|
|
|
|
376
|
|
|
// Data Source: Object |
377
|
|
|
// Return Type: SILVERSTRIPE |
378
|
|
|
// Operator: "#>" (Path) |
|
|
|
|
379
|
|
|
// Expect: Direct scalar comparison assertion (Boolean) |
380
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
381
|
|
|
$field->setReturnType('silverstripe'); |
382
|
|
|
$field->setValue($this->getFixture('object')); |
383
|
|
|
|
384
|
|
|
$res = $field->query('#>', '{"booleans":"0"}'); |
385
|
|
|
|
386
|
|
|
$this->assertInternalType('array', $res); |
|
|
|
|
387
|
|
|
$this->assertInternalType('array', $res[0]); // Why? Because value of "booleans" key is a JSON array |
|
|
|
|
388
|
|
|
$this->assertInstanceOf('Boolean', array_values($res[0])[0]); |
|
|
|
|
389
|
|
|
$this->assertEquals(1, array_values($res[0])[0]->getValue()); |
|
|
|
|
390
|
|
|
|
391
|
|
|
// #1 Empty source data |
392
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
393
|
|
|
$field->setReturnType('array'); |
394
|
|
|
$field->setValue(''); |
395
|
|
|
$this->assertEquals([], $field->query('#>', '{"japanese":"fast"}')); |
|
|
|
|
396
|
|
|
|
397
|
|
|
// #2 JSON path not found |
398
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
399
|
|
|
$field->setReturnType('silverstripe'); |
400
|
|
|
$field->setValue($this->getFixture('object')); |
401
|
|
|
$this->assertNull($field->query('#>', '{"ints":"1"}')); // The "ints" key only has a single array as a value |
|
|
|
|
402
|
|
|
|
403
|
|
|
// #3 Invalid operand on RHS |
404
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
405
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
406
|
|
|
$field->setReturnType('array'); |
407
|
|
|
$field->setValue($this->getFixture('object')); |
408
|
|
|
$this->assertEquals(['Kawasaki' => 'KR1S250'], $field->query('#>', '{"japanese":"fast"')); |
|
|
|
|
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
/** |
412
|
|
|
* Get the contents of a fixture |
413
|
|
|
* |
414
|
|
|
* @param string $fixture |
415
|
|
|
* @return string |
416
|
|
|
*/ |
417
|
|
|
private function getFixture($fixture) |
418
|
|
|
{ |
419
|
|
|
$files = $this->fixtures; |
420
|
|
|
return file_get_contents($files[$fixture]); |
421
|
|
|
} |
422
|
|
|
|
423
|
|
|
} |
424
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.