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
|
|
|
public function testIsExpressionValid() |
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
|
|
|
* Ordinarily we can just use !is_null(json_decode($json)) but SS allows empty strings passed to setValue() so we need |
30
|
|
|
* to allow otherwise invalid JSON by means of an optional 2nd param |
31
|
|
|
*/ |
32
|
|
|
public function testIsJson() |
33
|
|
|
{ |
34
|
|
|
$field = JSONText::create('MyJSON'); |
35
|
|
|
|
36
|
|
|
$this->assertFalse($field->isJson('')); |
|
|
|
|
37
|
|
|
$this->assertTrue($field->isJson('true')); |
|
|
|
|
38
|
|
|
$this->assertTrue($field->isJson('false')); |
|
|
|
|
39
|
|
|
$this->assertFalse($field->isJson('null')); |
|
|
|
|
40
|
|
|
$this->assertFalse($field->isJson("['one']")); |
|
|
|
|
41
|
|
|
$this->assertFalse($field->isJson('["one]')); |
|
|
|
|
42
|
|
|
$this->assertTrue($field->isJson('[]')); |
|
|
|
|
43
|
|
|
$this->assertTrue($field->isJson('["one"]')); |
|
|
|
|
44
|
|
|
$this->assertTrue($field->isJson('["one","two"]')); |
|
|
|
|
45
|
|
|
$this->assertTrue($field->isJson('{"cars":{"american":["buick","oldsmobile"]}}')); |
|
|
|
|
46
|
|
|
$this->assertTrue($field->isJson('', [''])); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
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.