From ee220f79b64607915ddbc59830bcda613c1cf1be Mon Sep 17 00:00:00 2001 From: Antony Videtta Date: Thu, 23 Jan 2020 12:24:09 +1300 Subject: [PATCH] Test that original fields also still exist and aren't broken --- tests/unit/JSONDataFormatterTest.php | 33 +++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/tests/unit/JSONDataFormatterTest.php b/tests/unit/JSONDataFormatterTest.php index d02c7b2..f6416e3 100755 --- a/tests/unit/JSONDataFormatterTest.php +++ b/tests/unit/JSONDataFormatterTest.php @@ -5,16 +5,14 @@ namespace SilverStripe\RestfulServer\Tests; use SilverStripe\RestfulServer\RestfulServer; use SilverStripe\ORM\DataObject; use SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject; +use SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterOriginalFunctionality; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\RestfulServer\DataFormatter\JSONDataFormatter; /** - * - * @todo Test Relation getters - * @todo Test filter and limit through GET params - * @todo Test DELETE verb - * + * Tests improvements made to JsonTypes, + * calls method which appends more fields */ class JSONDataFormatterTest extends SapphireTest { @@ -28,6 +26,7 @@ class JSONDataFormatterTest extends SapphireTest 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); @@ -39,16 +38,24 @@ class JSONDataFormatterTest extends SapphireTest // Returns valid array and isn't null $this->assertNotEmpty($json, 'Array is empty'); - $timestamp = date('Y-m-d H:i:s', time() - 1); + // Test that original fields still exist, ie id, href, and className + $standard_id = $json->Children[0]->id; + $standard_className = $json->Children[0]->className; + $standard_href = $json->Children[0]->href; + $this->assertEquals(8, $standard_id, "Standard id field not equal"); + $this->assertEquals('SilverStripe\RestfulServer\Tests\Stubs\JSONDataFormatterTypeTestObject', $standard_className, "Standard className does not equal"); + $this->assertEquals('http://localhost/api/v1/SilverStripe-RestfulServer-Tests-Stubs-JSONDataFormatterTypeTestObject/8.json', $standard_href, "Standard href field not equal"); + + // Test method improvements, more fields rather than just id, href, className $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); + $this->assertEquals(date('Y-m-d H:i:s', time() - 1), $json->LastEdited, "Last edited does not equal"); + $this->assertEquals(date('Y-m-d H:i:s', time() - 1), $json->Created, "Created at does not equal"); + $this->assertEquals("Test Object", $json->Name, "Name not equal"); + $this->assertEquals(false, $json->Active, "Active not equal to false"); + $this->assertEquals(0, $json->Sort, "Sort not equal to 0"); + $this->assertEquals(0, $json->Average, "Average not equal to 0"); + $this->assertEquals(0, $json->ParentID, "ParentID not equal to 0"); } }