diff --git a/tests/unit/JSONDataFormatterTest.php b/tests/unit/JSONDataFormatterTest.php index 1595967..d02c7b2 100755 --- a/tests/unit/JSONDataFormatterTest.php +++ b/tests/unit/JSONDataFormatterTest.php @@ -5,7 +5,7 @@ namespace SilverStripe\RestfulServer\Tests; use SilverStripe\RestfulServer\RestfulServer; use SilverStripe\ORM\DataObject; use SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject; - +use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\RestfulServer\DataFormatter\JSONDataFormatter; @@ -24,14 +24,31 @@ class JSONDataFormatterTest extends SapphireTest JSONDataFormatterTypeTestObject::class, ]; + protected $usesDatabase = true; + public function testJSONTypes() { + // Needed as private static $api_access = true; doesn't seem to work on the stub file + Config::inst()->update(JSONDataFormatterTypeTestObject::class, 'api_access', true); + + // Grab test object $formatter = new JSONDataFormatter(); $parent = $this->objFromFixture(JSONDataFormatterTypeTestObject::class, 'parent'); - $json = $formatter->convertDataObject($parent); - $this->assertContains('"Active":true', $json, 'boolean is false'); - $this->assertContains('"Sort":17', $json, 'Empty integer is 0'); - $this->assertContains('"Average":1.2345', $json, 'Empty float is 0'); - } + $json = json_decode($formatter->convertDataObject($parent)); + // Returns valid array and isn't null + $this->assertNotEmpty($json, 'Array is empty'); + + $timestamp = date('Y-m-d H:i:s', time() - 1); + + $this->assertEquals(9, $json->ID, "ID not equal"); + $this->assertEquals("SilverStripe\\RestfulServer\\Tests\\Stubs\\JSONDataFormatterTypeTestObject", $json->ClassName, "Class not equal"); + $this->assertEquals($timestamp, $json->LastEdited, "Last edited does not equal"); + $this->assertEquals($timestamp, $json->Created, "Created at does not equal"); + $this->assertEquals("Test Object", $json->Name); + $this->assertEquals(false, $json->Active); + $this->assertEquals(0, $json->Sort); + $this->assertEquals(0, $json->Average); + $this->assertEquals(0, $json->ParentID); + } } diff --git a/tests/unit/JSONDataFormatterTest.yml b/tests/unit/JSONDataFormatterTest.yml index 743b896..38f2b2a 100755 --- a/tests/unit/JSONDataFormatterTest.yml +++ b/tests/unit/JSONDataFormatterTest.yml @@ -1,7 +1,8 @@ SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject: + foo: + ID: 8 + Name: Test Object 1 parent: - Name: Parent - Active: true - Sort: 17 - Average: 1.2345 - Parent: =>SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject.parent + ID: 9 + Name: Test Object + Children: =>SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject.foo