JSONDataFormatterTest   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A testJSONTypes() 0 20 1
1
<?php
2
3
namespace SilverStripe\RestfulServer\Tests;
4
5
use SilverStripe\RestfulServer\RestfulServer;
6
use SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\RestfulServer\DataFormatter\JSONDataFormatter;
9
10
/**
11
 *
12
 * @todo Test Relation getters
13
 * @todo Test filter and limit through GET params
14
 * @todo Test DELETE verb
15
 *
16
 */
17
class JSONDataFormatterTest extends SapphireTest
18
{
19
    protected static $fixture_file = 'JSONDataFormatterTest.yml';
20
21
    protected static $extra_dataobjects = [
22
        JSONDataFormatterTypeTestObject::class,
23
    ];
24
25
    public function testJSONTypes()
26
    {
27
        $formatter = new JSONDataFormatter();
28
        $parent = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'parent');
29
        $json = $formatter->convertDataObject($parent);
30
        $this->assertRegexp('/"ID":\d+/', $json, 'PK casted to integer');
31
        $this->assertRegexp('/"Created":"\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}"/', $json, 'Datetime casted to string');
32
        $this->assertContains('"Name":"Parent"', $json, 'String casted to string');
33
        $this->assertContains('"Active":true', $json, 'Boolean casted to boolean');
34
        $this->assertContains('"Sort":17', $json, 'Integer casted to integer');
35
        $this->assertContains('"Average":1.2345', $json, 'Float casted to float');
36
        $this->assertContains('"ParentID":0', $json, 'Empty FK is 0');
37
38
        $child3 = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'child3');
39
        $json = $formatter->convertDataObject($child3);
40
        $this->assertContains('"Name":null', $json, 'Empty string is null');
41
        $this->assertContains('"Active":false', $json, 'Empty boolean is false');
42
        $this->assertContains('"Sort":0', $json, 'Empty integer is 0');
43
        $this->assertContains('"Average":0', $json, 'Empty float is 0');
44
        $this->assertRegexp('/"ParentID":\d+/', $json, 'FK casted to integer');
45
    }
46
}
47