Merge pull request #1007 from bgarel/use-or-between-path-patterns

Change the behavior of path_patterns filter to use OR instead of AND
This commit is contained in:
Guilhem Niot 2017-06-08 05:13:35 +00:00 committed by GitHub
commit e8f44b2a04
2 changed files with 46 additions and 3 deletions

View File

@ -38,11 +38,11 @@ final class FilteredRouteCollectionBuilder
private function match(Route $route): bool
{
foreach ($this->pathPatterns as $pathPattern) {
if (!preg_match('{'.$pathPattern.'}', $route->getPath())) {
return false;
if (preg_match('{'.$pathPattern.'}', $route->getPath())) {
return true;
}
}
return true;
return false;
}
}

View File

@ -0,0 +1,43 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\Routing;
use Nelmio\ApiDocBundle\Routing\FilteredRouteCollectionBuilder;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use PHPUnit\Framework\TestCase;
/**
* Tests for FilteredRouteCollectionBuilder class
*/
class FilteredRouteCollectionBuilderTest extends TestCase
{
public function testFilter()
{
$pathPattern = [
'^/api/foo',
'^/api/bar',
];
$routes = new RouteCollection();
$routes->add('r1', new Route('/api/bar/action1'));
$routes->add('r2', new Route('/api/foo/action1'));
$routes->add('r3', new Route('/api/foo/action2'));
$routes->add('r4', new Route('/api/demo'));
$routes->add('r5', new Route('/_profiler/test/test'));
$routeBuilder = new FilteredRouteCollectionBuilder($pathPattern);
$filteredRoutes = $routeBuilder->filter($routes);
$this->assertCount(3, $filteredRoutes);
}
}