class RequestFormatRouteFilterTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest
- 8.9.x core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest
- 10 core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest
@coversDefaultClass \Drupal\Core\Routing\RequestFormatRouteFilter @group Routing
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of RequestFormatRouteFilterTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Routing/ RequestFormatRouteFilterTest.php, line 21
Namespace
Drupal\Tests\Core\RoutingView source
class RequestFormatRouteFilterTest extends UnitTestCase {
/**
* @covers ::filter
* @dataProvider filterProvider
*/
public function testFilter(RouteCollection $collection, $request_format, array $expected_filtered_collection) : void {
$route_filter = new RequestFormatRouteFilter();
$request = new Request();
$request->setRequestFormat($request_format);
$collection = $route_filter->filter($collection, $request);
$this->assertSameSize($expected_filtered_collection, $collection);
$this->assertSame($expected_filtered_collection, array_keys($collection->all()));
}
public static function filterProvider() {
$route_without_format = new Route('/test');
$route_with_format = new Route('/test');
$route_with_format->setRequirement('_format', 'json');
$route_with_multiple_formats = new Route('/test');
$route_with_multiple_formats->setRequirement('_format', 'json|xml');
$collection = new RouteCollection();
$collection->add('test_0', $route_without_format);
$collection->add('test_1', $route_with_format);
$collection->add('test_2', $route_with_multiple_formats);
$sole_route_match_single_format = new RouteCollection();
$sole_route_match_single_format->add('sole_route_single_format', $route_with_format);
return [
'nothing requested' => [
clone $collection,
'',
[
'test_0',
],
],
'xml requested' => [
clone $collection,
'xml',
[
'test_2',
'test_0',
],
],
'json requested' => [
clone $collection,
'json',
[
'test_1',
'test_2',
'test_0',
],
],
'html format requested' => [
clone $collection,
'html',
[
'test_0',
],
],
'no format requested, defaults to html' => [
clone $collection,
NULL,
[
'test_0',
],
],
'no format requested, single route match with single format, defaults to that format' => [
clone $sole_route_match_single_format,
NULL,
[
'sole_route_single_format',
],
],
];
}
/**
* @covers ::filter
*/
public function testNoRouteFound() : void {
$url = $this->prophesize(GeneratedUrl::class);
$url_assembler = $this->prophesize(UnroutedUrlAssemblerInterface::class);
$url_assembler->assemble('http://localhost/test?_format=xml', [
'query' => [
'_format' => 'json',
],
'external' => TRUE,
], TRUE)
->willReturn($url);
$container = new ContainerBuilder();
$container->set('unrouted_url_assembler', $url_assembler->reveal());
\Drupal::setContainer($container);
$collection = new RouteCollection();
$route_with_format = new Route('/test');
$route_with_format->setRequirement('_format', 'json');
$collection->add('test_0', $route_with_format);
$collection->add('test_1', clone $route_with_format);
$request = Request::create('test?_format=xml', 'GET');
$request->setRequestFormat('xml');
$route_filter = new RequestFormatRouteFilter();
$this->expectException(NotAcceptableHttpException::class);
$this->expectExceptionMessage('No route found for the specified format xml.');
$route_filter->filter($collection, $request);
}
/**
* @covers ::filter
*/
public function testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats() : void {
$this->expectException(NotAcceptableHttpException::class);
$this->expectExceptionMessage('No route found for the specified format html.');
$url = $this->prophesize(GeneratedUrl::class);
$url_assembler = $this->prophesize(UnroutedUrlAssemblerInterface::class);
$url_assembler->assemble('http://localhost/test', [
'query' => [
'_format' => 'json',
],
'external' => TRUE,
], TRUE)
->willReturn($url);
$url_assembler->assemble('http://localhost/test', [
'query' => [
'_format' => 'xml',
],
'external' => TRUE,
], TRUE)
->willReturn($url);
$container = new ContainerBuilder();
$container->set('unrouted_url_assembler', $url_assembler->reveal());
\Drupal::setContainer($container);
$collection = new RouteCollection();
$route_with_format = new Route('/test');
$route_with_format->setRequirement('_format', 'json|xml');
$collection->add('sole_route_multiple_formats', $route_with_format);
$request = Request::create('test', 'GET');
$route_filter = new RequestFormatRouteFilter();
$route_filter->filter($collection, $request);
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
ExpectDeprecationTrait::expectDeprecation | public | function | Adds an expected deprecation. | |
ExpectDeprecationTrait::getCallableName | private static | function | Returns a callable as a string suitable for inclusion in a message. | |
ExpectDeprecationTrait::setUpErrorHandler | public | function | Sets up the test error handler. | |
ExpectDeprecationTrait::tearDownErrorHandler | public | function | Tears down the test error handler. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
RequestFormatRouteFilterTest::filterProvider | public static | function | ||
RequestFormatRouteFilterTest::testFilter | public | function | @covers ::filter @dataProvider filterProvider |
|
RequestFormatRouteFilterTest::testNoRouteFound | public | function | @covers ::filter | |
RequestFormatRouteFilterTest::testNoRouteFoundWhenNoRequestFormatAndSingleRouteWithMultipleFormats | public | function | @covers ::filter | |
UnitTestCase::$root | protected | property | The app root. | |
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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase::setUp | protected | function | 356 | |
UnitTestCase::setUpBeforeClass | public static | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.