function RouteProviderTest::testRouteByName

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testRouteByName()
  2. 10 core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testRouteByName()
  3. 11.x core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php \Drupal\KernelTests\Core\Routing\RouteProviderTest::testRouteByName()

Test RouteProvider::getRouteByName() and RouteProvider::getRoutesByNames().

File

core/tests/Drupal/KernelTests/Core/Routing/RouteProviderTest.php, line 616

Class

RouteProviderTest
Confirm that the default route provider is working correctly.

Namespace

Drupal\KernelTests\Core\Routing

Code

public function testRouteByName() {
    $connection = Database::getConnection();
    $provider = new RouteProvider($connection, $this->state, $this->currentPath, $this->cache, $this->pathProcessor, $this->cacheTagsInvalidator, 'test_routes');
    $this->fixtures
        ->createTables($connection);
    $dumper = new MatcherDumper($connection, $this->state, 'test_routes');
    $dumper->addRoutes($this->fixtures
        ->sampleRouteCollection());
    $dumper->dump();
    $route = $provider->getRouteByName('route_a');
    $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
    $this->assertEqual($route->getMethods(), [
        'GET',
    ], 'The right route method was found.');
    $route = $provider->getRouteByName('route_b');
    $this->assertEqual($route->getPath(), '/path/one', 'The right route pattern was found.');
    $this->assertEqual($route->getMethods(), [
        'PUT',
    ], 'The right route method was found.');
    $exception_thrown = FALSE;
    try {
        $provider->getRouteByName('invalid_name');
    } catch (RouteNotFoundException $e) {
        $exception_thrown = TRUE;
    }
    $this->assertTrue($exception_thrown, 'Random route was not found.');
    $routes = $provider->getRoutesByNames([
        'route_c',
        'route_d',
        $this->randomMachineName(),
    ]);
    $this->assertCount(2, $routes, 'Only two valid routes found.');
    $this->assertEqual($routes['route_c']->getPath(), '/path/two');
    $this->assertEqual($routes['route_d']->getPath(), '/path/three');
}

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