From e31cf8274dfb81a0da877d9cc8a940ecc9fc0263 Mon Sep 17 00:00:00 2001 From: Larry Garfield Date: Wed, 4 Jul 2012 16:35:47 -0500 Subject: [PATCH] Add tests for the path matcher, both outline-based paths and not-found paths. --- core/lib/Drupal/Core/Routing/PathMatcher.php | 5 ++ .../system/Tests/Routing/PathMatcherTest.php | 54 ++++++++++++++++++- .../system/Tests/Routing/RoutingFixtures.php | 31 ++++++++++- 3 files changed, 87 insertions(+), 3 deletions(-) diff --git a/core/lib/Drupal/Core/Routing/PathMatcher.php b/core/lib/Drupal/Core/Routing/PathMatcher.php index 73f1cb8419c..a5f007c804b 100644 --- a/core/lib/Drupal/Core/Routing/PathMatcher.php +++ b/core/lib/Drupal/Core/Routing/PathMatcher.php @@ -4,6 +4,7 @@ namespace Drupal\Core\Routing; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Drupal\Core\Database\Connection; @@ -59,6 +60,10 @@ class PathMatcher implements InitialMatcherInterface { $collection->add($name, unserialize($route)); } + if (!count($collection->all())) { + throw new ResourceNotFoundException(); + } + return $collection; } diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/PathMatcherTest.php b/core/modules/system/lib/Drupal/system/Tests/Routing/PathMatcherTest.php index 9e17cfe693c..1e91a34e84e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/PathMatcherTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/PathMatcherTest.php @@ -10,12 +10,15 @@ namespace Drupal\system\Tests\Routing; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; +use Symfony\Component\Routing\Exception\ResourceNotFoundException; use Drupal\simpletest\UnitTestBase; use Drupal\Core\Routing\PathMatcher; use Drupal\Core\Database\Database; use Drupal\Core\Routing\MatcherDumper; +use Exception; + /** * Basic tests for the UrlMatcherDumper. */ @@ -99,9 +102,56 @@ class PathMatcherTest extends UnitTestBase { * Confirms that we can find routes whose pattern would match the request. */ function testOutlinePathMatch() { + $connection = Database::getConnection(); + $matcher = new PathMatcher($connection, 'test_routes'); + + $this->fixtures->createTables($connection); + + $dumper = new MatcherDumper($connection, 'test_routes'); + $dumper->addRoutes($this->fixtures->complexRouteCollection()); + $dumper->dump(); + + $path = '/path/1/one'; + + $request = Request::create($path, 'GET'); + + $routes = $matcher->matchRequestPartial($request); + + // All of the matching paths have the correct pattern. + foreach ($routes as $route) { + $this->assertEqual($route->compile()->getPatternOutline(), '/path/%/one', t('Found path has correct pattern')); + } + + $this->assertEqual(count($routes->all()), 2, t('The correct number of routes was found.')); + $this->assertNotNull($routes->get('route_a'), t('The first matching route was found.')); + $this->assertNotNull($routes->get('route_b'), t('The second matching route was not found.')); + } + + /** + * Confirm that an exception is thrown when no matching path is found. + */ + function testOutlinePathNoMatch() { + $connection = Database::getConnection(); + $matcher = new PathMatcher($connection, 'test_routes'); + + $this->fixtures->createTables($connection); + + $dumper = new MatcherDumper($connection, 'test_routes'); + $dumper->addRoutes($this->fixtures->complexRouteCollection()); + $dumper->dump(); + + $path = '/no/such/path'; + + $request = Request::create($path, 'GET'); + + try { + $routes = $matcher->matchRequestPartial($request); + $this->fail(t('No exception was thrown.')); + } + catch (Exception $e) { + $this->assertTrue($e instanceof ResourceNotFoundException, t('The correct exception was thrown.')); + } } - } - diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/RoutingFixtures.php b/core/modules/system/lib/Drupal/system/Tests/Routing/RoutingFixtures.php index cdcb26572f5..8937c75563a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/RoutingFixtures.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/RoutingFixtures.php @@ -31,7 +31,6 @@ class RoutingFixtures { } } - /** * Returns a standard set of routes for testing. * @@ -62,6 +61,36 @@ class RoutingFixtures { return $collection; } + /** + * Returns a complex set of routes for testing. + * + * @return \Symfony\Component\Routing\RouteCollection + */ + public function complexRouteCollection() { + $collection = new RouteCollection(); + + $route = new Route('/path/{thing}/one'); + $route->setRequirement('_method', 'GET'); + $collection->add('route_a', $route); + + $route = new Route('/path/{thing}/one'); + $route->setRequirement('_method', 'PUT'); + $collection->add('route_b', $route); + + $route = new Route('/somewhere/{item}/over/the/rainbow'); + $route->setRequirement('_method', 'GET'); + $collection->add('route_c', $route); + + $route = new Route('/another/{thing}/about/{item}'); + $collection->add('route_d', $route); + + $route = new Route('/path/add/one'); + $route->setRequirement('_method', 'GET|HEAD'); + $collection->add('route_e', $route); + + return $collection; + } + public function routingTableDefinition() { $tables['test_routes'] = array(