class AcceptHeaderMatcherTest
Same name in other branches
- 9 core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
- 8.9.x core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
- 11.x core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
Confirm that the mime types partial matcher is functioning properly.
@group Routing
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait
- class \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of AcceptHeaderMatcherTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Routing/ AcceptHeaderMatcherTest.php, line 17
Namespace
Drupal\Tests\Core\RoutingView source
class AcceptHeaderMatcherTest extends UnitTestCase {
/**
* A collection of shared fixture data for tests.
*
* @var \Drupal\Tests\Core\Routing\RoutingFixtures
*/
protected $fixtures;
/**
* The matcher object that is going to be tested.
*
* @var \Drupal\accept_header_routing_test\Routing\AcceptHeaderMatcher
*/
protected $matcher;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->fixtures = new RoutingFixtures();
$this->matcher = new AcceptHeaderMatcher();
}
/**
* Provides data for the Accept header filtering test.
*
* @see Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest::testAcceptFiltering()
*/
public static function acceptFilterProvider() {
return [
// Check that JSON routes get filtered and prioritized correctly.
[
'application/json, text/xml;q=0.9',
'json',
'route_c',
'route_e',
],
// Tests a JSON request with alternative JSON MIME type Accept header.
[
'application/x-json, text/xml;q=0.9',
'json',
'route_c',
'route_e',
],
// Tests a standard HTML request.
[
'text/html, text/xml;q=0.9',
'html',
'route_e',
'route_c',
],
];
}
/**
* Tests that requests using Accept headers get filtered correctly.
*
* @param string $accept_header
* The HTTP Accept header value of the request.
* @param string $format
* The request format.
* @param string $included_route
* The route name that should survive the filter and be ranked first.
* @param string $excluded_route
* The route name that should be filtered out during matching.
*
* @dataProvider acceptFilterProvider
*/
public function testAcceptFiltering($accept_header, $format, $included_route, $excluded_route) : void {
$collection = $this->fixtures
->sampleRouteCollection();
$request = Request::create('path/two', 'GET');
$request->headers
->set('Accept', $accept_header);
$request->setRequestFormat($format);
$routes = $this->matcher
->filter($collection, $request);
$this->assertCount(4, $routes, 'The correct number of routes was found.');
$this->assertNotNull($routes->get($included_route), "Route {$included_route} was found when matching {$accept_header}.");
$this->assertNull($routes->get($excluded_route), "Route {$excluded_route} was not found when matching {$accept_header}.");
foreach ($routes as $name => $route) {
$this->assertEquals($name, $included_route, "Route {$included_route} is the first one in the collection when matching {$accept_header}.");
break;
}
}
/**
* Confirms that the AcceptHeaderMatcher throws an exception for no-route.
*/
public function testNoRouteFound() : void {
// Remove the sample routes that would match any method.
$routes = $this->fixtures
->sampleRouteCollection();
$routes->remove('route_a');
$routes->remove('route_b');
$routes->remove('route_c');
$routes->remove('route_d');
$request = Request::create('path/two', 'GET');
$request->headers
->set('Accept', 'application/json, text/xml;q=0.9');
$request->setRequestFormat('json');
$this->expectException(NotAcceptableHttpException::class);
$this->expectExceptionMessage('No route found for the specified formats application/json text/xml');
$this->matcher
->filter($routes, $request);
}
}
Members
Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|---|
AcceptHeaderMatcherTest::$fixtures | protected | property | A collection of shared fixture data for tests. | |||
AcceptHeaderMatcherTest::$matcher | protected | property | The matcher object that is going to be tested. | |||
AcceptHeaderMatcherTest::acceptFilterProvider | public static | function | Provides data for the Accept header filtering test. | |||
AcceptHeaderMatcherTest::setUp | protected | function | Overrides UnitTestCase::setUp | |||
AcceptHeaderMatcherTest::testAcceptFiltering | public | function | Tests that requests using Accept headers get filtered correctly. | |||
AcceptHeaderMatcherTest::testNoRouteFound | public | function | Confirms that the AcceptHeaderMatcher throws an exception for no-route. | |||
PhpUnitWarnings::$deprecationWarnings | private static | property | Deprecation warnings from PHPUnit to raise with @trigger_error(). | |||
PhpUnitWarnings::addWarning | public | function | Converts PHPUnit deprecation warnings to E_USER_DEPRECATED. | |||
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. | |||
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. | ||
UnitTestCase::$root | protected | property | The app root. | 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::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
UnitTestCase::setUpBeforeClass | public static | function | ||||
UnitTestCase::__get | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.