Test automation for Catalyst improvements

This commit is contained in:
Antony Videtta 2020-01-23 10:51:05 +13:00
parent 6af68006cc
commit f208f3e527
2 changed files with 29 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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