function StyleSerializerTest::testSerializerResponses

Same name in this branch
  1. 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testSerializerResponses()
Same name in other branches
  1. 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testSerializerResponses()

Checks the behavior of the Serializer callback paths and row plugins.

File

core/modules/hal/tests/src/Functional/rest/Views/StyleSerializerTest.php, line 84

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\hal\Functional\rest\Views

Code

public function testSerializerResponses() {
    // Test the entity rows.
    $view = Views::getView('test_serializer_display_entity');
    $view->initDisplay();
    $this->executeView($view);
    // Get the serializer service.
    $serializer = $this->container
        ->get('serializer');
    $entities = [];
    foreach ($view->result as $row) {
        $entities[] = $row->_entity;
    }
    $expected_cache_tags = $view->getCacheTags();
    $expected_cache_tags[] = 'entity_test_list';
    
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    foreach ($entities as $entity) {
        $expected_cache_tags = Cache::mergeTags($expected_cache_tags, $entity->getCacheTags());
    }
    $expected = $serializer->serialize($entities, 'hal_json');
    $actual_json = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'hal_json',
        ],
    ]);
    $this->assertSame($expected, $actual_json, 'The expected HAL output was found.');
    $this->assertCacheTags($expected_cache_tags);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.