1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package silverstripe-jsontext |
5
|
|
|
* @subpackage fields |
6
|
|
|
* @author Russell Michell <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
use JSONText\Fields\JSONText; |
10
|
|
|
|
11
|
|
|
class JSONTextTest extends SapphireTest |
|
|
|
|
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array |
15
|
|
|
*/ |
16
|
|
|
protected $fixtures = [ |
17
|
|
|
'array' => 'tests/fixtures/json/array.json', |
18
|
|
|
'object' => 'tests/fixtures/json/object.json' |
19
|
|
|
]; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* JSONTextTest constructor. |
23
|
|
|
* |
24
|
|
|
* Modify fixtures property to be able to run on PHP <5.6 without use of constant in class property which 5.6+ allows |
25
|
|
|
*/ |
26
|
|
|
public function __construct() |
27
|
|
|
{ |
28
|
|
|
foreach($this->fixtures as $name => $path) { |
29
|
|
|
$this->fixtures[$name] = MODULE_DIR . '/' . $path; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @todo There are a ton more permutations of a JSONPath regex |
35
|
|
|
* See the trace() method in JSONPath for more examples to work from |
36
|
|
|
*/ |
37
|
|
View Code Duplication |
public function testIsValidExpression() |
|
|
|
|
38
|
|
|
{ |
39
|
|
|
$field = JSONText::create('MyJSON'); |
40
|
|
|
|
41
|
|
|
$this->assertTrue($field->isValidExpression('$..')); |
|
|
|
|
42
|
|
|
$this->assertTrue($field->isValidExpression('*')); |
|
|
|
|
43
|
|
|
$this->assertTrue($field->isValidExpression('$.[2]')); |
|
|
|
|
44
|
|
|
$this->assertTrue($field->isValidExpression('$.cars.american[*]')); |
|
|
|
|
45
|
|
|
$this->assertTrue($field->isValidExpression('[0:1:1]')); |
|
|
|
|
46
|
|
|
$this->assertFalse($field->isValidExpression('[0:1:]')); |
|
|
|
|
47
|
|
|
$this->assertFalse($field->isValidExpression('[0:1:1')); |
|
|
|
|
48
|
|
|
$this->assertFalse($field->isValidExpression('')); |
|
|
|
|
49
|
|
|
$this->assertFalse($field->isValidExpression('$.1.cars.american[*]')); |
|
|
|
|
50
|
|
|
$this->assertFalse($field->isValidExpression('$')); |
|
|
|
|
51
|
|
|
$this->assertFalse($field->isValidExpression('$[2]')); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return void |
56
|
|
|
*/ |
57
|
|
View Code Duplication |
public function testIsValidJson() |
|
|
|
|
58
|
|
|
{ |
59
|
|
|
$field = JSONText::create('MyJSON'); |
60
|
|
|
|
61
|
|
|
$this->assertFalse($field->isValidJson('')); |
|
|
|
|
62
|
|
|
$this->assertTrue($field->isValidJson('true')); |
|
|
|
|
63
|
|
|
$this->assertTrue($field->isValidJson('false')); |
|
|
|
|
64
|
|
|
$this->assertFalse($field->isValidJson('null')); |
|
|
|
|
65
|
|
|
$this->assertFalse($field->isValidJson("['one']")); |
|
|
|
|
66
|
|
|
$this->assertFalse($field->isValidJson('["one]')); |
|
|
|
|
67
|
|
|
$this->assertFalse($field->isValidJson('{{{')); |
|
|
|
|
68
|
|
|
$this->assertTrue($field->isValidJson('[]')); |
|
|
|
|
69
|
|
|
$this->assertTrue($field->isValidJson('["one"]')); |
|
|
|
|
70
|
|
|
$this->assertTrue($field->isValidJson('["one","two"]')); |
|
|
|
|
71
|
|
|
$this->assertTrue($field->isValidJson('{"cars":{"american":["buick","oldsmobile"]}}')); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need |
77
|
|
|
* to allow otherwise invalid JSON by means of an optional 2nd param |
78
|
|
|
* |
79
|
|
|
* @return void |
80
|
|
|
*/ |
81
|
|
|
public function testIsValidDBValue() |
82
|
|
|
{ |
83
|
|
|
$field = JSONText::create('MyJSON'); |
84
|
|
|
|
85
|
|
|
$this->assertFalse($field->isValidDBValue('true')); |
|
|
|
|
86
|
|
|
$this->assertFalse($field->isValidDBValue('false')); |
|
|
|
|
87
|
|
|
$this->assertFalse($field->isValidDBValue('null')); |
|
|
|
|
88
|
|
|
$this->assertTrue($field->isValidDBValue('')); |
|
|
|
|
89
|
|
|
$this->assertTrue($field->isValidDBValue('["one","two"]')); |
|
|
|
|
90
|
|
|
$this->assertTrue($field->isValidDBValue('{"cars":{"american":["buick","oldsmobile"]}}')); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Properly excercise our internal SS type conversion. |
95
|
|
|
*/ |
96
|
|
|
public function testToSSTypes() |
97
|
|
|
{ |
98
|
|
|
$field = JSONText::create('MyJSON'); |
99
|
|
|
$field->setValue($this->getFixture('array')); |
100
|
|
|
$field->setReturnType('silverstripe'); |
101
|
|
|
|
102
|
|
|
$data = $field->last()[6]; |
103
|
|
|
$this->assertInstanceOf('Float', $data); |
|
|
|
|
104
|
|
|
|
105
|
|
|
$data = $field->first()[0]; |
106
|
|
|
$this->assertInstanceOf('Varchar', $data); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$data = $field->nth(5)[5]; |
109
|
|
|
$this->assertInstanceOf('Int', $data); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$data = $field->nth(1)[1]; |
112
|
|
|
$this->assertInstanceOf('Boolean', $data); |
|
|
|
|
113
|
|
|
|
114
|
|
|
$field->setValue('["true"]'); |
115
|
|
|
$data = $field->first()[0]; |
116
|
|
|
$this->assertInstanceOf('Varchar', $data); |
|
|
|
|
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Get the contents of a fixture |
121
|
|
|
* |
122
|
|
|
* @param string $fixture |
123
|
|
|
* @return string |
124
|
|
|
*/ |
125
|
|
|
private function getFixture($fixture) |
126
|
|
|
{ |
127
|
|
|
$files = $this->fixtures; |
128
|
|
|
return file_get_contents($files[$fixture]); |
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.