|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package silverstripe-jsontext |
|
5
|
|
|
* @subpackage fields |
|
6
|
|
|
* @author Russell Michell <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use JSONText\Fields; |
|
10
|
|
|
use JSONText\Exceptions; |
|
11
|
|
|
|
|
12
|
|
|
class JSONTextSetValueTest extends SapphireTest |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var array |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $fixtures = [ |
|
18
|
|
|
'array' => 'tests/fixtures/json/array.json', |
|
19
|
|
|
'object' => 'tests/fixtures/json/object.json', |
|
20
|
|
|
'invalid' => 'tests/fixtures/json/invalid.json' |
|
21
|
|
|
]; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* JSONTextTest constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct() |
|
29
|
|
|
{ |
|
30
|
|
View Code Duplication |
foreach($this->fixtures as $name => $path) { |
|
|
|
|
|
|
31
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Tests JSONText::setValue() by means of a simple JSONPath expression operating on a JSON array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testSetValueOnSourceArray() |
|
39
|
|
|
{ |
|
40
|
|
|
// Data Source: Array |
|
41
|
|
|
// Return Type: ARRAY |
|
42
|
|
|
// Expression: '$.[2]' The third item |
|
43
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
44
|
|
|
$field->setReturnType('array'); |
|
45
|
|
|
$field->setValue($this->getFixture('array')); |
|
46
|
|
|
// Assert current value |
|
47
|
|
|
$this->assertEquals(['trabant'], $field->query('$.[2]')); |
|
|
|
|
|
|
48
|
|
|
// Now update it... |
|
49
|
|
|
$field->setValue('lada', null, '$.[2]'); |
|
50
|
|
|
// Assert new value |
|
51
|
|
|
$this->assertEquals(['lada'], $field->query('$.[2]')); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
// Data Source: Array |
|
54
|
|
|
// Return Type: ARRAY |
|
55
|
|
|
// Expression: '$.[6]' The seventh item |
|
56
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
57
|
|
|
$field->setReturnType('array'); |
|
58
|
|
|
$field->setValue($this->getFixture('array')); |
|
59
|
|
|
// Assert current value |
|
60
|
|
|
$this->assertEquals([33.3333], $field->query('$.[6]')); |
|
|
|
|
|
|
61
|
|
|
// Now update it... |
|
62
|
|
|
$field->setValue(99.99, null, '$.[6]'); |
|
63
|
|
|
// Assert new value |
|
64
|
|
|
$this->assertEquals([99.99], $field->query('$.[6]')); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
// Invalid #1 |
|
67
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
68
|
|
|
$field->setValue(99.99, null, '$[6]'); // Invalid JSON path expression |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Tests JSONText::setValue() by means of a simple JSONPath expression operating on a JSON object |
|
73
|
|
|
* |
|
74
|
|
|
* Tests performing single and multiple updates |
|
75
|
|
|
*/ |
|
76
|
|
|
public function testSetValueOnSourceObject() |
|
77
|
|
|
{ |
|
78
|
|
|
// Data Source: Object |
|
79
|
|
|
// Return Type: ARRAY |
|
80
|
|
|
// Expression: '$.[2]' The third item |
|
81
|
|
|
$field = JSONText\Fields\JSONText::create('MyJSON'); |
|
82
|
|
|
$field->setReturnType('array'); |
|
83
|
|
|
$field->setValue($this->getFixture('object')); |
|
84
|
|
|
// Assert we cannot use array accessors at the root level of the source JSON _object_ |
|
85
|
|
|
$this->assertEmpty($field->query('$.[2]')); |
|
|
|
|
|
|
86
|
|
|
// Assert current types and value |
|
87
|
|
|
$this->assertInternalType('array', $field->query('$.cars')); |
|
|
|
|
|
|
88
|
|
|
$this->assertCount(1, $field->query('$.cars')); // The "cars" key's value is an object returned as a single value array |
|
|
|
|
|
|
89
|
|
|
$this->assertCount(3, $field->query('$.cars')[0]); //...with three classifications of car manufacturer by country |
|
|
|
|
|
|
90
|
|
|
$this->assertCount(2, $field->query('$.cars')[0]['british']); |
|
|
|
|
|
|
91
|
|
|
$this->assertEquals('morris', $field->query('$.cars')[0]['british'][1]); |
|
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
// Now do a multiple update |
|
94
|
|
|
$newCars = [ |
|
95
|
|
|
'american' => ['ford', 'tesla'], |
|
96
|
|
|
'british' => ['aston martin', 'austin', 'rover'] |
|
97
|
|
|
]; |
|
98
|
|
|
|
|
99
|
|
|
$field->setValue($newCars, null, '$.cars'); |
|
100
|
|
|
|
|
101
|
|
|
// Assert news types and value |
|
102
|
|
|
$this->assertInternalType('array', $field->query('$.cars')); |
|
|
|
|
|
|
103
|
|
|
$this->assertCount(1, $field->query('$.cars')); // The "cars" key's value is an object returned as a single value array |
|
|
|
|
|
|
104
|
|
|
$this->assertCount(2, $field->query('$.cars')[0]); //...with three classifications of car manufacturer by country |
|
|
|
|
|
|
105
|
|
|
$this->assertCount(3, $field->query('$.cars')[0]['british']); |
|
|
|
|
|
|
106
|
|
|
$this->assertEquals('austin', $field->query('$.cars')[0]['british'][1]); |
|
|
|
|
|
|
107
|
|
|
|
|
108
|
|
|
// So far we've used JSONPath to identify and update, let's try Postgres operators too |
|
109
|
|
|
// Now do attempt multiple update |
|
110
|
|
|
$newerCars = [ |
|
111
|
|
|
'american' => ['chrysler', 'general motors', 'edsel'] |
|
112
|
|
|
]; |
|
113
|
|
|
|
|
114
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
|
|
115
|
|
|
$field->setValue($newerCars, null, '{"cars":"american"}'); // setValue() only takes JSONPath expressions |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Get the contents of a fixture |
|
120
|
|
|
* |
|
121
|
|
|
* @param string $fixture |
|
122
|
|
|
* @return string |
|
123
|
|
|
*/ |
|
124
|
|
|
private function getFixture($fixture) |
|
125
|
|
|
{ |
|
126
|
|
|
$files = $this->fixtures; |
|
127
|
|
|
return file_get_contents($files[$fixture]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
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.