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
|
|
|
* @todo There are a ton more permutations of a JSONPath regex |
15
|
|
|
* See the walk() method in JSONStore |
16
|
|
|
*/ |
17
|
|
View Code Duplication |
public function testIsValidExpression() |
|
|
|
|
18
|
|
|
{ |
19
|
|
|
$field = JSONText::create('MyJSON'); |
20
|
|
|
|
21
|
|
|
$this->assertTrue($field->isValidExpression('$..')); |
|
|
|
|
22
|
|
|
$this->assertTrue($field->isValidExpression('$.[2]')); |
|
|
|
|
23
|
|
|
$this->assertTrue($field->isValidExpression('$.cars.american[*]')); |
|
|
|
|
24
|
|
|
$this->assertFalse($field->isValidExpression('$')); |
|
|
|
|
25
|
|
|
$this->assertFalse($field->isValidExpression('$[2]')); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return void |
30
|
|
|
*/ |
31
|
|
|
public function testIsValidJson() |
32
|
|
|
{ |
33
|
|
|
$field = JSONText::create('MyJSON'); |
34
|
|
|
|
35
|
|
|
$this->assertFalse($field->isValidJson('')); |
|
|
|
|
36
|
|
|
$this->assertTrue($field->isValidJson('true')); |
|
|
|
|
37
|
|
|
$this->assertTrue($field->isValidJson('false')); |
|
|
|
|
38
|
|
|
$this->assertFalse($field->isValidJson('null')); |
|
|
|
|
39
|
|
|
$this->assertFalse($field->isValidJson("['one']")); |
|
|
|
|
40
|
|
|
$this->assertFalse($field->isValidJson('["one]')); |
|
|
|
|
41
|
|
|
$this->assertFalse($field->isValidJson('{{{')); |
|
|
|
|
42
|
|
|
$this->assertTrue($field->isValidJson('[]')); |
|
|
|
|
43
|
|
|
$this->assertTrue($field->isValidJson('["one"]')); |
|
|
|
|
44
|
|
|
$this->assertTrue($field->isValidJson('["one","two"]')); |
|
|
|
|
45
|
|
|
$this->assertTrue($field->isValidJson('{"cars":{"american":["buick","oldsmobile"]}}')); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need |
51
|
|
|
* to allow otherwise invalid JSON by means of an optional 2nd param |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
public function testIsValidDBValue() |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$field = JSONText::create('MyJSON'); |
58
|
|
|
|
59
|
|
|
$this->assertFalse($field->isValidDBValue('true')); |
|
|
|
|
60
|
|
|
$this->assertFalse($field->isValidDBValue('false')); |
|
|
|
|
61
|
|
|
$this->assertFalse($field->isValidDBValue('null')); |
|
|
|
|
62
|
|
|
$this->assertTrue($field->isValidDBValue('')); |
|
|
|
|
63
|
|
|
$this->assertTrue($field->isValidJson('["one","two"]')); |
|
|
|
|
64
|
|
|
$this->assertTrue($field->isValidJson('{"cars":{"american":["buick","oldsmobile"]}}')); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
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.