Make the mock FinalMatcher a real matcher, since it's useful on its own.

8.0.x
Larry Garfield 2012-07-04 14:52:54 -05:00 committed by effulgentsia
parent 7a8d3df9a6
commit f0c3b571e7
3 changed files with 30 additions and 8 deletions

View File

@ -10,6 +10,15 @@ use Symfony\Component\Routing\RouteCollection;
*/ */
interface FinalMatcherInterface { interface FinalMatcherInterface {
/**
* Sets the route collection this matcher should use.
*
* @param RouteCollection $collection
* The collection against which to match.
*
* @return FinalMatcherInterface
* The current matcher.
*/
public function setCollection(RouteCollection $collection); public function setCollection(RouteCollection $collection);
/** /**

View File

@ -1,22 +1,34 @@
<?php <?php
namespace Drupal\system\Tests\Routing; namespace Drupal\Core\Routing;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route; use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RouteCollection;
use Drupal\Core\Routing\FinalMatcherInterface;
/** /**
* Mock final matcher for testing. * Final matcher that simply returns the first item in the remaining routes.
* *
* This class simply matches the first remaining route. * This class simply matches the first remaining route.
*/ */
class MockFinalMatcher implements FinalMatcherInterface { class FirstEntryFinalMatcher implements FinalMatcherInterface {
/**
* The RouteCollection this matcher should match against.
*
* @var RouteCollection
*/
protected $routes; protected $routes;
/**
* Sets the route collection this matcher should use.
*
* @param RouteCollection $collection
* The collection against which to match.
*
* @return FinalMatcherInterface
* The current matcher.
*/
public function setCollection(RouteCollection $collection) { public function setCollection(RouteCollection $collection) {
$this->routes = $collection; $this->routes = $collection;
@ -25,7 +37,7 @@ class MockFinalMatcher implements FinalMatcherInterface {
public function matchRequest(Request $request) { public function matchRequest(Request $request) {
// For testing purposes, just return whatever the first route is. // Return whatever the first route in the collection is.
foreach ($this->routes as $name => $route) { foreach ($this->routes as $name => $route) {
return array_merge($this->mergeDefaults(array(), $route->getDefaults()), array('_route' => $name)); return array_merge($this->mergeDefaults(array(), $route->getDefaults()), array('_route' => $name));
} }

View File

@ -15,6 +15,7 @@ use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Drupal\simpletest\UnitTestBase; use Drupal\simpletest\UnitTestBase;
use Drupal\Core\Routing\HttpMethodMatcher; use Drupal\Core\Routing\HttpMethodMatcher;
use Drupal\Core\Routing\NestedMatcher; use Drupal\Core\Routing\NestedMatcher;
use Drupal\Core\Routing\FirstEntryFinalMatcher;
use Exception; use Exception;
@ -74,7 +75,7 @@ class HttpMethodMatcherTest extends UnitTestBase {
$matcher->setInitialMatcher(new MockPathMatcher($this->fixtures->sampleRouteCollection())); $matcher->setInitialMatcher(new MockPathMatcher($this->fixtures->sampleRouteCollection()));
$matcher->addPartialMatcher(new HttpMethodMatcher()); $matcher->addPartialMatcher(new HttpMethodMatcher());
$matcher->setFinalMatcher(new MockFinalMatcher()); $matcher->setFinalMatcher(new FirstEntryFinalMatcher());
$request = Request::create('/path/one', 'GET'); $request = Request::create('/path/one', 'GET');