CurrentRouteMatchTest.php in Zircon Profile 8.0
File
core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php
View source
<?php
namespace Drupal\Tests\Core\Routing;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Route;
class CurrentRouteMatchTest extends RouteMatchTestBase {
protected function getRouteMatch($name, Route $route, array $parameters, array $raw_parameters) {
$request_stack = new RequestStack();
$request = new Request();
$request_stack
->push($request);
$request = $request_stack
->getCurrentRequest();
$request->attributes = new ParameterBag($parameters);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, $name);
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, $route);
$request->attributes
->set('_raw_variables', new ParameterBag($raw_parameters));
return new CurrentRouteMatch($request_stack);
}
public function testGetCurrentRouteObject() {
$request_stack = new RequestStack();
$request = new Request();
$request_stack
->push($request);
$current_route_match = new CurrentRouteMatch($request_stack);
$this
->assertNull($current_route_match
->getRouteObject());
$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'));
$request->attributes
->set('foo', '2');
$this
->assertSame('1', $current_route_match
->getParameter('foo'));
$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'));
$request_stack
->pop();
$this
->assertSame('1', $current_route_match
->getParameter('foo'));
}
public function testGetRouteMatchFromRequestWithRouting() {
$request_stack = new RequestStack();
$request = new Request();
$request_stack
->push($request);
$current_route_match = new CurrentRouteMatch($request_stack);
$route_match = $current_route_match
->getRouteMatchFromRequest($request);
$this
->assertNull($route_match
->getRouteName());
$this
->assertNull($route_match
->getRouteObject());
}
public function testGetRouteMatchFromRequest() {
$request_stack = new RequestStack();
$request = new Request();
$request_stack
->push($request);
$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');
$current_route_match = new CurrentRouteMatch($request_stack);
$route_match = $current_route_match
->getRouteMatchFromRequest($request);
$this
->assertEquals('test_route', $route_match
->getRouteName());
$this
->assertEquals($route, $route_match
->getRouteObject());
}
}