Add tests for the path matcher, both outline-based paths and not-found paths.

8.0.x
Larry Garfield 2012-07-04 16:35:47 -05:00 committed by effulgentsia
parent 8ae0b323f2
commit e31cf8274d
3 changed files with 87 additions and 3 deletions

View File

@ -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;
}

View File

@ -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.'));
}
}
}

View File

@ -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(