1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package silverstripe-jsontext |
5
|
|
|
* @subpackage fields |
6
|
|
|
* @author Russell Michell <[email protected]> |
7
|
|
|
* @todo Add 'object' fixture to each |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
use JSONText\Fields; |
11
|
|
|
use JSONText\Exceptions; |
12
|
|
|
|
13
|
|
|
class JSONTextBasicTest extends SapphireTest |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
protected $fixtures = [ |
19
|
|
|
'array' => 'tests/fixtures/json/array.json', |
20
|
|
|
'object' => 'tests/fixtures/json/object.json' |
21
|
|
|
]; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \JSONText\Fields\JSONText |
25
|
|
|
*/ |
26
|
|
|
protected $sut; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* JSONTextTest constructor. |
30
|
|
|
* |
31
|
|
|
* Modifies fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
View Code Duplication |
foreach($this->fixtures as $name => $path) { |
|
|
|
|
36
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Setup the System Under Test for this test suite. |
42
|
|
|
*/ |
43
|
|
|
public function setUp() |
44
|
|
|
{ |
45
|
|
|
parent::setUp(); |
46
|
|
|
|
47
|
|
|
$this->sut = JSONText\Fields\JSONText::create('MyJSON'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function testGetValueAsJSONStore() |
51
|
|
|
{ |
52
|
|
|
$field = $this->sut; |
53
|
|
|
|
54
|
|
|
$field->setValue(''); |
55
|
|
|
$this->assertEquals([], $field->getStoreAsArray()); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$field->setValue('{'); |
58
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
59
|
|
|
$field->getJSONStore(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
public function testFirst() |
|
|
|
|
63
|
|
|
{ |
64
|
|
|
$field = $this->sut; |
65
|
|
|
|
66
|
|
|
// Test: Source data is simple JSON array |
67
|
|
|
// Return type: Array |
68
|
|
|
$field->setReturnType('array'); |
69
|
|
|
$field->setValue($this->getFixture('array')); |
70
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
71
|
|
|
$this->assertCount(1, $field->first()); |
|
|
|
|
72
|
|
|
$this->assertEquals([0 => 'great wall'], $field->first()); |
|
|
|
|
73
|
|
|
|
74
|
|
|
// Test: Source data is simple JSON array |
75
|
|
|
// Return type: JSON |
76
|
|
|
$field->setReturnType('json'); |
77
|
|
|
$field->setValue($this->getFixture('array')); |
78
|
|
|
$this->assertInternalType('string', $field->first()); |
|
|
|
|
79
|
|
|
$this->assertEquals('["great wall"]', $field->first()); |
|
|
|
|
80
|
|
|
|
81
|
|
|
// Test: Source data is simple JSON array |
82
|
|
|
// Return type: SilverStripe |
83
|
|
|
$field->setReturnType('silverstripe'); |
84
|
|
|
$field->setValue($this->getFixture('array')); |
85
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
86
|
|
|
$this->assertInstanceOf('Varchar', $field->first()[0]); |
|
|
|
|
87
|
|
|
$this->assertEquals('great wall', $field->first()[0]->getValue()); |
|
|
|
|
88
|
|
|
|
89
|
|
|
// Test: Empty |
90
|
|
|
$field->setReturnType('array'); |
91
|
|
|
$field->setValue(''); |
92
|
|
|
$this->assertInternalType('array', $field->first()); |
|
|
|
|
93
|
|
|
$this->assertCount(0, $field->first()); |
|
|
|
|
94
|
|
|
|
95
|
|
|
// Test: Invalid |
96
|
|
|
$field->setReturnType('array'); |
97
|
|
|
$field->setValue('{'); |
98
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
99
|
|
|
$field->first(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
View Code Duplication |
public function testLast() |
|
|
|
|
103
|
|
|
{ |
104
|
|
|
$field = $this->sut; |
105
|
|
|
|
106
|
|
|
// Test: Source data is simple JSON array |
107
|
|
|
// Return type: Array |
108
|
|
|
$field->setReturnType('array'); |
109
|
|
|
$field->setValue($this->getFixture('array')); |
110
|
|
|
$this->assertInternalType('array', $field->last()); |
|
|
|
|
111
|
|
|
$this->assertCount(1, $field->last()); |
|
|
|
|
112
|
|
|
$this->assertEquals([6 => 33.3333], $field->last()); |
|
|
|
|
113
|
|
|
|
114
|
|
|
// Test: Source data is simple JSON array |
115
|
|
|
// Return type: JSON |
116
|
|
|
$field->setReturnType('json'); |
117
|
|
|
$field->setValue($this->getFixture('array')); |
118
|
|
|
$this->assertInternalType('string', $field->last()); |
|
|
|
|
119
|
|
|
$this->assertEquals('{"6":33.3333}', $field->last()); |
|
|
|
|
120
|
|
|
|
121
|
|
|
// Test: Source data is simple JSON array |
122
|
|
|
// Return type: SilverStripe |
123
|
|
|
$field->setReturnType('silverstripe'); |
124
|
|
|
$field->setValue($this->getFixture('array')); |
125
|
|
|
$this->assertInternalType('array', $field->last()); |
|
|
|
|
126
|
|
|
$this->assertInstanceOf('Float', $field->last()[6]); |
|
|
|
|
127
|
|
|
$this->assertEquals(33.3333, $field->last()[6]->getValue()); |
|
|
|
|
128
|
|
|
|
129
|
|
|
// Test: Empty |
130
|
|
|
$field->setReturnType('array'); |
131
|
|
|
$field->setValue(''); |
132
|
|
|
$this->assertInternalType('array', $field->last()); |
|
|
|
|
133
|
|
|
$this->assertCount(0, $field->last()); |
|
|
|
|
134
|
|
|
|
135
|
|
|
// Test: Invalid |
136
|
|
|
$field->setReturnType('array'); |
137
|
|
|
$field->setValue('{'); |
138
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
139
|
|
|
$field->last(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testNth() |
143
|
|
|
{ |
144
|
|
|
$field = $this->sut; |
145
|
|
|
|
146
|
|
|
// Test: Source data is simple JSON array |
147
|
|
|
// Return type: Array |
148
|
|
|
$field->setReturnType('array'); |
149
|
|
|
$field->setValue($this->getFixture('array')); |
150
|
|
|
$this->assertInternalType('array', $field->nth(1)); |
|
|
|
|
151
|
|
|
$this->assertCount(1, $field->nth(1)); |
|
|
|
|
152
|
|
|
$this->assertEquals([1 => true], $field->nth(1)); |
|
|
|
|
153
|
|
|
|
154
|
|
|
// Test: Source data is simple JSON array |
155
|
|
|
// Return type: JSON |
156
|
|
|
$field->setReturnType('json'); |
157
|
|
|
$field->setValue($this->getFixture('array')); |
158
|
|
|
$this->assertInternalType('string', $field->nth(1)); |
|
|
|
|
159
|
|
|
$this->assertEquals('{"1":true}', $field->nth(1)); |
|
|
|
|
160
|
|
|
|
161
|
|
|
// Test: Source data is simple JSON array |
162
|
|
|
// Return type: SilverStripe |
163
|
|
|
$field->setReturnType('silverstripe'); |
164
|
|
|
$field->setValue($this->getFixture('array')); |
165
|
|
|
$this->assertInternalType('array', $field->nth(1)); |
|
|
|
|
166
|
|
|
$this->assertInstanceOf('Boolean', $field->nth(1)[1]); |
|
|
|
|
167
|
|
|
$this->assertEquals(true, $field->nth(1)[1]->getValue()); |
|
|
|
|
168
|
|
|
|
169
|
|
|
// Test: Empty |
170
|
|
|
$field->setReturnType('array'); |
171
|
|
|
$field->setValue(''); |
172
|
|
|
$this->assertInternalType('array', $field->nth(1)); |
|
|
|
|
173
|
|
|
$this->assertCount(0, $field->nth(1)); |
|
|
|
|
174
|
|
|
|
175
|
|
|
// Test: Invalid |
176
|
|
|
$field->setReturnType('array'); |
177
|
|
|
$field->setValue('{'); |
178
|
|
|
$this->setExpectedException('\JSONText\Exceptions\JSONTextException'); |
|
|
|
|
179
|
|
|
$field->nth(1); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the contents of a fixture |
184
|
|
|
* |
185
|
|
|
* @param string $fixture |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
private function getFixture($fixture) |
189
|
|
|
{ |
190
|
|
|
$files = $this->fixtures; |
191
|
|
|
return file_get_contents($files[$fixture]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
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.