diff --git a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php index cee502d404297..46882cdc4fcfc 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php +++ b/src/Symfony/Bundle/FrameworkBundle/Routing/Router.php @@ -77,8 +77,10 @@ public function warmUp($cacheDir) * Replaces placeholders with service container parameter values in: * - the route defaults, * - the route requirements, - * - the route pattern. - * - the route host. + * - the route path, + * - the route host, + * - the route schemes, + * - the route methods. * * @param RouteCollection $collection */ @@ -90,11 +92,27 @@ private function resolveParameters(RouteCollection $collection) } foreach ($route->getRequirements() as $name => $value) { + if ('_scheme' === $name || '_method' === $name) { + continue; // ignore deprecated requirements to not trigger deprecation warnings + } + $route->setRequirement($name, $this->resolve($value)); } $route->setPath($this->resolve($route->getPath())); $route->setHost($this->resolve($route->getHost())); + + $schemes = array(); + foreach ($route->getSchemes() as $scheme) { + $schemes = array_merge($schemes, explode('|', $this->resolve($scheme))); + } + $route->setSchemes($schemes); + + $methods = array(); + foreach ($route->getMethods() as $method) { + $methods = array_merge($methods, explode('|', $this->resolve($method))); + } + $route->setMethods($methods); } } diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php index 70cf0fcffd031..057aa6c8091a4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Routing/RedirectableUrlMatcherTest.php @@ -38,26 +38,6 @@ public function testRedirectWhenNoSlash() ); } - public function testSchemeRedirectBC() - { - $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); - - $matcher = new RedirectableUrlMatcher($coll, $context = new RequestContext()); - - $this->assertEquals(array( - '_controller' => 'Symfony\Bundle\FrameworkBundle\Controller\RedirectController::urlRedirectAction', - 'path' => '/foo', - 'permanent' => true, - 'scheme' => 'https', - 'httpPort' => $context->getHttpPort(), - 'httpsPort' => $context->getHttpsPort(), - '_route' => 'foo', - ), - $matcher->match('/foo') - ); - } - public function testSchemeRedirect() { $coll = new RouteCollection(); diff --git a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php index 8a95f512861d2..7d2e12fe3414c 100644 --- a/src/Symfony/Component/Routing/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/XmlFileLoader.php @@ -122,6 +122,8 @@ protected function parseRoute(RouteCollection $collection, \DOMElement $node, $p throw new \InvalidArgumentException(sprintf('The element in file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path)); } + trigger_error('The "pattern" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', E_USER_DEPRECATED); + $node->setAttribute('path', $node->getAttribute('pattern')); $node->removeAttribute('pattern'); } diff --git a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php index 8de520cd224eb..63d3cd6439ff5 100644 --- a/src/Symfony/Component/Routing/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Routing/Loader/YamlFileLoader.php @@ -81,6 +81,8 @@ public function load($file, $type = null) throw new \InvalidArgumentException(sprintf('The file "%s" cannot define both a "path" and a "pattern" attribute. Use only "path".', $path)); } + trigger_error('The "pattern" is deprecated since version 2.2 and will be removed in 3.0. Use the "path" option in the route definition instead.', E_USER_DEPRECATED); + $config['path'] = $config['pattern']; unset($config['pattern']); } diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php index a7cc485b925a2..377d53496b957 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/ApacheMatcherDumper.php @@ -90,10 +90,7 @@ public function dump(array $options = array()) $rules[] = $this->dumpRoute($name, $route, $options, $hostRegexUnique); - if ($req = $route->getRequirement('_method')) { - $methods = explode('|', strtoupper($req)); - $methodVars = array_merge($methodVars, $methods); - } + $methodVars = array_merge($methodVars, $route->getMethods()); } if (0 < count($methodVars)) { $rule = array('# 405 Method Not Allowed'); @@ -200,13 +197,11 @@ private function dumpRoute($name, $route, array $options, $hostRegexUnique) */ private function getRouteMethods(Route $route) { - $methods = array(); - if ($req = $route->getRequirement('_method')) { - $methods = explode('|', strtoupper($req)); - // GET and HEAD are equivalent - if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { - $methods[] = 'HEAD'; - } + $methods = $route->getMethods(); + + // GET and HEAD are equivalent + if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { + $methods[] = 'HEAD'; } return $methods; diff --git a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php index e31fa881ec937..8b7bff2e2e79c 100644 --- a/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php +++ b/src/Symfony/Component/Routing/Matcher/Dumper/PhpMatcherDumper.php @@ -215,14 +215,11 @@ private function compileRoute(Route $route, $name, $supportsRedirections, $paren $hasTrailingSlash = false; $matches = false; $hostMatches = false; - $methods = array(); + $methods = $route->getMethods(); - if ($req = $route->getRequirement('_method')) { - $methods = explode('|', strtoupper($req)); - // GET and HEAD are equivalent - if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { - $methods[] = 'HEAD'; - } + // GET and HEAD are equivalent + if (in_array('GET', $methods) && !in_array('HEAD', $methods)) { + $methods[] = 'HEAD'; } $supportsTrailingSlash = $supportsRedirections && (!$methods || in_array('HEAD', $methods)); diff --git a/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php b/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php index 35296c6c6e3ad..ef4f24c6c65f2 100644 --- a/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/TraceableUrlMatcher.php @@ -78,16 +78,16 @@ protected function matchCollection($pathinfo, RouteCollection $routes) } // check HTTP method requirement - if ($req = $route->getRequirement('_method')) { + if ($requiredMethods = $route->getMethods()) { // HEAD and GET are equivalent as per RFC if ('HEAD' === $method = $this->context->getMethod()) { $method = 'GET'; } - if (!in_array($method, $req = explode('|', strtoupper($req)))) { - $this->allow = array_merge($this->allow, $req); + if (!in_array($method, $requiredMethods)) { + $this->allow = array_merge($this->allow, $requiredMethods); - $this->addTrace(sprintf('Method "%s" does not match the requirement ("%s")', $this->context->getMethod(), implode(', ', $req)), self::ROUTE_ALMOST_MATCHES, $name, $route); + $this->addTrace(sprintf('Method "%s" does not match any of the required methods (%s)', $this->context->getMethod(), implode(', ', $requiredMethods)), self::ROUTE_ALMOST_MATCHES, $name, $route); continue; } @@ -107,7 +107,7 @@ protected function matchCollection($pathinfo, RouteCollection $routes) $scheme = $this->context->getScheme(); if (!$route->hasScheme($scheme)) { - $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes ("%s"); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route); + $this->addTrace(sprintf('Scheme "%s" does not match any of the required schemes (%s); the user will be redirected to first required scheme', $scheme, implode(', ', $requiredSchemes)), self::ROUTE_ALMOST_MATCHES, $name, $route); return true; } diff --git a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php index 3ed5ac7fb33a7..c75414480ec2b 100644 --- a/src/Symfony/Component/Routing/Matcher/UrlMatcher.php +++ b/src/Symfony/Component/Routing/Matcher/UrlMatcher.php @@ -98,7 +98,7 @@ public function match($pathinfo) } throw 0 < count($this->allow) - ? new MethodNotAllowedException(array_unique(array_map('strtoupper', $this->allow))) + ? new MethodNotAllowedException(array_unique($this->allow)) : new ResourceNotFoundException(sprintf('No routes found for "%s".', $pathinfo)); } @@ -152,14 +152,14 @@ protected function matchCollection($pathinfo, RouteCollection $routes) } // check HTTP method requirement - if ($req = $route->getRequirement('_method')) { + if ($requiredMethods = $route->getMethods()) { // HEAD and GET are equivalent as per RFC if ('HEAD' === $method = $this->context->getMethod()) { $method = 'GET'; } - if (!in_array($method, $req = explode('|', strtoupper($req)))) { - $this->allow = array_merge($this->allow, $req); + if (!in_array($method, $requiredMethods)) { + $this->allow = array_merge($this->allow, $requiredMethods); continue; } diff --git a/src/Symfony/Component/Routing/Route.php b/src/Symfony/Component/Routing/Route.php index 02b81b3483f9f..a7b6ad1fd4733 100644 --- a/src/Symfony/Component/Routing/Route.php +++ b/src/Symfony/Component/Routing/Route.php @@ -151,7 +151,7 @@ public function unserialize($serialized) */ public function getPattern() { - trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getPath() method instead and use the "path" option instead of the "pattern" option in the route definition.', E_USER_DEPRECATED); + trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the getPath() method instead.', E_USER_DEPRECATED); return $this->path; } @@ -169,7 +169,7 @@ public function getPattern() */ public function setPattern($pattern) { - trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the setPath() method instead and use the "path" option instead of the "pattern" option in the route definition.', E_USER_DEPRECATED); + trigger_error('The '.__METHOD__.' method is deprecated since version 2.2 and will be removed in 3.0. Use the setPath() method instead.', E_USER_DEPRECATED); return $this->setPath($pattern); } @@ -548,6 +548,12 @@ public function addRequirements(array $requirements) */ public function getRequirement($key) { + if ('_scheme' === $key) { + trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getSchemes() instead.', E_USER_DEPRECATED); + } elseif ('_method' === $key) { + trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use getMethods() instead.', E_USER_DEPRECATED); + } + return isset($this->requirements[$key]) ? $this->requirements[$key] : null; } @@ -649,8 +655,12 @@ private function sanitizeRequirement($key, $regex) // this is to keep BC and will be removed in a future version if ('_scheme' === $key) { + trigger_error('The "_scheme" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setSchemes() method instead or the "schemes" option in the route definition.', E_USER_DEPRECATED); + $this->setSchemes(explode('|', $regex)); } elseif ('_method' === $key) { + trigger_error('The "_method" requirement is deprecated since version 2.2 and will be removed in 3.0. Use the setMethods() method instead or the "methods" option in the route definition.', E_USER_DEPRECATED); + $this->setMethods(explode('|', $regex)); } diff --git a/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php b/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php index cc082dd62657d..921edf63d0746 100644 --- a/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/Annotation/RouteTest.php @@ -36,14 +36,14 @@ public function getValidParameters() { return array( array('value', '/Blog', 'getPath'), - array('requirements', array('_method' => 'GET'), 'getRequirements'), + array('requirements', array('locale' => 'en'), 'getRequirements'), array('options', array('compiler_class' => 'RouteCompiler'), 'getOptions'), array('name', 'blog_index', 'getName'), array('defaults', array('_controller' => 'MyBlogBundle:Blog:index'), 'getDefaults'), array('schemes', array('https'), 'getSchemes'), array('methods', array('GET', 'POST'), 'getMethods'), - array('host', array('{locale}.example.com'), 'getHost'), - array('condition', array('context.getMethod() == "GET"'), 'getCondition'), + array('host', '{locale}.example.com', 'getHost'), + array('condition', 'context.getMethod() == "GET"', 'getCondition'), ); } diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.xml b/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.xml new file mode 100644 index 0000000000000..a01ebca23ae09 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.xml @@ -0,0 +1,16 @@ + + + + + + MyBundle:Blog:show + + GET|POST|put|OpTiOnS + hTTps + \w+ + + context.getMethod() == "GET" + + diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.yml b/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.yml new file mode 100644 index 0000000000000..ada65f0568da1 --- /dev/null +++ b/src/Symfony/Component/Routing/Tests/Fixtures/legacy_validpattern.yml @@ -0,0 +1,8 @@ +blog_show_legacy: + pattern: /blog/{slug} + defaults: { _controller: "MyBundle:Blog:show" } + host: "{locale}.example.com" + requirements: { '_method': 'GET|POST|put|OpTiOnS', _scheme: https, 'locale': '\w+' } + condition: 'context.getMethod() == "GET"' + options: + compiler_class: RouteCompiler diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalid.xml b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalid.xml index 755e44304ce78..dc147d2e67a25 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalid.xml +++ b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalid.xml @@ -6,6 +6,5 @@ MyBundle:Blog:show - GET diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidroute.xml b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidroute.xml index a46961eee5f26..908958c032d38 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidroute.xml +++ b/src/Symfony/Component/Routing/Tests/Fixtures/nonvalidroute.xml @@ -6,7 +6,6 @@ MyBundle:Blog:show - GET baz diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php index f0a0cd672dbae..5b13c986da246 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php +++ b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.php @@ -13,15 +13,5 @@ array('GET', 'POST', 'put', 'OpTiOnS'), 'context.getMethod() == "GET"' )); -$collection->add('blog_show_legacy', new Route( - '/blog/{slug}', - array('_controller' => 'MyBlogBundle:Blog:show'), - array('_method' => 'GET|POST|put|OpTiOnS', '_scheme' => 'https', 'locale' => '\w+'), - array('compiler_class' => 'RouteCompiler'), - '{locale}.example.com', - array(), - array(), - 'context.getMethod() == "GET"' -)); return $collection; diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml index a8221314cb559..dbc72e46ddd4d 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml +++ b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.xml @@ -11,15 +11,5 @@ context.getMethod() == "GET" - - MyBundle:Blog:show - - GET|POST|put|OpTiOnS - hTTps - \w+ - - context.getMethod() == "GET" - - diff --git a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml index 26136c3969d9c..565abaaa2c467 100644 --- a/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml +++ b/src/Symfony/Component/Routing/Tests/Fixtures/validpattern.yml @@ -9,14 +9,5 @@ blog_show: options: compiler_class: RouteCompiler -blog_show_legacy: - pattern: /blog/{slug} - defaults: { _controller: "MyBundle:Blog:show" } - host: "{locale}.example.com" - requirements: { '_method': 'GET|POST|put|OpTiOnS', _scheme: https, 'locale': '\w+' } - condition: 'context.getMethod() == "GET"' - options: - compiler_class: RouteCompiler - blog_show_inherited: path: /blog/{slug} diff --git a/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php b/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php index 170c5b4dcf69c..6fbf7e220348b 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/Dumper/PhpGeneratorDumperTest.php @@ -118,7 +118,6 @@ public function testDumpForRouteWithDefaults() public function testDumpWithSchemeRequirement() { $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https'))); - $this->routeCollection->add('Test2', new Route('/testing_bc', array(), array('_scheme' => 'https'))); // BC file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator'))); include $this->testTmpFilepath; @@ -126,25 +125,17 @@ public function testDumpWithSchemeRequirement() $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php')); $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true); - $absoluteUrlBC = $projectUrlGenerator->generate('Test2', array(), true); $relativeUrl = $projectUrlGenerator->generate('Test1', array(), false); - $relativeUrlBC = $projectUrlGenerator->generate('Test2', array(), false); $this->assertEquals($absoluteUrl, 'ftp://localhost/app.php/testing'); - $this->assertEquals($absoluteUrlBC, 'https://localhost/app.php/testing_bc'); $this->assertEquals($relativeUrl, 'ftp://localhost/app.php/testing'); - $this->assertEquals($relativeUrlBC, 'https://localhost/app.php/testing_bc'); $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https')); $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true); - $absoluteUrlBC = $projectUrlGenerator->generate('Test2', array(), true); $relativeUrl = $projectUrlGenerator->generate('Test1', array(), false); - $relativeUrlBC = $projectUrlGenerator->generate('Test2', array(), false); $this->assertEquals($absoluteUrl, 'https://localhost/app.php/testing'); - $this->assertEquals($absoluteUrlBC, 'https://localhost/app.php/testing_bc'); $this->assertEquals($relativeUrl, '/app.php/testing'); - $this->assertEquals($relativeUrlBC, '/app.php/testing_bc'); } } diff --git a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php index 516b3ca6983a1..a2696e5a76966 100644 --- a/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php +++ b/src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php @@ -244,12 +244,6 @@ public function testRequiredParamAndEmptyPassed() public function testSchemeRequirementDoesNothingIfSameCurrentScheme() { - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); // BC - $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test')); - - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); // BC - $this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); - $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http'))); $this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test')); @@ -259,12 +253,6 @@ public function testSchemeRequirementDoesNothingIfSameCurrentScheme() public function testSchemeRequirementForcesAbsoluteUrl() { - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https'))); // BC - $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test')); - - $routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http'))); // BC - $this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test')); - $routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https'))); $this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test')); @@ -461,24 +449,6 @@ public function testUrlWithInvalidParameterInHostInNonStrictMode() $this->assertNull($generator->generate('test', array('foo' => 'baz'), false)); } - public function testGenerateNetworkPathBC() - { - $routes = $this->getRoutes('test', new Route('/{name}', array(), array('_scheme' => 'http'), array(), '{locale}.example.com')); - - $this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', - array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'network path with different host' - ); - $this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', - array('name' => 'Fabien', 'locale' => 'fr', 'query' => 'string'), UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context' - ); - $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', - array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context' - ); - $this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', - array('name' => 'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested' - ); - } - public function testGenerateNetworkPath() { $routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com', array('http'))); @@ -503,7 +473,6 @@ public function testGenerateRelativePath() $routes->add('article', new Route('/{author}/{article}/')); $routes->add('comments', new Route('/{author}/{article}/comments')); $routes->add('host', new Route('/{article}', array(), array(), array(), '{author}.example.com')); - $routes->add('schemeBC', new Route('/{author}', array(), array('_scheme' => 'https'))); // BC $routes->add('scheme', new Route('/{author}/blog', array(), array(), array(), '', array('https'))); $routes->add('unrelated', new Route('/about')); @@ -524,9 +493,6 @@ public function testGenerateRelativePath() $this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('host', array('author' => 'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH) ); - $this->assertSame('https://example.com/app.php/bernhard', $generator->generate('schemeBC', - array('author' => 'bernhard'), UrlGeneratorInterface::RELATIVE_PATH) - ); $this->assertSame('https://example.com/app.php/bernhard/blog', $generator->generate('scheme', array('author' => 'bernhard'), UrlGeneratorInterface::RELATIVE_PATH) ); diff --git a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php index 8e2d98499d478..5d66446f0fe23 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/PhpFileLoaderTest.php @@ -33,7 +33,7 @@ public function testLoadWithRoute() $routeCollection = $loader->load('validpattern.php'); $routes = $routeCollection->all(); - $this->assertCount(2, $routes, 'Two routes are loaded'); + $this->assertCount(1, $routes, 'One route is loaded'); $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); foreach ($routes as $route) { @@ -52,7 +52,7 @@ public function testLoadWithImport() $routeCollection = $loader->load('validresource.php'); $routes = $routeCollection->all(); - $this->assertCount(2, $routes, 'Two routes are loaded'); + $this->assertCount(1, $routes, 'One route is loaded'); $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); foreach ($routes as $route) { diff --git a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php index c488c43bcc91b..1b552d6d66aa3 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/XmlFileLoaderTest.php @@ -32,23 +32,36 @@ public function testLoadWithRoute() { $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); $routeCollection = $loader->load('validpattern.xml'); - $routes = $routeCollection->all(); + $route = $routeCollection->get('blog_show'); - $this->assertCount(3, $routes, 'Three routes are loaded'); - $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); + $this->assertInstanceOf('Symfony\Component\Routing\Route', $route); + $this->assertSame('/blog/{slug}', $route->getPath()); + $this->assertSame('{locale}.example.com', $route->getHost()); + $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); + $this->assertSame('\w+', $route->getRequirement('locale')); + $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); + $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); + $this->assertEquals(array('https'), $route->getSchemes()); + $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); + } - $identicalRoutes = array_slice($routes, 0, 2); - - foreach ($identicalRoutes as $route) { - $this->assertSame('/blog/{slug}', $route->getPath()); - $this->assertSame('{locale}.example.com', $route->getHost()); - $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); - $this->assertSame('\w+', $route->getRequirement('locale')); - $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); - $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); - $this->assertEquals(array('https'), $route->getSchemes()); - $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); - } + public function testLegacyRouteDefinitionLoading() + { + $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); + + $loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); + $routeCollection = $loader->load('legacy_validpattern.xml'); + $route = $routeCollection->get('blog_show_legacy'); + + $this->assertInstanceOf('Symfony\Component\Routing\Route', $route); + $this->assertSame('/blog/{slug}', $route->getPath()); + $this->assertSame('{locale}.example.com', $route->getHost()); + $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); + $this->assertSame('\w+', $route->getRequirement('locale')); + $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); + $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); + $this->assertEquals(array('https'), $route->getSchemes()); + $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); } public function testLoadWithNamespacePrefix() @@ -74,7 +87,7 @@ public function testLoadWithImport() $routeCollection = $loader->load('validresource.xml'); $routes = $routeCollection->all(); - $this->assertCount(3, $routes, 'Three routes are loaded'); + $this->assertCount(2, $routes, 'Two routes are loaded'); $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); foreach ($routes as $route) { diff --git a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php index 966768cf2ee45..c3365e7ee54eb 100644 --- a/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Routing/Tests/Loader/YamlFileLoaderTest.php @@ -66,23 +66,36 @@ public function testLoadWithRoute() { $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); $routeCollection = $loader->load('validpattern.yml'); - $routes = $routeCollection->all(); + $route = $routeCollection->get('blog_show'); - $this->assertCount(3, $routes, 'Three routes are loaded'); - $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); + $this->assertInstanceOf('Symfony\Component\Routing\Route', $route); + $this->assertSame('/blog/{slug}', $route->getPath()); + $this->assertSame('{locale}.example.com', $route->getHost()); + $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); + $this->assertSame('\w+', $route->getRequirement('locale')); + $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); + $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); + $this->assertEquals(array('https'), $route->getSchemes()); + $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); + } - $identicalRoutes = array_slice($routes, 0, 2); - - foreach ($identicalRoutes as $route) { - $this->assertSame('/blog/{slug}', $route->getPath()); - $this->assertSame('{locale}.example.com', $route->getHost()); - $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); - $this->assertSame('\w+', $route->getRequirement('locale')); - $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); - $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); - $this->assertEquals(array('https'), $route->getSchemes()); - $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); - } + public function testLegacyRouteDefinitionLoading() + { + $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); + + $loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures'))); + $routeCollection = $loader->load('legacy_validpattern.yml'); + $route = $routeCollection->get('blog_show_legacy'); + + $this->assertInstanceOf('Symfony\Component\Routing\Route', $route); + $this->assertSame('/blog/{slug}', $route->getPath()); + $this->assertSame('{locale}.example.com', $route->getHost()); + $this->assertSame('MyBundle:Blog:show', $route->getDefault('_controller')); + $this->assertSame('\w+', $route->getRequirement('locale')); + $this->assertSame('RouteCompiler', $route->getOption('compiler_class')); + $this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods()); + $this->assertEquals(array('https'), $route->getSchemes()); + $this->assertEquals('context.getMethod() == "GET"', $route->getCondition()); } public function testLoadWithResource() @@ -91,7 +104,7 @@ public function testLoadWithResource() $routeCollection = $loader->load('validresource.yml'); $routes = $routeCollection->all(); - $this->assertCount(3, $routes, 'Three routes are loaded'); + $this->assertCount(2, $routes, 'Two routes are loaded'); $this->assertContainsOnly('Symfony\Component\Routing\Route', $routes); foreach ($routes as $route) { diff --git a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/LegacyApacheMatcherDumperTest.php b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/LegacyApacheMatcherDumperTest.php index 1cdb3d3a5c00c..f7335023b869e 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/LegacyApacheMatcherDumperTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/LegacyApacheMatcherDumperTest.php @@ -84,13 +84,21 @@ private function getRouteCollection() $collection->add('bar', new Route( '/bar/{foo}', array(), - array('_method' => 'GET|head') + array(), + array(), + '', + array(), + array('GET', 'head') )); // method requirement (again) $collection->add('baragain', new Route( '/baragain/{foo}', array(), - array('_method' => 'get|post') + array(), + array(), + '', + array(), + array('get', 'post') )); // simple $collection->add('baz', new Route( @@ -112,13 +120,21 @@ private function getRouteCollection() $collection->add('baz5', new Route( '/test/{foo}/', array(), - array('_method' => 'get') + array(), + array(), + '', + array(), + array('GET') )); // trailing slash and unsafe method $collection->add('baz5unsafe', new Route( '/testunsafe/{foo}/', array(), - array('_method' => 'post') + array(), + array(), + '', + array(), + array('post') )); // complex $collection->add('baz6', new Route( diff --git a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php index 7cf529c345c55..257187733b2d3 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/Dumper/PhpMatcherDumperTest.php @@ -26,7 +26,10 @@ public function testDumpWhenSchemeIsUsedWithoutAProperDumper() $collection->add('secure', new Route( '/secure', array(), - array('_scheme' => 'https') + array(), + array(), + '', + array('https') )); $dumper = new PhpMatcherDumper($collection); $dumper->dump(); @@ -61,13 +64,21 @@ public function getRouteCollections() $collection->add('bar', new Route( '/bar/{foo}', array(), - array('_method' => 'GET|head') + array(), + array(), + '', + array(), + array('GET', 'head') )); // GET method requirement automatically adds HEAD as valid $collection->add('barhead', new Route( '/barhead/{foo}', array(), - array('_method' => 'GET') + array(), + array(), + '', + array(), + array('GET') )); // simple $collection->add('baz', new Route( @@ -89,13 +100,21 @@ public function getRouteCollections() $collection->add('baz5', new Route( '/test/{foo}/', array(), - array('_method' => 'post') + array(), + array(), + '', + array(), + array('post') )); // complex name $collection->add('baz.baz6', new Route( '/test/{foo}/', array(), - array('_method' => 'put') + array(), + array(), + '', + array(), + array('put') )); // defaults without variable $collection->add('foofoo', new Route( @@ -235,14 +254,20 @@ public function getRouteCollections() $redirectCollection->add('secure', new Route( '/secure', array(), - array('_scheme' => 'https') + array(), + array(), + '', + array('https') )); // force HTTP redirection $redirectCollection->add('nonsecure', new Route( '/nonsecure', array(), - array('_scheme' => 'http') + array(), + array(), + '', + array('http') )); /* test case 3 */ diff --git a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php index 5cbb605479117..b6c5a3e62218f 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/RedirectableUrlMatcherTest.php @@ -41,21 +41,6 @@ public function testRedirectWhenNoSlashForNonSafeMethod() $matcher->match('/foo'); } - public function testSchemeRedirectBC() - { - $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); - - $matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext())); - $matcher - ->expects($this->once()) - ->method('redirect') - ->with('/foo', 'foo', 'https') - ->will($this->returnValue(array('_route' => 'foo'))) - ; - $matcher->match('/foo'); - } - public function testSchemeRedirectRedirectsToFirstScheme() { $coll = new RouteCollection(); diff --git a/src/Symfony/Component/Routing/Tests/Matcher/TraceableUrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/TraceableUrlMatcherTest.php index 969ab0a2f0d00..20b30d7b91264 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/TraceableUrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/TraceableUrlMatcherTest.php @@ -21,9 +21,9 @@ class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase public function test() { $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_method' => 'POST'))); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('POST'))); $coll->add('bar', new Route('/bar/{id}', array(), array('id' => '\d+'))); - $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+', '_method' => 'POST'))); + $coll->add('bar1', new Route('/bar/{name}', array(), array('id' => '\w+'), array(), '', array(), array('POST'))); $coll->add('bar2', new Route('/foo', array(), array(), array(), 'baz')); $coll->add('bar3', new Route('/foo1', array(), array(), array(), 'baz')); $coll->add('bar4', new Route('/foo2', array(), array(), array(), 'baz', array(), array(), 'context.getMethod() == "GET"')); @@ -33,29 +33,29 @@ public function test() $matcher = new TraceableUrlMatcher($coll, $context); $traces = $matcher->getTraces('/babar'); - $this->assertEquals(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces)); + $this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces)); $traces = $matcher->getTraces('/foo'); - $this->assertEquals(array(1, 0, 0, 2), $this->getLevels($traces)); + $this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces)); $traces = $matcher->getTraces('/bar/12'); - $this->assertEquals(array(0, 2), $this->getLevels($traces)); + $this->assertSame(array(0, 2), $this->getLevels($traces)); $traces = $matcher->getTraces('/bar/dd'); - $this->assertEquals(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces)); + $this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces)); $traces = $matcher->getTraces('/foo1'); - $this->assertEquals(array(0, 0, 0, 0, 2), $this->getLevels($traces)); + $this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces)); $context->setMethod('POST'); $traces = $matcher->getTraces('/foo'); - $this->assertEquals(array(2), $this->getLevels($traces)); + $this->assertSame(array(2), $this->getLevels($traces)); $traces = $matcher->getTraces('/bar/dd'); - $this->assertEquals(array(0, 1, 2), $this->getLevels($traces)); + $this->assertSame(array(0, 1, 2), $this->getLevels($traces)); $traces = $matcher->getTraces('/foo2'); - $this->assertEquals(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces)); + $this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces)); } public function testMatchRouteOnMultipleHosts() @@ -83,7 +83,7 @@ public function testMatchRouteOnMultipleHosts() $matcher = new TraceableUrlMatcher($routes, $context); $traces = $matcher->getTraces('/mypath/'); - $this->assertEquals( + $this->assertSame( array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES), $this->getLevels($traces) ); diff --git a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php index 1e10367e0c0c6..11e939a77a789 100644 --- a/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php +++ b/src/Symfony/Component/Routing/Tests/Matcher/UrlMatcherTest.php @@ -26,13 +26,13 @@ public function testNoMethodSoAllowed() $coll->add('foo', new Route('/foo')); $matcher = new UrlMatcher($coll, new RequestContext()); - $matcher->match('/foo'); + $this->assertInternalType('array', $matcher->match('/foo')); } public function testMethodNotAllowed() { $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_method' => 'post'))); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post'))); $matcher = new UrlMatcher($coll, new RequestContext()); @@ -47,17 +47,17 @@ public function testMethodNotAllowed() public function testHeadAllowedWhenRequirementContainsGet() { $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_method' => 'get'))); + $coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get'))); $matcher = new UrlMatcher($coll, new RequestContext('', 'head')); - $matcher->match('/foo'); + $this->assertInternalType('array', $matcher->match('/foo')); } public function testMethodNotAllowedAggregatesAllowedMethods() { $coll = new RouteCollection(); - $coll->add('foo1', new Route('/foo', array(), array('_method' => 'post'))); - $coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete'))); + $coll->add('foo1', new Route('/foo', array(), array(), array(), '', array(), array('post'))); + $coll->add('foo2', new Route('/foo', array(), array(), array(), '', array(), array('put', 'delete'))); $matcher = new UrlMatcher($coll, new RequestContext()); @@ -90,7 +90,7 @@ public function testMatch() // test that route "method" is ignored if no method is given in the context $collection = new RouteCollection(); - $collection->add('foo', new Route('/foo', array(), array('_method' => 'GET|head'))); + $collection->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get', 'head'))); $matcher = new UrlMatcher($collection, new RequestContext()); $this->assertInternalType('array', $matcher->match('/foo')); @@ -312,16 +312,6 @@ public function testDefaultRequirementOfVariableDisallowsNextSeparator() $matcher->match('/do.t.html'); } - /** - * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException - */ - public function testSchemeRequirementBC() - { - $coll = new RouteCollection(); - $coll->add('foo', new Route('/foo', array(), array('_scheme' => 'https'))); - $matcher = new UrlMatcher($coll, new RequestContext()); - $matcher->match('/foo'); - } /** * @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException */ diff --git a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php index ad2d0e0f2f2a9..376b8db76fe4a 100644 --- a/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteCollectionTest.php @@ -164,12 +164,12 @@ public function testAddPrefix() public function testAddPrefixOverridesDefaultsAndRequirements() { $collection = new RouteCollection(); - $collection->add('foo', $foo = new Route('/foo')); - $collection->add('bar', $bar = new Route('/bar', array(), array('_scheme' => 'http'))); - $collection->addPrefix('/admin', array(), array('_scheme' => 'https')); + $collection->add('foo', $foo = new Route('/foo.{_format}')); + $collection->add('bar', $bar = new Route('/bar.{_format}', array(), array('_format' => 'json'))); + $collection->addPrefix('/admin', array(), array('_format' => 'html')); - $this->assertEquals('https', $collection->get('foo')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements'); - $this->assertEquals('https', $collection->get('bar')->getRequirement('_scheme'), '->addPrefix() overrides existing requirements'); + $this->assertEquals('html', $collection->get('foo')->getRequirement('_format'), '->addPrefix() overrides existing requirements'); + $this->assertEquals('html', $collection->get('bar')->getRequirement('_format'), '->addPrefix() overrides existing requirements'); } public function testResource() diff --git a/src/Symfony/Component/Routing/Tests/RouteTest.php b/src/Symfony/Component/Routing/Tests/RouteTest.php index 02ca4f4590c8c..3d72637795ccb 100644 --- a/src/Symfony/Component/Routing/Tests/RouteTest.php +++ b/src/Symfony/Component/Routing/Tests/RouteTest.php @@ -164,8 +164,10 @@ public function testScheme() $this->assertTrue($route->hasScheme('httpS')); } - public function testSchemeIsBC() + public function testLegacySchemeRequirement() { + $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); + $route = new Route('/'); $route->setRequirement('_scheme', 'http|https'); $this->assertEquals('http|https', $route->getRequirement('_scheme')); @@ -189,8 +191,10 @@ public function testMethod() $this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them'); } - public function testMethodIsBC() + public function testLegacyMethodRequirement() { + $this->iniSet('error_reporting', -1 & ~E_USER_DEPRECATED); + $route = new Route('/'); $route->setRequirement('_method', 'GET|POST'); $this->assertEquals('GET|POST', $route->getRequirement('_method'));