You are here

final class ResourceRoutesTest in JSON:API Resources 8

Tests the JSON:API resource route subscriber.

@group jsonapi_resources @coversDefaultClass \Drupal\jsonapi_resources\Unstable\Routing\ResourceRoutes

Hierarchy

Expanded class hierarchy of ResourceRoutesTest

File

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

Namespace

Drupal\Tests\jsonapi_resources\Unit\Routing
View source
final class ResourceRoutesTest extends UnitTestCase {

  /**
   * Tests route decoration.
   */
  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'));
  }

  /**
   * Tests for an exception when the %jsonapi% base path placeholder is missing.
   */
  public function testMissingBasePathPlaceholder() {
    $route_collection = new RouteCollection();
    $route_defaults = [
      '_jsonapi_resource' => '\\Drupal\\jsonapi_resources_test\\Resource\\AuthorArticles',
      '_jsonapi_resource_types' => [
        'node--article',
      ],
    ];
    $route_collection
      ->add('jsonapi_resource_route', new Route('/missing-base-path', $route_defaults));
    $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());
    $this
      ->expectExceptionObject(new RouteDefinitionException("The jsonapi_resource_route route definition's path, `/missing-base-path`, must begin with `/%jsonapi%` so that the JSON:API base path can be substituted in its place."));
    $resource_routes
      ->decorateJsonapiResourceRoutes($route_rebuild_event);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ResourceRoutesTest::testDecoratedRouteCollection public function Tests route decoration.
ResourceRoutesTest::testMissingBasePathPlaceholder public function Tests for an exception when the %jsonapi% base path placeholder is missing.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340