You are here

public function EntityResourceTest::testGetPagedCollection in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Kernel/Controller/EntityResourceTest.php \Drupal\Tests\jsonapi\Kernel\Controller\EntityResourceTest::testGetPagedCollection()

@covers ::getCollection

File

tests/src/Kernel/Controller/EntityResourceTest.php, line 285

Class

EntityResourceTest
@coversDefaultClass \Drupal\jsonapi\Controller\EntityResource @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testGetPagedCollection() {
  $pager = new OffsetPage(1, 1);
  $request = new Request([], [], [
    '_route_params' => [
      '_json_api_params' => [
        'page' => $pager,
      ],
    ],
    '_json_api_params' => [
      'page' => $pager,
    ],
  ]);
  $entity_resource = new EntityResource($this->container
    ->get('jsonapi.resource_type.repository')
    ->get('node', 'article'), $this->container
    ->get('entity_type.manager'), $this->container
    ->get('entity_field.manager'), $this->container
    ->get('plugin.manager.field.field_type'), $this->container
    ->get('jsonapi.link_manager'), $this->container
    ->get('jsonapi.resource_type.repository'), $this->container
    ->get('renderer'));

  // Get the response.
  $response = $entity_resource
    ->getCollection($request);

  // Assertions.
  $this
    ->assertInstanceOf(JsonApiDocumentTopLevel::class, $response
    ->getResponseData());
  $this
    ->assertInstanceOf(EntityCollection::class, $response
    ->getResponseData()
    ->getData());
  $data = $response
    ->getResponseData()
    ->getData();
  $this
    ->assertCount(1, $data);
  $this
    ->assertEquals(2, $data
    ->toArray()[0]
    ->id());
  $this
    ->assertEquals([
    'node:2',
    'node_list',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());
}