function RoutesTest::testRoutesCollection

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Unit/Routing/RoutesTest.php \Drupal\Tests\jsonapi\Unit\Routing\RoutesTest::testRoutesCollection()
  2. 8.9.x core/modules/jsonapi/tests/src/Unit/Routing/RoutesTest.php \Drupal\Tests\jsonapi\Unit\Routing\RoutesTest::testRoutesCollection()
  3. 10 core/modules/jsonapi/tests/src/Unit/Routing/RoutesTest.php \Drupal\Tests\jsonapi\Unit\Routing\RoutesTest::testRoutesCollection()

@covers ::routes

File

core/modules/jsonapi/tests/src/Unit/Routing/RoutesTest.php, line 71

Class

RoutesTest
@coversDefaultClass \Drupal\jsonapi\Routing\Routes[[api-linebreak]] @group jsonapi

Namespace

Drupal\Tests\jsonapi\Unit\Routing

Code

public function testRoutesCollection() : void {
  // Get the route collection and start making assertions.
  $routes = $this->routes['ok']
    ->routes();
  // - 2 collection routes; GET & POST for the non-internal resource type.
  // - 3 individual routes; GET, PATCH & DELETE for the non-internal resource
  //   type.
  // - 2 related routes; GET for the non-internal resource type relationships
  //   fields: external & both.
  // - 12 relationship routes; 3 fields * 4 HTTP methods.
  //   `relationship` routes are generated even for internal target resource
  //   types (`related` routes are not).
  // - 1 for the JSON:API entry point.
  $this->assertEquals(20, $routes->count());
  $iterator = $routes->getIterator();
  // Check the collection route.
  /** @var \Symfony\Component\Routing\Route $route */
  $route = $iterator->offsetGet('jsonapi.entity_type_1--bundle_1_1.collection');
  $this->assertSame('/jsonapi/entity_type_1/bundle_1_1', $route->getPath());
  $this->assertSame([
    'lorem',
    'ipsum',
  ], $route->getOption('_auth'));
  $this->assertSame('entity_type_1--bundle_1_1', $route->getDefault(Routes::RESOURCE_TYPE_KEY));
  $this->assertEquals([
    'GET',
  ], $route->getMethods());
  $this->assertSame(Routes::CONTROLLER_SERVICE_NAME . ':getCollection', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME));
  // Check the collection POST route.
  $route = $iterator->offsetGet('jsonapi.entity_type_1--bundle_1_1.collection.post');
  $this->assertSame('/jsonapi/entity_type_1/bundle_1_1', $route->getPath());
  $this->assertSame([
    'lorem',
    'ipsum',
  ], $route->getOption('_auth'));
  $this->assertSame('entity_type_1--bundle_1_1', $route->getDefault(Routes::RESOURCE_TYPE_KEY));
  $this->assertEquals([
    'POST',
  ], $route->getMethods());
  $this->assertSame(Routes::CONTROLLER_SERVICE_NAME . ':createIndividual', $route->getDefault(RouteObjectInterface::CONTROLLER_NAME));
}

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