class AcceptHeaderMatcherTest

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
  2. 10 core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php \Drupal\Tests\Core\Routing\AcceptHeaderMatcherTest
  3. 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

Expanded class hierarchy of AcceptHeaderMatcherTest

File

core/tests/Drupal/Tests/Core/Routing/AcceptHeaderMatcherTest.php, line 15

Namespace

Drupal\Tests\Core\Routing
View 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() {
        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 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) {
        $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() {
        // 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 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.
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.
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.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.