You are here

class RequestFormatRouteFilterTest in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest

@coversDefaultClass \Drupal\Core\Routing\RequestFormatRouteFilter @group Routing

Hierarchy

Expanded class hierarchy of RequestFormatRouteFilterTest

File

core/tests/Drupal/Tests/Core/Routing/RequestFormatRouteFilterTest.php, line 20
Contains \Drupal\Tests\Core\Routing\RequestFormatRouteFilterTest.

Namespace

Drupal\Tests\Core\Routing
View source
class RequestFormatRouteFilterTest extends UnitTestCase {

  /**
   * @covers ::applies
   */
  public function testAppliesWithoutFormat() {
    $route_filter = new RequestFormatRouteFilter();
    $route = new Route('/test');
    $this
      ->assertFalse($route_filter
      ->applies($route));
  }

  /**
   * @covers ::applies
   */
  public function testAppliesWithFormat() {
    $route_filter = new RequestFormatRouteFilter();
    $route = new Route('/test');
    $route
      ->setRequirement('_format', 'json');
    $this
      ->assertTrue($route_filter
      ->applies($route));
  }

  /**
   * @covers ::filter
   */
  public function testFilter() {
    $route_filter = new RequestFormatRouteFilter();
    $route_without_format = new Route('/test');
    $route_with_format = $route = new Route('/test');
    $route_with_format
      ->setRequirement('_format', 'json');
    $route_with_multiple_formats = $route = 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);
    $request = new Request();
    $request
      ->setRequestFormat('xml');
    $collection = $route_filter
      ->filter($collection, $request);
    $this
      ->assertCount(2, $collection);
    $this
      ->assertEquals(array_keys($collection
      ->all())[0], 'test_2');
    $this
      ->assertEquals(array_keys($collection
      ->all())[1], 'test_0');
  }

  /**
   * @covers ::filter
   * @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException
   * @expectedExceptionMessage No route found for the specified format xml.
   */
  public function testNoRouteFound() {
    $collection = new RouteCollection();
    $route_with_format = $route = 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();
    $route_filter
      ->filter($collection, $request);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RequestFormatRouteFilterTest::testAppliesWithFormat public function @covers ::applies
RequestFormatRouteFilterTest::testAppliesWithoutFormat public function @covers ::applies
RequestFormatRouteFilterTest::testFilter public function @covers ::filter
RequestFormatRouteFilterTest::testNoRouteFound public function @covers ::filter @expectedException \Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException @expectedExceptionMessage No route found for the specified format xml.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root.
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName protected function Mocks a block with a block plugin.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed in 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 259