public function generateDBEntries() { $peter = ApiTest_Author::create(array('Name' => 'Peter', 'IsMan' => true)); $marie = ApiTest_Author::create(array('Name' => 'Marie', 'IsMan' => false)); $bible = ApiTest_Book::create(array('Title' => 'The Bible', 'Pages' => 2000)); $kamasutra = ApiTest_Book::create(array('Title' => 'Kama Sutra', 'Pages' => 1000)); $helsinki = ApiTest_Library::create(array('Name' => 'Helsinki')); $paris = ApiTest_Library::create(array('Name' => 'Paris')); // write to DB $peter->write(); $marie->write(); $bible->write(); $kamasutra->write(); $helsinki->write(); $paris->write(); // relations $peter->Books()->add($bible); $marie->Books()->add($kamasutra); $helsinki->Books()->add($bible); $helsinki->Books()->add($kamasutra); $paris->Books()->add($kamasutra); // since it doesn't seem to be called automatically $ext = new RESTfulAPI_GroupExtension(); $ext->requireDefaultRecords(); }
/** * Checks data serialization */ public function testSerialize() { $serializer = $this->getSerializer(); // test single dataObject serialization $dataObject = ApiTest_Author::get()->filter(array('Name' => 'Peter'))->first(); $jsonString = $serializer->serialize($dataObject); $jsonObject = json_decode($jsonString); $this->assertEquals(1, $jsonObject->apiTest_Author->id, "EmberData Serialize should wrap result in an object in JSON root"); }
/** * Checks data serialization */ public function testSerialize() { Config::inst()->update('RESTfulAPI', 'access_control_policy', false); $serializer = $this->getSerializer(); // test single dataObject serialization $dataObject = ApiTest_Author::get()->filter(array('Name' => 'Peter'))->first(); $jsonString = $serializer->serialize($dataObject); $jsonObject = json_decode($jsonString); $this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Basic Serialize dataObject should return valid JSON"); $this->assertEquals($dataObject->Name, $jsonObject->Name, "Basic Serialize should return an object and not modify values"); // test datalist serialization $dataList = ApiTest_Author::get(); $jsonString = $serializer->serialize($dataList); $jsonArray = json_decode($jsonString); $this->assertEquals(JSON_ERROR_NONE, json_last_error(), "Basic Serialize dataList should return valid JSON"); $this->assertTrue(is_array($jsonArray), "Basic Serialize dataObject should return an object"); }
/** * Ensures the api_fields config value will define the fields that should be returned, including related models */ public function testReturnDefinedApiFieldsOnly() { Config::inst()->update('ApiTest_Author', 'api_access', true); $serializer = $this->getSerializer(); $dataObject = ApiTest_Author::get()->filter(array('Name' => 'Marie'))->first(); Config::inst()->update('ApiTest_Author', 'api_fields', array('Name')); $result = $serializer->serialize($dataObject); $result = json_decode($result); $this->assertFalse(property_exists($result, 'IsMan'), 'You should be able to exclude DB fields using api_fields config.'); $this->assertFalse(property_exists($result, 'Books'), 'You should be able to exclude related models by not including them in api_fields.'); Config::inst()->update('ApiTest_Author', 'api_fields', array('IsMan', 'Books')); $result = $serializer->serialize($dataObject); $result = json_decode($result); $this->assertTrue(property_exists($result, 'IsMan'), 'Fields listed in a DataObjects api_fields config array should be visible in the serialized API output.'); $this->assertTrue(property_exists($result, 'Books'), 'Related model name included in api_fields should be returned in output.'); }