class CurrentRouteMatchTest
Same name and namespace in other branches
- 11.x core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest
- 10 core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest
- 9 core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest
- 8.9.x core/tests/Drupal/Tests/Core/Routing/CurrentRouteMatchTest.php \Drupal\Tests\Core\Routing\CurrentRouteMatchTest
Tests Drupal\Core\Routing\CurrentRouteMatch.
Attributes
#[CoversClass(CurrentRouteMatch::class)]
#[Group('Routing')]
Hierarchy
- class \Drupal\Tests\UnitTestCase uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait, \Drupal\Tests\RandomGeneratorTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\Core\Routing\RouteMatchTestBase extends \Drupal\Tests\UnitTestCase
- class \Drupal\Tests\Core\Routing\CurrentRouteMatchTest extends \Drupal\Tests\Core\Routing\RouteMatchTestBase
- class \Drupal\Tests\Core\Routing\RouteMatchTestBase extends \Drupal\Tests\UnitTestCase
Expanded class hierarchy of CurrentRouteMatchTest
File
-
core/
tests/ Drupal/ Tests/ Core/ Routing/ CurrentRouteMatchTest.php, line 22
Namespace
Drupal\Tests\Core\RoutingView source
class CurrentRouteMatchTest extends RouteMatchTestBase {
/**
* {@inheritdoc}
*/
protected static function getRouteMatch(string $name, Route $route, array $parameters, array $raw_parameters) : RouteMatchInterface {
$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 InputBag($raw_parameters));
return new CurrentRouteMatch($request_stack);
}
/**
* Tests get current route object.
*
* @legacy-covers ::__construct
* @legacy-covers ::getRouteObject
* @legacy-covers ::getCurrentRouteMatch
* @legacy-covers ::getRouteMatch
*/
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'));
// Test a null request.
$request_stack->pop();
$this->assertTrue($current_route_match->getCurrentRouteMatch() instanceof NullRouteMatch);
}
/**
* Tests get route match from request with routing.
*/
public function testGetRouteMatchFromRequestWithRouting() : void {
$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());
}
/**
* Tests get route match from request.
*/
public function testGetRouteMatchFromRequest() : void {
$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());
}
/**
* Tests reset route match.
*/
public function testResetRouteMatch() : void {
$route = new Route('/test-route/{foo}');
$request = new Request();
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, 'test_route');
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, $route);
$request_stack = new RequestStack();
$request_stack->push($request);
$current_route_match = new CurrentRouteMatch($request_stack);
$route_name = $current_route_match->getRouteName();
$this->assertSame('test_route', $route_name);
// Replace the matched route on the request.
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, NULL);
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, NULL);
// Reset the route match.
$current_route_match->resetRouteMatch();
$route_name = $current_route_match->getRouteName();
$this->assertNull($route_name);
}
}
Members
| Title Sort descending | Deprecated | Modifiers | Object type | Summary | Overriden Title | Overrides |
|---|---|---|---|---|---|---|
| CurrentRouteMatchTest::getRouteMatch | protected static | function | Build a test route match object for the given implementation. | Overrides RouteMatchTestBase::getRouteMatch | ||
| CurrentRouteMatchTest::testGetCurrentRouteObject | public | function | Tests get current route object. | |||
| CurrentRouteMatchTest::testGetRouteMatchFromRequest | public | function | Tests get route match from request. | |||
| CurrentRouteMatchTest::testGetRouteMatchFromRequestWithRouting | public | function | Tests get route match from request with routing. | |||
| CurrentRouteMatchTest::testResetRouteMatch | public | function | Tests reset route match. | |||
| DrupalTestCaseTrait::checkErrorHandlerOnTearDown | public | function | Checks the test error handler after test execution. | |||
| ExpectDeprecationTrait::expectDeprecation | Deprecated | public | function | Adds an expected deprecation. | ||
| ExpectDeprecationTrait::regularExpressionForFormatDescription | private | function | ||||
| RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |||
| RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |||
| RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |||
| RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |||
| RouteMatchTestBase::routeMatchProvider | public static | function | Provide sets of parameters and expected parameters for parameter tests. | |||
| RouteMatchTestBase::testGetParameter | public | function | Tests get parameter. | |||
| RouteMatchTestBase::testGetParameters | public | function | Tests get parameters. | |||
| RouteMatchTestBase::testGetRawParameter | public | function | Tests get raw parameter. | |||
| RouteMatchTestBase::testGetRawParameters | public | function | Tests get raw parameters. | |||
| RouteMatchTestBase::testGetRouteName | public | function | Tests get route name. | |||
| RouteMatchTestBase::testGetRouteObject | public | function | Tests get route object. | |||
| UnitTestCase::$root | protected | property | The app root. | |||
| 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::getContainerWithCacheTagsInvalidator | protected | function | Sets up a container with a cache tags invalidator. | |||
| UnitTestCase::getStringTranslationStub | public | function | Returns a stub translation manager that just returns the passed string. | |||
| UnitTestCase::setDebugDumpHandler | public static | function | Registers the dumper CLI handler when the DebugDump extension is enabled. | |||
| UnitTestCase::setUp | protected | function | 366 | |||
| UnitTestCase::setupMockIterator | protected | function | Set up a traversable class mock to return specific items when iterated. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.