You are here

public function EntityResourceTest::testGetSortedCollection 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::testGetSortedCollection()

@covers ::getCollection

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Controller

Code

public function testGetSortedCollection() {
  $sort = new Sort([
    [
      'path' => 'type',
      'direction' => 'DESC',
    ],
  ]);
  $request = new Request([], [], [
    '_route_params' => [
      '_json_api_params' => [
        'sort' => $sort,
      ],
    ],
    '_json_api_params' => [
      'sort' => $sort,
    ],
  ]);
  $entity_resource = new EntityResource($this->container
    ->get('jsonapi.resource_type.repository')
    ->get('node_type', 'node_type'), $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());
  $this
    ->assertCount(2, $response
    ->getResponseData()
    ->getData());
  $this
    ->assertEquals($response
    ->getResponseData()
    ->getData()
    ->toArray()[0]
    ->id(), 'lorem');
  $this
    ->assertEquals([
    'config:node_type_list',
  ], $response
    ->getCacheableMetadata()
    ->getCacheTags());
}