|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package silverstripe-jsontext |
|
5
|
|
|
* @subpackage fields |
|
6
|
|
|
* @author Russell Michell <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
class JSONTextTest extends SapphireTest |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $fixture = [ |
|
14
|
|
|
'indexed' => '["great wall", "ford", "trabant", "oldsmobile", "buick", "vauxhall", "morris"]', |
|
15
|
|
|
'hashed' => '{"chinese":"great wall","american":["buick","oldsmobile","ford"],"british":["vauxhall","morris"]}', |
|
16
|
|
|
'invalid' => '{"chinese":"great wall","american":["buick","oldsmobile","ford"],"british":["vauxhall","morris]', |
|
17
|
|
|
'nested' => '{"cars":{"american":["buick","oldsmobile"],"british":["vauxhall","morris"]},"planes":{"russian":["antonov","mig"],"french":"airbus"}}', |
|
18
|
|
|
'multiple' => '{"cars":{"american":["buick","oldsmobile"],"british":["vauxhall","morris"]},"planes":{"british":"airbus","french":"airbus"}}', |
|
19
|
|
|
'empty' => '' |
|
20
|
|
|
]; |
|
21
|
|
|
|
|
22
|
|
|
public function testgetValueAsIterable() |
|
23
|
|
|
{ |
|
24
|
|
|
$field = JSONText::create('MyJSON'); |
|
25
|
|
|
$field->setValue($this->fixture['invalid']); |
|
26
|
|
|
$this->setExpectedException('JSONTextException'); |
|
27
|
|
|
$this->assertEquals(['chinese' => 'great wall'], $field->getValueAsIterable()); |
|
28
|
|
|
|
|
29
|
|
|
$field = JSONText::create('MyJSON'); |
|
30
|
|
|
$field->setValue($this->fixture['empty']); |
|
31
|
|
|
$this->assertEquals([], $field->getValueAsIterable()); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
View Code Duplication |
public function testFirst_AsArray() |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$field = JSONText::create('MyJSON'); |
|
37
|
|
|
$field->setValue($this->fixture['indexed']); |
|
38
|
|
|
$field->setReturnType('array'); |
|
39
|
|
|
$this->assertEquals([0 => 'great wall'], $field->first()); |
|
40
|
|
|
|
|
41
|
|
|
$field = JSONText::create('MyJSON'); |
|
42
|
|
|
$field->setValue($this->fixture['empty']); |
|
43
|
|
|
$field->setReturnType('array'); |
|
44
|
|
|
$this->assertInternalType('array', $field->first()); |
|
45
|
|
|
$this->assertCount(0, $field->first()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
View Code Duplication |
public function testFirst_AsJson() |
|
|
|
|
|
|
49
|
|
|
{ |
|
50
|
|
|
$field = JSONText::create('MyJSON'); |
|
51
|
|
|
$field->setValue($this->fixture['indexed']); |
|
52
|
|
|
$field->setReturnType('json'); |
|
53
|
|
|
$this->assertEquals('["great wall"]', $field->first()); |
|
54
|
|
|
|
|
55
|
|
|
$field = JSONText::create('MyJSON'); |
|
56
|
|
|
$field->setValue($this->fixture['empty']); |
|
57
|
|
|
$field->setReturnType('json'); |
|
58
|
|
|
$this->assertInternalType('string', $field->first()); |
|
59
|
|
|
$this->assertEquals('[]', $field->first()); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
View Code Duplication |
public function testLast_AsArray() |
|
|
|
|
|
|
63
|
|
|
{ |
|
64
|
|
|
$field = JSONText::create('MyJSON'); |
|
65
|
|
|
$field->setValue($this->fixture['indexed']); |
|
66
|
|
|
$field->setReturnType('array'); |
|
67
|
|
|
$this->assertEquals([6 => 'morris'], $field->last()); |
|
68
|
|
|
|
|
69
|
|
|
$field = JSONText::create('MyJSON'); |
|
70
|
|
|
$field->setValue($this->fixture['empty']); |
|
71
|
|
|
$field->setReturnType('array'); |
|
72
|
|
|
$this->assertInternalType('array', $field->first()); |
|
73
|
|
|
$this->assertCount(0, $field->first()); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
View Code Duplication |
public function testLast_AsJson() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$field = JSONText::create('MyJSON'); |
|
79
|
|
|
$field->setValue($this->fixture['indexed']); |
|
80
|
|
|
$field->setReturnType('json'); |
|
81
|
|
|
$this->assertEquals('{"6":"morris"}', $field->last()); |
|
82
|
|
|
|
|
83
|
|
|
$field = JSONText::create('MyJSON'); |
|
84
|
|
|
$field->setValue($this->fixture['empty']); |
|
85
|
|
|
$field->setReturnType('json'); |
|
86
|
|
|
$this->assertInternalType('string', $field->first()); |
|
87
|
|
|
$this->assertEquals('[]', $field->first()); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function testNth_AsArray() |
|
91
|
|
|
{ |
|
92
|
|
|
$field = JSONText::create('MyJSON'); |
|
93
|
|
|
$field->setValue($this->fixture['indexed']); |
|
94
|
|
|
$field->setReturnType('array'); |
|
95
|
|
|
$this->assertEquals([0 => 'great wall'], $field->nth(0)); |
|
96
|
|
|
$this->assertEquals([2 => 'trabant'], $field->nth(2)); |
|
97
|
|
|
|
|
98
|
|
|
$field = JSONText::create('MyJSON'); |
|
99
|
|
|
$field->setValue($this->fixture['empty']); |
|
100
|
|
|
$field->setReturnType('array'); |
|
101
|
|
|
$this->assertInternalType('array', $field->first()); |
|
102
|
|
|
$this->assertCount(0, $field->first()); |
|
103
|
|
|
|
|
104
|
|
|
$this->setExpectedException('JSONTextException'); |
|
105
|
|
|
$field = JSONText::create('MyJSON'); |
|
106
|
|
|
$field->setValue($this->fixture['hashed']); |
|
107
|
|
|
$field->setReturnType('array'); |
|
108
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->nth('2')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testNth_AsJson() |
|
112
|
|
|
{ |
|
113
|
|
|
$field = JSONText::create('MyJSON'); |
|
114
|
|
|
$field->setValue($this->fixture['indexed']); |
|
115
|
|
|
$field->setReturnType('json'); |
|
116
|
|
|
$this->assertEquals('["great wall"]', $field->nth(0)); |
|
117
|
|
|
$this->assertEquals('{"2":"trabant"}', $field->nth(2)); |
|
118
|
|
|
|
|
119
|
|
|
$field = JSONText::create('MyJSON'); |
|
120
|
|
|
$field->setValue($this->fixture['empty']); |
|
121
|
|
|
$field->setReturnType('json'); |
|
122
|
|
|
$this->assertInternalType('string', $field->first()); |
|
123
|
|
|
$this->assertEquals('[]', $field->first()); |
|
124
|
|
|
|
|
125
|
|
|
$this->setExpectedException('JSONTextException'); |
|
126
|
|
|
$field = JSONText::create('MyJSON'); |
|
127
|
|
|
$field->setValue($this->fixture['hashed']); |
|
128
|
|
|
$field->setReturnType('json'); |
|
129
|
|
|
$this->assertEquals('{"british":["vauxhall","morris"]}', $field->nth('2')); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Tests query() by means of the integer Postgres operator: -> |
|
134
|
|
|
*/ |
|
135
|
|
|
public function testquery_AsInt_AsArray() |
|
136
|
|
|
{ |
|
137
|
|
|
// Hashed |
|
138
|
|
|
$field = JSONText::create('MyJSON'); |
|
139
|
|
|
$field->setValue($this->fixture['hashed']); |
|
140
|
|
|
$field->setReturnType('array'); |
|
141
|
|
|
|
|
142
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->', 5)); |
|
143
|
|
|
$this->assertEquals(['american' => ['buick', 'oldsmobile', 'ford']], $field->query('->', 1)); |
|
144
|
|
|
$this->assertEquals([], $field->query('->', '6')); // strict handling |
|
145
|
|
|
|
|
146
|
|
|
// Empty |
|
147
|
|
|
$field = JSONText::create('MyJSON'); |
|
148
|
|
|
$field->setValue($this->fixture['empty']); |
|
149
|
|
|
$field->setReturnType('array'); |
|
150
|
|
|
$this->assertEquals([], $field->query('->', 42)); |
|
151
|
|
|
|
|
152
|
|
|
// Nested |
|
153
|
|
|
$field = JSONText::create('MyJSON'); |
|
154
|
|
|
$field->setValue($this->fixture['nested']); |
|
155
|
|
|
$field->setReturnType('array'); |
|
156
|
|
|
|
|
157
|
|
|
$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->', 7)); |
|
158
|
|
|
$this->assertEquals([], $field->query('->', '7')); // Attempt to match a string using the int matcher |
|
159
|
|
|
$this->assertEquals([0 => 'buick'], $field->query('->', 2)); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Tests query() by means of the string Postgres operator: ->> |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testquery_AsStr_AsArray() |
|
166
|
|
|
{ |
|
167
|
|
|
// Hashed |
|
168
|
|
|
$field = JSONText::create('MyJSON'); |
|
169
|
|
|
$field->setValue($this->fixture['hashed']); |
|
170
|
|
|
$field->setReturnType('array'); |
|
171
|
|
|
|
|
172
|
|
|
$this->assertEquals(['british' => ['vauxhall', 'morris']], $field->query('->>', 'british')); |
|
173
|
|
|
$this->assertEquals([], $field->query('->', '6')); // strict handling |
|
174
|
|
|
|
|
175
|
|
|
// Empty |
|
176
|
|
|
$field = JSONText::create('MyJSON'); |
|
177
|
|
|
$field->setValue($this->fixture['empty']); |
|
178
|
|
|
$field->setReturnType('array'); |
|
179
|
|
|
$this->assertEquals([], $field->query('->>', 'british')); |
|
180
|
|
|
|
|
181
|
|
|
// Nested |
|
182
|
|
|
$field = JSONText::create('MyJSON'); |
|
183
|
|
|
$field->setValue($this->fixture['nested']); |
|
184
|
|
|
$field->setReturnType('array'); |
|
185
|
|
|
|
|
186
|
|
|
$this->assertEquals(['planes' => ['russian' => ['antonov', 'mig'], 'french' => 'airbus']], $field->query('->>', 'planes')); |
|
187
|
|
|
$this->assertEquals([], $field->query('->', '7')); // Attempt to match a string using the int matcher |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
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.