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