You are here

public function CurrentRouteMatchTest::testGetCurrentRouteObject in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 45
Contains \Drupal\Tests\Core\Routing\CurrentRouteMatchTest.

Class

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

Namespace

Drupal\Tests\Core\Routing

Code

public function testGetCurrentRouteObject() {
  $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'));
}