1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class JsonFieldTest extends SapphireTest |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
public function testJsonFieldConstructorMultiWays() |
6
|
|
|
{ |
7
|
|
|
$jsonField = new JsonField('MyJsonField', new FieldList( |
8
|
|
|
new TextField('FirstName', 'Given name'), |
9
|
|
|
new TextField('Surname', 'Last name') |
10
|
|
|
)); |
11
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2); |
|
|
|
|
12
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]')); |
|
|
|
|
13
|
|
|
|
14
|
|
|
$jsonField = new JsonField('MyJsonField', array(new TextField('FirstName', 'Given name'), |
15
|
|
|
new TextField('Surname', 'Last name'))); |
16
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2); |
|
|
|
|
17
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]')); |
|
|
|
|
18
|
|
|
|
19
|
|
|
$jsonField = new JsonField( |
20
|
|
|
'MyJsonField', |
21
|
|
|
new TextField('FirstName', 'Given name'), |
22
|
|
|
new TextField('Surname', 'Last name') |
23
|
|
|
); |
24
|
|
|
$this->assertEquals($jsonField->FieldList()->count(), 2); |
|
|
|
|
25
|
|
|
$this->assertNotNull($jsonField->FieldList()->dataFieldByName('MyJsonField[FirstName]')); |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testJsonFieldDataValueCouldDealWithArray() |
29
|
|
|
{ |
30
|
|
|
$jsonField = new JsonField('MyJsonField', new FieldList( |
31
|
|
|
new TextField('FirstName', 'Given name'), |
32
|
|
|
new TextField('Surname', 'Last name') |
33
|
|
|
)); |
34
|
|
|
$jsonField->setValue($value = array( |
35
|
|
|
'MyJsonField'=>array( |
36
|
|
|
'FirstName' => 'Normann', |
37
|
|
|
'Surname' => 'Lou', |
38
|
|
|
), |
39
|
|
|
)); |
40
|
|
|
|
41
|
|
|
$this->assertEquals($jsonField->dataValue(), Convert::array2json($value)); |
|
|
|
|
42
|
|
|
$jsonField->setValue($value = array( |
43
|
|
|
'MyJsonField'=>array(), |
44
|
|
|
)); |
45
|
|
|
$this->assertNull($jsonField->dataValue()); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
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.