public function RestfulSimpleJsonTestCase::testSimpleJson in RESTful 7
Same name and namespace in other branches
- 7.2 tests/RestfulSimpleJsonTestCase.test \RestfulSimpleJsonTestCase::testSimpleJson()
Test the Simple JSON formatter.
File
- tests/
RestfulSimpleJsonTestCase.test, line 27 - Contains RestfulSimpleJsonTestCase
Class
- RestfulSimpleJsonTestCase
- @file Contains RestfulSimpleJsonTestCase
Code
public function testSimpleJson() {
$node = restful_test_create_node_with_tags();
$handler = restful_get_restful_handler('articles', 1, 5);
// Use the simple JSON formatter.
$handler
->setPluginKey('formatter', 'json');
$response = $handler
->get($node->nid);
$formatter = \RestfulManager::outputFormat($handler);
$result = $formatter
->format($response);
if ($decoded_json = drupal_json_decode($result)) {
$this
->pass('Valid JSON output generated.');
}
else {
$this
->fail('Invalid JSON output generated.');
}
$this
->assertNotNull($decoded_json['data'], 'The "data" wrapper was created successfully.');
// Assert the embedded tags.
foreach ($decoded_json['data'][0]['tags'] as $index => $tag_info) {
$this
->assertNotNull($tag_info['self'], 'The "self" property was populated for the tags.');
$this
->assertNotNull($tag_info['id'], 'The "id" property was populated.');
$this
->assertEqual($tag_info['label'], $response[0]['tags'][$index]['label'], 'The "label" property was populated correctly.');
}
// Assert the HATEOAS.
// Create another node for pagination.
restful_test_create_node_with_tags();
$handler
->setRange(1);
$response = $handler
->get();
$result = $formatter
->format($response);
$decoded_json = drupal_json_decode($result);
$this
->assertNotNull($decoded_json['self'], '"Self" property added.');
$this
->assertEqual($decoded_json['count'], 2, 'Count was populated correctly.');
$this
->assertEqual(count($decoded_json['data']), 1, 'The correct number of items was listed.');
$this
->assertNotNull($decoded_json['next'], '"Next" property added.');
}