You are here

abstract class RouteMatchTestBase in Drupal 8

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

Base test class for testing classes implementing the route match interface.

Hierarchy

Expanded class hierarchy of RouteMatchTestBase

File

core/tests/Drupal/Tests/Core/Routing/RouteMatchTestBase.php, line 12

Namespace

Drupal\Tests\Core\Routing
View source
abstract class RouteMatchTestBase extends UnitTestCase {

  /**
   * Build a test route match object for the given implementation.
   *
   * @param $name
   *   Route name.
   * @param \Symfony\Component\Routing\Route $route
   *   Request object
   * @param array $parameters
   *   Parameters array
   * @param $raw_parameters
   *   Raw parameters array
   *
   * @return \Drupal\Core\Routing\RouteMatchInterface
   */
  protected abstract function getRouteMatch($name, Route $route, array $parameters, array $raw_parameters);

  /**
   * Provide sets of parameters and expected parameters for parameter tests.
   */
  public function routeMatchProvider() {
    $base_data = [
      [
        new Route('/test-route/{param_without_leading_underscore}/{_param_with_leading_underscore}', [
          'default_without_leading_underscore' => NULL,
          '_default_with_leading_underscore' => NULL,
        ]),
        [
          'param_without_leading_underscore' => 'value',
          '_param_with_leading_underscore' => 'value',
          'default_without_leading_underscore' => 'value',
          '_default_with_leading_underscore' => 'value',
          'foo' => 'value',
        ],
        // Parameters should be filtered to only those defined by the route.
        // Specifically:
        // - Path parameters, regardless of name.
        // - Defaults that are not path parameters only if they do not start with
        //   an underscore.
        [
          'param_without_leading_underscore' => 'value',
          '_param_with_leading_underscore' => 'value',
          'default_without_leading_underscore' => 'value',
        ],
      ],
    ];
    $data = [];
    foreach ($base_data as $entry) {
      $route = $entry[0];
      $params = $entry[1];
      $expected_params = $entry[2];
      $data[] = [
        $this
          ->getRouteMatch('test_route', $route, $params, $params),
        $route,
        $params,
        $expected_params,
      ];
    }
    return $data;
  }

  /**
   * @covers ::getRouteName
   * @dataProvider routeMatchProvider
   */
  public function testGetRouteName(RouteMatchInterface $route_match) {
    $this
      ->assertSame('test_route', $route_match
      ->getRouteName());
  }

  /**
   * @covers ::getRouteObject
   * @dataProvider routeMatchProvider
   */
  public function testGetRouteObject(RouteMatchInterface $route_match, Route $route) {
    $this
      ->assertSame($route, $route_match
      ->getRouteObject());
  }

  /**
   * @covers ::getParameter
   * @covers \Drupal\Core\Routing\RouteMatch::getParameterNames
   * @dataProvider routeMatchProvider
   */
  public function testGetParameter(RouteMatchInterface $route_match, Route $route, $parameters, $expected_filtered_parameters) {
    foreach ($expected_filtered_parameters as $name => $expected_value) {
      $this
        ->assertSame($expected_value, $route_match
        ->getParameter($name));
    }
    foreach (array_diff_key($parameters, $expected_filtered_parameters) as $name) {
      $this
        ->assertNull($route_match
        ->getParameter($name));
    }
  }

  /**
   * @covers ::getParameters
   * @covers \Drupal\Core\Routing\RouteMatch::getParameterNames
   * @dataProvider routeMatchProvider
   */
  public function testGetParameters(RouteMatchInterface $route_match, Route $route, $parameters, $expected_filtered_parameters) {
    $this
      ->assertSame($expected_filtered_parameters, $route_match
      ->getParameters()
      ->all());
  }

  /**
   * @covers ::getRawParameter
   * @covers \Drupal\Core\Routing\RouteMatch::getParameterNames
   * @dataProvider routeMatchProvider
   */
  public function testGetRawParameter(RouteMatchInterface $route_match, Route $route, $parameters, $expected_filtered_parameters) {
    foreach ($expected_filtered_parameters as $name => $expected_value) {
      $this
        ->assertSame($expected_value, $route_match
        ->getRawParameter($name));
    }
    foreach (array_diff_key($parameters, $expected_filtered_parameters) as $name) {
      $this
        ->assertNull($route_match
        ->getRawParameter($name));
    }
  }

  /**
   * @covers ::getRawParameters
   * @covers \Drupal\Core\Routing\RouteMatch::getParameterNames
   * @dataProvider routeMatchProvider
   */
  public function testGetRawParameters(RouteMatchInterface $route_match, Route $route, $parameters, $expected_filtered_parameters) {
    $this
      ->assertSame($expected_filtered_parameters, $route_match
      ->getRawParameters()
      ->all());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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.
RouteMatchTestBase::getRouteMatch abstract protected function Build a test route match object for the given implementation. 2
RouteMatchTestBase::routeMatchProvider public function Provide sets of parameters and expected parameters for parameter tests.
RouteMatchTestBase::testGetParameter public function @covers ::getParameter @covers \Drupal\Core\Routing\RouteMatch::getParameterNames @dataProvider routeMatchProvider
RouteMatchTestBase::testGetParameters public function @covers ::getParameters @covers \Drupal\Core\Routing\RouteMatch::getParameterNames @dataProvider routeMatchProvider
RouteMatchTestBase::testGetRawParameter public function @covers ::getRawParameter @covers \Drupal\Core\Routing\RouteMatch::getParameterNames @dataProvider routeMatchProvider
RouteMatchTestBase::testGetRawParameters public function @covers ::getRawParameters @covers \Drupal\Core\Routing\RouteMatch::getParameterNames @dataProvider routeMatchProvider
RouteMatchTestBase::testGetRouteName public function @covers ::getRouteName @dataProvider routeMatchProvider
RouteMatchTestBase::testGetRouteObject public function @covers ::getRouteObject @dataProvider routeMatchProvider
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.
UnitTestCase::setUp protected function 340