function CurrentRouteMatchTest::testGetCurrentRouteObject

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest::testGetCurrentRouteObject()
  2. 8.9.x core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest::testGetCurrentRouteObject()
  3. 10 core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest::testGetCurrentRouteObject()

@covers ::__construct @covers ::getRouteObject @covers ::getCurrentRouteMatch @covers ::getRouteMatch

File

core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php, line 44

Class

CurrentRouteMatchTest
@coversDefaultClass \Drupal\Core\Routing\CurrentRouteMatch @group Routing

Namespace

Drupal\Tests\Core\Routing

Code

public function testGetCurrentRouteObject() : void {
    $request_stack = new RequestStack();
    $request = new Request();
    $request_stack->push($request);
    $current_route_match = new CurrentRouteMatch($request_stack);
    // Before routing.
    $this->assertNull($current_route_match->getRouteObject());
    // After routing.
    $route = new Route('/test-route/{foo}');
    $request->attributes
        ->set(RouteObjectInterface::ROUTE_NAME, 'test_route');
    $request->attributes
        ->set(RouteObjectInterface::ROUTE_OBJECT, $route);
    $request->attributes
        ->set('foo', '1');
    $this->assertSame('1', $current_route_match->getParameter('foo'));
    // Immutable for the same request once a route has been matched.
    $request->attributes
        ->set('foo', '2');
    $this->assertSame('1', $current_route_match->getParameter('foo'));
    // Subrequest.
    $subrequest = new Request();
    $subrequest->attributes
        ->set(RouteObjectInterface::ROUTE_NAME, 'test_subrequest_route');
    $subrequest->attributes
        ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/test-subrequest-route/{foo}'));
    $subrequest->attributes
        ->set('foo', '2');
    $request_stack->push($subrequest);
    $this->assertSame('2', $current_route_match->getParameter('foo'));
    // Restored original request.
    $request_stack->pop();
    $this->assertSame('1', $current_route_match->getParameter('foo'));
}

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