You are here

public function ResourceRoutesTest::testDecoratedRouteCollection in JSON:API Resources 8

Tests route decoration.

File

tests/src/Unit/Routing/ResourceRoutesTest.php, line 25

Class

ResourceRoutesTest
Tests the JSON:API resource route subscriber.

Namespace

Drupal\Tests\jsonapi_resources\Unit\Routing

Code

public function testDecoratedRouteCollection() {
  $route_collection = new RouteCollection();
  $route_collection
    ->add('generic_route', new Route('/generic'));
  $route_defaults = [
    '_jsonapi_resource' => '\\Drupal\\jsonapi_resources_test\\Resource\\AuthorArticles',
    '_jsonapi_resource_types' => [
      'node--article',
    ],
  ];
  $route_collection
    ->add('jsonapi_resource_route', new Route('/%jsonapi%/resource', $route_defaults));
  $route_collection
    ->add('jsonapi_resource_multi_method_route', new Route('/%jsonapi%/resource', $route_defaults, [], [], '', [], [
    'POST',
    'PATCH',
  ]));
  $route_rebuild_event = new RouteBuildEvent($route_collection);
  $resource_type_repository = $this
    ->prophesize(ResourceTypeRepositoryInterface::class);
  $container = $this
    ->prophesize(ContainerInterface::class);
  $resource_routes = new ResourceRoutes($resource_type_repository
    ->reveal(), [
    'basic_auth' => 'basic_auth',
  ], '/custom-base-path', $container
    ->reveal());
  $resource_routes
    ->decorateJsonapiResourceRoutes($route_rebuild_event);
  $generic_route = $route_collection
    ->get('generic_route');
  $this
    ->assertSame('/generic', $generic_route
    ->getPath());
  $this
    ->assertNull($generic_route
    ->getOption('_auth'));
  $jsonapi_resource_route = $route_collection
    ->get('jsonapi_resource_route');
  $this
    ->assertSame('/custom-base-path/resource', $jsonapi_resource_route
    ->getPath());
  $this
    ->assertSame([
    'GET',
  ], $jsonapi_resource_route
    ->getMethods());
  $this
    ->assertSame([
    'basic_auth',
  ], $jsonapi_resource_route
    ->getOption('_auth'));
  $multi_method_route = $route_collection
    ->get('jsonapi_resource_multi_method_route');
  $this
    ->assertSame('/custom-base-path/resource', $multi_method_route
    ->getPath());
  $this
    ->assertSame([
    'POST',
    'PATCH',
  ], $multi_method_route
    ->getMethods());
  $this
    ->assertSame([
    'basic_auth',
  ], $multi_method_route
    ->getOption('_auth'));
}