Change the behavior of path_patterns filter to use OR instead of AND

This commit is contained in:
Benjamin GAREL 2017-06-07 13:55:32 +02:00
parent 90df7c3f9b
commit 245cf4e9c9
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);
}
}