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
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\jsonapi_resources\Unit\Routing\ResourceRoutesTest
Expanded class hierarchy of ResourceRoutesTest
File
- tests/
src/ Unit/ Routing/ ResourceRoutesTest.php, line 20
Namespace
Drupal\Tests\jsonapi_resources\Unit\RoutingView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
ResourceRoutesTest:: |
public | function | Tests route decoration. | |
ResourceRoutesTest:: |
public | function | Tests for an exception when the %jsonapi% base path placeholder is missing. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. | |
UnitTestCase:: |
protected | function | 340 |