Update to drupal-org-drupal 8.0.0-rc2. For more information, see https://www.drupal.org/node/2598668

This commit is contained in:
Pantheon Automation 2015-10-21 21:44:50 -07:00 committed by Greg Anderson
parent f32e58e4b1
commit 8e18df8c36
3062 changed files with 15044 additions and 172506 deletions

View file

@ -1,58 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Annotation;
use Symfony\Component\Routing\Annotation\Route;
class RouteTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \BadMethodCallException
*/
public function testInvalidRouteParameter()
{
$route = new Route(array('foo' => 'bar'));
}
/**
* @dataProvider getValidParameters
*/
public function testRouteParameters($parameter, $value, $getter)
{
$route = new Route(array($parameter => $value));
$this->assertEquals($route->$getter(), $value);
}
public function getValidParameters()
{
return array(
array('value', '/Blog', 'getPath'),
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', '{locale}.example.com', 'getHost'),
array('condition', 'context.getMethod() == "GET"', 'getCondition'),
);
}
/**
* @group legacy
*/
public function testLegacyGetPattern()
{
$route = new Route(array('value' => '/Blog'));
$this->assertEquals($route->getPattern(), '/Blog');
}
}

View file

@ -1,26 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\Routing\CompiledRoute;
class CompiledRouteTest extends \PHPUnit_Framework_TestCase
{
public function testAccessors()
{
$compiled = new CompiledRoute('prefix', 'regex', array('tokens'), array(), array(), array(), array(), array('variables'));
$this->assertEquals('prefix', $compiled->getStaticPrefix(), '__construct() takes a static prefix as its second argument');
$this->assertEquals('regex', $compiled->getRegex(), '__construct() takes a regexp as its third argument');
$this->assertEquals(array('tokens'), $compiled->getTokens(), '__construct() takes an array of tokens as its fourth argument');
$this->assertEquals(array('variables'), $compiled->getVariables(), '__construct() takes an array of variables as its ninth argument');
}
}

View file

@ -1,16 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
abstract class AbstractClass
{
}

View file

@ -1,19 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
class BarClass
{
public function routeAction($arg1, $arg2 = 'defaultValue2', $arg3 = 'defaultValue3')
{
}
}

View file

@ -1,16 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses;
class FooClass
{
}

View file

@ -1,26 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures;
use Symfony\Component\Routing\Loader\XmlFileLoader;
use Symfony\Component\Config\Util\XmlUtils;
/**
* XmlFileLoader with schema validation turned off.
*/
class CustomXmlFileLoader extends XmlFileLoader
{
protected function loadFile($file)
{
return XmlUtils::loadFile($file, function () { return true; });
}
}

View file

@ -1,19 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures\OtherAnnotatedClasses;
class VariadicClass
{
public function routeAction(...$params)
{
}
}

View file

@ -1,30 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Fixtures;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Matcher\RedirectableUrlMatcherInterface;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class RedirectableUrlMatcher extends UrlMatcher implements RedirectableUrlMatcherInterface
{
public function redirect($path, $route, $scheme = null)
{
return array(
'_controller' => 'Some controller reference...',
'path' => $path,
'scheme' => $scheme,
);
}
}

View file

@ -1,3 +0,0 @@
blog_show:
path: /blog/{slug}
defaults: { _controller: "MyBundle:Blog:show" }

View file

@ -1,163 +0,0 @@
# skip "real" requests
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [QSA,L]
# foo
RewriteCond %{REQUEST_URI} ^/foo/(baz|symfony)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:foo,E=_ROUTING_param_bar:%1,E=_ROUTING_default_def:test]
# foobar
RewriteCond %{REQUEST_URI} ^/foo(?:/([^/]++))?$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:foobar,E=_ROUTING_param_bar:%1,E=_ROUTING_default_bar:toto]
# bar
RewriteCond %{REQUEST_URI} ^/bar/([^/]++)$
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
RewriteRule .* - [S=1,E=_ROUTING_allow_GET:1,E=_ROUTING_allow_HEAD:1]
RewriteCond %{REQUEST_URI} ^/bar/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:bar,E=_ROUTING_param_foo:%1]
# baragain
RewriteCond %{REQUEST_URI} ^/baragain/([^/]++)$
RewriteCond %{REQUEST_METHOD} !^(GET|POST|HEAD)$ [NC]
RewriteRule .* - [S=1,E=_ROUTING_allow_GET:1,E=_ROUTING_allow_POST:1,E=_ROUTING_allow_HEAD:1]
RewriteCond %{REQUEST_URI} ^/baragain/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baragain,E=_ROUTING_param_foo:%1]
# baz
RewriteCond %{REQUEST_URI} ^/test/baz$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz]
# baz2
RewriteCond %{REQUEST_URI} ^/test/baz\.html$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz2]
# baz3
RewriteCond %{REQUEST_URI} ^/test/baz3$
RewriteRule .* $0/ [QSA,L,R=301]
RewriteCond %{REQUEST_URI} ^/test/baz3/$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz3]
# baz4
RewriteCond %{REQUEST_URI} ^/test/([^/]++)$
RewriteRule .* $0/ [QSA,L,R=301]
RewriteCond %{REQUEST_URI} ^/test/([^/]++)/$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz4,E=_ROUTING_param_foo:%1]
# baz5
RewriteCond %{REQUEST_URI} ^/test/([^/]++)/$
RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$ [NC]
RewriteRule .* - [S=2,E=_ROUTING_allow_GET:1,E=_ROUTING_allow_HEAD:1]
RewriteCond %{REQUEST_URI} ^/test/([^/]++)$
RewriteRule .* $0/ [QSA,L,R=301]
RewriteCond %{REQUEST_URI} ^/test/([^/]++)/$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz5,E=_ROUTING_param_foo:%1]
# baz5unsafe
RewriteCond %{REQUEST_URI} ^/testunsafe/([^/]++)/$
RewriteCond %{REQUEST_METHOD} !^(POST)$ [NC]
RewriteRule .* - [S=1,E=_ROUTING_allow_POST:1]
RewriteCond %{REQUEST_URI} ^/testunsafe/([^/]++)/$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz5unsafe,E=_ROUTING_param_foo:%1]
# baz6
RewriteCond %{REQUEST_URI} ^/test/baz$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz6,E=_ROUTING_default_foo:bar\ baz]
# baz7
RewriteCond %{REQUEST_URI} ^/te\ st/baz$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz7]
# baz8
RewriteCond %{REQUEST_URI} ^/te\\\ st/baz$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz8]
# baz9
RewriteCond %{REQUEST_URI} ^/test/(te\\\ st)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:baz9,E=_ROUTING_param_baz:%1]
RewriteCond %{HTTP:Host} ^a\.example\.com$
RewriteRule .? - [E=__ROUTING_host_1:1]
# route1
RewriteCond %{ENV:__ROUTING_host_1} =1
RewriteCond %{REQUEST_URI} ^/route1$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route1]
# route2
RewriteCond %{ENV:__ROUTING_host_1} =1
RewriteCond %{REQUEST_URI} ^/c2/route2$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route2]
RewriteCond %{HTTP:Host} ^b\.example\.com$
RewriteRule .? - [E=__ROUTING_host_2:1]
# route3
RewriteCond %{ENV:__ROUTING_host_2} =1
RewriteCond %{REQUEST_URI} ^/c2/route3$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route3]
RewriteCond %{HTTP:Host} ^a\.example\.com$
RewriteRule .? - [E=__ROUTING_host_3:1]
# route4
RewriteCond %{ENV:__ROUTING_host_3} =1
RewriteCond %{REQUEST_URI} ^/route4$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route4]
RewriteCond %{HTTP:Host} ^c\.example\.com$
RewriteRule .? - [E=__ROUTING_host_4:1]
# route5
RewriteCond %{ENV:__ROUTING_host_4} =1
RewriteCond %{REQUEST_URI} ^/route5$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route5]
# route6
RewriteCond %{REQUEST_URI} ^/route6$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route6]
RewriteCond %{HTTP:Host} ^([^\.]++)\.example\.com$
RewriteRule .? - [E=__ROUTING_host_5:1,E=__ROUTING_host_5_var1:%1]
# route11
RewriteCond %{ENV:__ROUTING_host_5} =1
RewriteCond %{REQUEST_URI} ^/route11$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route11,E=_ROUTING_param_var1:%{ENV:__ROUTING_host_5_var1}]
# route12
RewriteCond %{ENV:__ROUTING_host_5} =1
RewriteCond %{REQUEST_URI} ^/route12$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route12,E=_ROUTING_param_var1:%{ENV:__ROUTING_host_5_var1},E=_ROUTING_default_var1:val]
# route13
RewriteCond %{ENV:__ROUTING_host_5} =1
RewriteCond %{REQUEST_URI} ^/route13/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route13,E=_ROUTING_param_var1:%{ENV:__ROUTING_host_5_var1},E=_ROUTING_param_name:%1]
# route14
RewriteCond %{ENV:__ROUTING_host_5} =1
RewriteCond %{REQUEST_URI} ^/route14/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route14,E=_ROUTING_param_var1:%{ENV:__ROUTING_host_5_var1},E=_ROUTING_param_name:%1,E=_ROUTING_default_var1:val]
RewriteCond %{HTTP:Host} ^c\.example\.com$
RewriteRule .? - [E=__ROUTING_host_6:1]
# route15
RewriteCond %{ENV:__ROUTING_host_6} =1
RewriteCond %{REQUEST_URI} ^/route15/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route15,E=_ROUTING_param_name:%1]
# route16
RewriteCond %{REQUEST_URI} ^/route16/([^/]++)$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route16,E=_ROUTING_param_name:%1,E=_ROUTING_default_var1:val]
# route17
RewriteCond %{REQUEST_URI} ^/route17$
RewriteRule .* app.php [QSA,L,E=_ROUTING_route:route17]
# 405 Method Not Allowed
RewriteCond %{ENV:_ROUTING__allow_GET} =1 [OR]
RewriteCond %{ENV:_ROUTING__allow_HEAD} =1 [OR]
RewriteCond %{ENV:_ROUTING__allow_POST} =1
RewriteRule .* app.php [QSA,L]

View file

@ -1,312 +0,0 @@
<?php
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* ProjectUrlMatcher.
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
{
/**
* Constructor.
*/
public function __construct(RequestContext $context)
{
$this->context = $context;
}
public function match($pathinfo)
{
$allow = array();
$pathinfo = rawurldecode($pathinfo);
$context = $this->context;
$request = $this->request;
// foo
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',));
}
if (0 === strpos($pathinfo, '/bar')) {
// bar
if (preg_match('#^/bar/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_bar;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ());
}
not_bar:
// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_barhead;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
}
not_barhead:
}
if (0 === strpos($pathinfo, '/test')) {
if (0 === strpos($pathinfo, '/test/baz')) {
// baz
if ($pathinfo === '/test/baz') {
return array('_route' => 'baz');
}
// baz2
if ($pathinfo === '/test/baz.html') {
return array('_route' => 'baz2');
}
// baz3
if ($pathinfo === '/test/baz3/') {
return array('_route' => 'baz3');
}
}
// baz4
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
}
// baz5
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'POST') {
$allow[] = 'POST';
goto not_baz5;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
}
not_baz5:
// baz.baz6
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'PUT') {
$allow[] = 'PUT';
goto not_bazbaz6;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
}
not_bazbaz6:
}
// foofoo
if ($pathinfo === '/foofoo') {
return array ( 'def' => 'test', '_route' => 'foofoo',);
}
// quoter
if (preg_match('#^/(?P<quoter>[\']+)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ());
}
// space
if ($pathinfo === '/spa ce') {
return array('_route' => 'space');
}
if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo1
if (preg_match('#^/a/b\'b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ());
}
// bar1
if (preg_match('#^/a/b\'b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ());
}
}
// overridden
if (preg_match('#^/a/(?P<var>.*)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ());
}
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo2
if (preg_match('#^/a/b\'b/(?P<foo1>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ());
}
// bar2
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ());
}
}
}
if (0 === strpos($pathinfo, '/multi')) {
// helloWorld
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?P<who>[^/]++))?$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
}
// overridden2
if ($pathinfo === '/multi/new') {
return array('_route' => 'overridden2');
}
// hey
if ($pathinfo === '/multi/hey/') {
return array('_route' => 'hey');
}
}
// foo3
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ());
}
// bar3
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ());
}
if (0 === strpos($pathinfo, '/aba')) {
// ababa
if ($pathinfo === '/ababa') {
return array('_route' => 'ababa');
}
// foo4
if (preg_match('#^/aba/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ());
}
}
$host = $this->context->getHost();
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
// route1
if ($pathinfo === '/route1') {
return array('_route' => 'route1');
}
// route2
if ($pathinfo === '/c2/route2') {
return array('_route' => 'route2');
}
}
if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) {
// route3
if ($pathinfo === '/c2/route3') {
return array('_route' => 'route3');
}
}
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
// route4
if ($pathinfo === '/route4') {
return array('_route' => 'route4');
}
}
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
// route5
if ($pathinfo === '/route5') {
return array('_route' => 'route5');
}
}
// route6
if ($pathinfo === '/route6') {
return array('_route' => 'route6');
}
if (preg_match('#^(?P<var1>[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) {
if (0 === strpos($pathinfo, '/route1')) {
// route11
if ($pathinfo === '/route11') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ());
}
// route12
if ($pathinfo === '/route12') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
}
// route13
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ());
}
// route14
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
}
}
}
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
// route15
if (0 === strpos($pathinfo, '/route15') && preg_match('#^/route15/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ());
}
}
if (0 === strpos($pathinfo, '/route1')) {
// route16
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
}
// route17
if ($pathinfo === '/route17') {
return array('_route' => 'route17');
}
}
if (0 === strpos($pathinfo, '/a')) {
// a
if ($pathinfo === '/a/a...') {
return array('_route' => 'a');
}
if (0 === strpos($pathinfo, '/a/b')) {
// b
if (preg_match('#^/a/b/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ());
}
// c
if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ());
}
}
}
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}
}

View file

@ -1,7 +0,0 @@
# skip "real" requests
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [QSA,L]
# foo
RewriteCond %{REQUEST_URI} ^/foo$
RewriteRule .* ap\ p_d\ ev.php [QSA,L,E=_ROUTING_route:foo]

View file

@ -1,344 +0,0 @@
<?php
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* ProjectUrlMatcher.
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher
{
/**
* Constructor.
*/
public function __construct(RequestContext $context)
{
$this->context = $context;
}
public function match($pathinfo)
{
$allow = array();
$pathinfo = rawurldecode($pathinfo);
$context = $this->context;
$request = $this->request;
// foo
if (0 === strpos($pathinfo, '/foo') && preg_match('#^/foo/(?P<bar>baz|symfony)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo')), array ( 'def' => 'test',));
}
if (0 === strpos($pathinfo, '/bar')) {
// bar
if (preg_match('#^/bar/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_bar;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar')), array ());
}
not_bar:
// barhead
if (0 === strpos($pathinfo, '/barhead') && preg_match('#^/barhead/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
if (!in_array($this->context->getMethod(), array('GET', 'HEAD'))) {
$allow = array_merge($allow, array('GET', 'HEAD'));
goto not_barhead;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'barhead')), array ());
}
not_barhead:
}
if (0 === strpos($pathinfo, '/test')) {
if (0 === strpos($pathinfo, '/test/baz')) {
// baz
if ($pathinfo === '/test/baz') {
return array('_route' => 'baz');
}
// baz2
if ($pathinfo === '/test/baz.html') {
return array('_route' => 'baz2');
}
// baz3
if (rtrim($pathinfo, '/') === '/test/baz3') {
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'baz3');
}
return array('_route' => 'baz3');
}
}
// baz4
if (preg_match('#^/test/(?P<foo>[^/]++)/?$#s', $pathinfo, $matches)) {
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'baz4');
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz4')), array ());
}
// baz5
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'POST') {
$allow[] = 'POST';
goto not_baz5;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz5')), array ());
}
not_baz5:
// baz.baz6
if (preg_match('#^/test/(?P<foo>[^/]++)/$#s', $pathinfo, $matches)) {
if ($this->context->getMethod() != 'PUT') {
$allow[] = 'PUT';
goto not_bazbaz6;
}
return $this->mergeDefaults(array_replace($matches, array('_route' => 'baz.baz6')), array ());
}
not_bazbaz6:
}
// foofoo
if ($pathinfo === '/foofoo') {
return array ( 'def' => 'test', '_route' => 'foofoo',);
}
// quoter
if (preg_match('#^/(?P<quoter>[\']+)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'quoter')), array ());
}
// space
if ($pathinfo === '/spa ce') {
return array('_route' => 'space');
}
if (0 === strpos($pathinfo, '/a')) {
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo1
if (preg_match('#^/a/b\'b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo1')), array ());
}
// bar1
if (preg_match('#^/a/b\'b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar1')), array ());
}
}
// overridden
if (preg_match('#^/a/(?P<var>.*)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'overridden')), array ());
}
if (0 === strpos($pathinfo, '/a/b\'b')) {
// foo2
if (preg_match('#^/a/b\'b/(?P<foo1>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo2')), array ());
}
// bar2
if (preg_match('#^/a/b\'b/(?P<bar1>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar2')), array ());
}
}
}
if (0 === strpos($pathinfo, '/multi')) {
// helloWorld
if (0 === strpos($pathinfo, '/multi/hello') && preg_match('#^/multi/hello(?:/(?P<who>[^/]++))?$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'helloWorld')), array ( 'who' => 'World!',));
}
// overridden2
if ($pathinfo === '/multi/new') {
return array('_route' => 'overridden2');
}
// hey
if (rtrim($pathinfo, '/') === '/multi/hey') {
if (substr($pathinfo, -1) !== '/') {
return $this->redirect($pathinfo.'/', 'hey');
}
return array('_route' => 'hey');
}
}
// foo3
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo3')), array ());
}
// bar3
if (preg_match('#^/(?P<_locale>[^/]++)/b/(?P<bar>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'bar3')), array ());
}
if (0 === strpos($pathinfo, '/aba')) {
// ababa
if ($pathinfo === '/ababa') {
return array('_route' => 'ababa');
}
// foo4
if (preg_match('#^/aba/(?P<foo>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'foo4')), array ());
}
}
$host = $this->context->getHost();
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
// route1
if ($pathinfo === '/route1') {
return array('_route' => 'route1');
}
// route2
if ($pathinfo === '/c2/route2') {
return array('_route' => 'route2');
}
}
if (preg_match('#^b\\.example\\.com$#si', $host, $hostMatches)) {
// route3
if ($pathinfo === '/c2/route3') {
return array('_route' => 'route3');
}
}
if (preg_match('#^a\\.example\\.com$#si', $host, $hostMatches)) {
// route4
if ($pathinfo === '/route4') {
return array('_route' => 'route4');
}
}
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
// route5
if ($pathinfo === '/route5') {
return array('_route' => 'route5');
}
}
// route6
if ($pathinfo === '/route6') {
return array('_route' => 'route6');
}
if (preg_match('#^(?P<var1>[^\\.]++)\\.example\\.com$#si', $host, $hostMatches)) {
if (0 === strpos($pathinfo, '/route1')) {
// route11
if ($pathinfo === '/route11') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route11')), array ());
}
// route12
if ($pathinfo === '/route12') {
return $this->mergeDefaults(array_replace($hostMatches, array('_route' => 'route12')), array ( 'var1' => 'val',));
}
// route13
if (0 === strpos($pathinfo, '/route13') && preg_match('#^/route13/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route13')), array ());
}
// route14
if (0 === strpos($pathinfo, '/route14') && preg_match('#^/route14/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($hostMatches, $matches, array('_route' => 'route14')), array ( 'var1' => 'val',));
}
}
}
if (preg_match('#^c\\.example\\.com$#si', $host, $hostMatches)) {
// route15
if (0 === strpos($pathinfo, '/route15') && preg_match('#^/route15/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route15')), array ());
}
}
if (0 === strpos($pathinfo, '/route1')) {
// route16
if (0 === strpos($pathinfo, '/route16') && preg_match('#^/route16/(?P<name>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'route16')), array ( 'var1' => 'val',));
}
// route17
if ($pathinfo === '/route17') {
return array('_route' => 'route17');
}
}
if (0 === strpos($pathinfo, '/a')) {
// a
if ($pathinfo === '/a/a...') {
return array('_route' => 'a');
}
if (0 === strpos($pathinfo, '/a/b')) {
// b
if (preg_match('#^/a/b/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'b')), array ());
}
// c
if (0 === strpos($pathinfo, '/a/b/c') && preg_match('#^/a/b/c/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'c')), array ());
}
}
}
// secure
if ($pathinfo === '/secure') {
$requiredSchemes = array ( 'https' => 0,);
if (!isset($requiredSchemes[$this->context->getScheme()])) {
return $this->redirect($pathinfo, 'secure', key($requiredSchemes));
}
return array('_route' => 'secure');
}
// nonsecure
if ($pathinfo === '/nonsecure') {
$requiredSchemes = array ( 'http' => 0,);
if (!isset($requiredSchemes[$this->context->getScheme()])) {
return $this->redirect($pathinfo, 'nonsecure', key($requiredSchemes));
}
return array('_route' => 'nonsecure');
}
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}
}

View file

@ -1,50 +0,0 @@
<?php
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\RequestContext;
/**
* ProjectUrlMatcher.
*
* This class has been auto-generated
* by the Symfony Routing Component.
*/
class ProjectUrlMatcher extends Symfony\Component\Routing\Matcher\UrlMatcher
{
/**
* Constructor.
*/
public function __construct(RequestContext $context)
{
$this->context = $context;
}
public function match($pathinfo)
{
$allow = array();
$pathinfo = rawurldecode($pathinfo);
$context = $this->context;
$request = $this->request;
if (0 === strpos($pathinfo, '/rootprefix')) {
// static
if ($pathinfo === '/rootprefix/test') {
return array('_route' => 'static');
}
// dynamic
if (preg_match('#^/rootprefix/(?P<var>[^/]++)$#s', $pathinfo, $matches)) {
return $this->mergeDefaults(array_replace($matches, array('_route' => 'dynamic')), array ());
}
}
// with-condition
if ($pathinfo === '/with-condition' && ($context->getMethod() == "GET")) {
return array('_route' => 'with-condition');
}
throw 0 < count($allow) ? new MethodNotAllowedException(array_unique($allow)) : new ResourceNotFoundException();
}
}

View file

@ -1,2 +0,0 @@
blog_show:
defaults: { _controller: MyBlogBundle:Blog:show }

View file

@ -1,16 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show_legacy" pattern="/blog/{slug}" host="{locale}.example.com">
<default key="_controller">MyBundle:Blog:show</default>
<default key="slug" xsi:nil="true" />
<requirement key="_method">GET|POST|put|OpTiOnS</requirement>
<requirement key="_scheme">hTTps</requirement>
<requirement key="locale">\w+</requirement>
<option key="compiler_class">RouteCompiler</option>
<condition>context.getMethod() == "GET"</condition>
</route>
</routes>

View file

@ -1,8 +0,0 @@
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

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route path="/test"></route>
</routes>

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="myroute"></route>
</routes>

View file

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<r:routes xmlns:r="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<r:route id="blog_show" path="/blog/{slug}" host="{_locale}.example.com">
<r:default key="_controller">MyBundle:Blog:show</r:default>
<requirement xmlns="http://symfony.com/schema/routing" key="slug">\w+</requirement>
<r2:requirement xmlns:r2="http://symfony.com/schema/routing" key="_locale">en|fr|de</r2:requirement>
<r:option key="compiler_class">RouteCompiler</r:option>
</r:route>
</r:routes>

View file

@ -1,3 +0,0 @@
blog_show:
resource: validpattern.yml
path: /test

View file

@ -1,3 +0,0 @@
blog_show:
path: /blog/{slug}
type: custom

View file

@ -1,10 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" path="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<!-- </route> -->
</routes>

View file

@ -1 +0,0 @@
foo

View file

@ -1 +0,0 @@
route: string

View file

@ -1,3 +0,0 @@
someroute:
resource: path/to/some.yml
name_prefix: test_

View file

@ -1,8 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<foo>bar</foo>
</routes>

View file

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" path="/blog/{slug}">
<default key="_controller">MyBundle:Blog:show</default>
<option key="compiler_class">RouteCompiler</option>
<foo key="bar">baz</foo>
</route>
</routes>

View file

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" path="/blog/{slug}">
<default key="foo" xsi:nil="true" />
<default key="bar" xsi:nil="1" />
<default key="foobar" xsi:nil="false">foo</default>
<default key="baz" xsi:nil="0">bar</default>
</route>
</routes>

View file

@ -1,2 +0,0 @@
"#$péß^a|":
path: "true"

View file

@ -1,18 +0,0 @@
<?php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
$collection = new RouteCollection();
$collection->add('blog_show', new Route(
'/blog/{slug}',
array('_controller' => 'MyBlogBundle:Blog:show'),
array('locale' => '\w+'),
array('compiler_class' => 'RouteCompiler'),
'{locale}.example.com',
array('https'),
array('GET', 'POST', 'put', 'OpTiOnS'),
'context.getMethod() == "GET"'
));
return $collection;

View file

@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<route id="blog_show" path="/blog/{slug}" host="{locale}.example.com" methods="GET|POST put,OpTiOnS" schemes="hTTps">
<default key="_controller">MyBundle:Blog:show</default>
<requirement key="locale">\w+</requirement>
<option key="compiler_class">RouteCompiler</option>
<condition>context.getMethod() == "GET"</condition>
</route>
<route id="blog_show_inherited" path="/blog/{slug}" />
</routes>

View file

@ -1,13 +0,0 @@
blog_show:
path: /blog/{slug}
defaults: { _controller: "MyBundle:Blog:show" }
host: "{locale}.example.com"
requirements: { 'locale': '\w+' }
methods: ['GET','POST','put','OpTiOnS']
schemes: ['https']
condition: 'context.getMethod() == "GET"'
options:
compiler_class: RouteCompiler
blog_show_inherited:
path: /blog/{slug}

View file

@ -1,18 +0,0 @@
<?php
/** @var $loader \Symfony\Component\Routing\Loader\PhpFileLoader */
/** @var \Symfony\Component\Routing\RouteCollection $collection */
$collection = $loader->import('validpattern.php');
$collection->addDefaults(array(
'foo' => 123,
));
$collection->addRequirements(array(
'foo' => '\d+',
));
$collection->addOptions(array(
'foo' => 'bar',
));
$collection->setCondition('context.getMethod() == "POST"');
$collection->addPrefix('/prefix');
return $collection;

View file

@ -1,13 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
<import resource="validpattern.xml" prefix="/{foo}" host="">
<default key="foo">123</default>
<requirement key="foo">\d+</requirement>
<option key="foo">bar</option>
<condition>context.getMethod() == "POST"</condition>
</import>
</routes>

View file

@ -1,8 +0,0 @@
_blog:
resource: validpattern.yml
prefix: /{foo}
defaults: { 'foo': '123' }
requirements: { 'foo': '\d+' }
options: { 'foo': 'bar' }
host: ""
condition: 'context.getMethod() == "POST"'

View file

@ -1,5 +0,0 @@
<?php
$path = '/1/2/3';
return new \Symfony\Component\Routing\RouteCollection();

View file

@ -1,3 +0,0 @@
<?xml version="1.0"?>
<!DOCTYPE foo>
<foo></foo>

View file

@ -1,141 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Generator\Dumper;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
use Symfony\Component\Routing\RequestContext;
class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RouteCollection
*/
private $routeCollection;
/**
* @var PhpGeneratorDumper
*/
private $generatorDumper;
/**
* @var string
*/
private $testTmpFilepath;
protected function setUp()
{
parent::setUp();
$this->routeCollection = new RouteCollection();
$this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
$this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.php';
@unlink($this->testTmpFilepath);
}
protected function tearDown()
{
parent::tearDown();
@unlink($this->testTmpFilepath);
$this->routeCollection = null;
$this->generatorDumper = null;
$this->testTmpFilepath = null;
}
public function testDumpWithRoutes()
{
$this->routeCollection->add('Test', new Route('/testing/{foo}'));
$this->routeCollection->add('Test2', new Route('/testing2'));
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
include $this->testTmpFilepath;
$projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
$absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true);
$absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true);
$relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), false);
$relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), false);
$this->assertEquals($absoluteUrlWithParameter, 'http://localhost/app.php/testing/bar');
$this->assertEquals($absoluteUrlWithoutParameter, 'http://localhost/app.php/testing2');
$this->assertEquals($relativeUrlWithParameter, '/app.php/testing/bar');
$this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testDumpWithoutRoutes()
{
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
include $this->testTmpFilepath;
$projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
$projectUrlGenerator->generate('Test', array());
}
/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGenerateNonExistingRoute()
{
$this->routeCollection->add('Test', new Route('/test'));
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
include $this->testTmpFilepath;
$projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
$url = $projectUrlGenerator->generate('NonExisting', array());
}
public function testDumpForRouteWithDefaults()
{
$this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
include $this->testTmpFilepath;
$projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
$url = $projectUrlGenerator->generate('Test', array());
$this->assertEquals($url, '/testing');
}
public function testDumpWithSchemeRequirement()
{
$this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator')));
include $this->testTmpFilepath;
$projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
$absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true);
$relativeUrl = $projectUrlGenerator->generate('Test1', array(), false);
$this->assertEquals($absoluteUrl, 'ftp://localhost/app.php/testing');
$this->assertEquals($relativeUrl, 'ftp://localhost/app.php/testing');
$projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
$absoluteUrl = $projectUrlGenerator->generate('Test1', array(), true);
$relativeUrl = $projectUrlGenerator->generate('Test1', array(), false);
$this->assertEquals($absoluteUrl, 'https://localhost/app.php/testing');
$this->assertEquals($relativeUrl, '/app.php/testing');
}
}

View file

@ -1,665 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Generator;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Generator\UrlGenerator;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RequestContext;
class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testAbsoluteUrlWithPort80()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes)->generate('test', array(), true);
$this->assertEquals('http://localhost/app.php/testing', $url);
}
public function testAbsoluteSecureUrlWithPort443()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', array(), true);
$this->assertEquals('https://localhost/app.php/testing', $url);
}
public function testAbsoluteUrlWithNonStandardPort()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes, array('httpPort' => 8080))->generate('test', array(), true);
$this->assertEquals('http://localhost:8080/app.php/testing', $url);
}
public function testAbsoluteSecureUrlWithNonStandardPort()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes, array('httpsPort' => 8080, 'scheme' => 'https'))->generate('test', array(), true);
$this->assertEquals('https://localhost:8080/app.php/testing', $url);
}
public function testRelativeUrlWithoutParameters()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes)->generate('test', array(), false);
$this->assertEquals('/app.php/testing', $url);
}
public function testRelativeUrlWithParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
$this->assertEquals('/app.php/testing/bar', $url);
}
public function testRelativeUrlWithNullParameter()
{
$routes = $this->getRoutes('test', new Route('/testing.{format}', array('format' => null)));
$url = $this->getGenerator($routes)->generate('test', array(), false);
$this->assertEquals('/app.php/testing', $url);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testRelativeUrlWithNullParameterButNotOptional()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
// This must raise an exception because the default requirement for "foo" is "[^/]+" which is not met with these params.
// Generating path "/testing//bar" would be wrong as matching this route would fail.
$this->getGenerator($routes)->generate('test', array(), false);
}
public function testRelativeUrlWithOptionalZeroParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{page}'));
$url = $this->getGenerator($routes)->generate('test', array('page' => 0), false);
$this->assertEquals('/app.php/testing/0', $url);
}
public function testNotPassedOptionalParameterInBetween()
{
$routes = $this->getRoutes('test', new Route('/{slug}/{page}', array('slug' => 'index', 'page' => 0)));
$this->assertSame('/app.php/index/1', $this->getGenerator($routes)->generate('test', array('page' => 1)));
$this->assertSame('/app.php/', $this->getGenerator($routes)->generate('test'));
}
public function testRelativeUrlWithExtraParameters()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
$this->assertEquals('/app.php/testing?foo=bar', $url);
}
public function testAbsoluteUrlWithExtraParameters()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
}
public function testUrlWithNullExtraParameters()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$url = $this->getGenerator($routes)->generate('test', array('foo' => null), true);
$this->assertEquals('http://localhost/app.php/testing', $url);
}
public function testUrlWithExtraParametersFromGlobals()
{
$routes = $this->getRoutes('test', new Route('/testing'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('bar', 'bar');
$generator->setContext($context);
$url = $generator->generate('test', array('foo' => 'bar'));
$this->assertEquals('/app.php/testing?foo=bar', $url);
}
public function testUrlWithGlobalParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('foo', 'bar');
$generator->setContext($context);
$url = $generator->generate('test', array());
$this->assertEquals('/app.php/testing/bar', $url);
}
public function testGlobalParameterHasHigherPriorityThanDefault()
{
$routes = $this->getRoutes('test', new Route('/{_locale}', array('_locale' => 'en')));
$generator = $this->getGenerator($routes);
$context = new RequestContext('/app.php');
$context->setParameter('_locale', 'de');
$generator->setContext($context);
$url = $generator->generate('test', array());
$this->assertSame('/app.php/de', $url);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGenerateWithoutRoutes()
{
$routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
$this->getGenerator($routes)->generate('test', array(), true);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\MissingMandatoryParametersException
*/
public function testGenerateForRouteWithoutMandatoryParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$this->getGenerator($routes)->generate('test', array(), true);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testGenerateForRouteWithInvalidOptionalParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testGenerateForRouteWithInvalidParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => '1|2')));
$this->getGenerator($routes)->generate('test', array('foo' => '0'), true);
}
public function testGenerateForRouteWithInvalidOptionalParameterNonStrict()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', array('foo' => 'bar'), true));
}
public function testGenerateForRouteWithInvalidOptionalParameterNonStrictWithLogger()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger->expects($this->once())
->method('error');
$generator = $this->getGenerator($routes, array(), $logger);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', array('foo' => 'bar'), true));
}
public function testGenerateForRouteWithInvalidParameterButDisabledRequirementsCheck()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(null);
$this->assertSame('/app.php/testing/bar', $generator->generate('test', array('foo' => 'bar')));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testGenerateForRouteWithInvalidMandatoryParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testRequiredParamAndEmptyPassed()
{
$routes = $this->getRoutes('test', new Route('/{slug}', array(), array('slug' => '.+')));
$this->getGenerator($routes)->generate('test', array('slug' => ''));
}
public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
{
$routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http')));
$this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
$routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https')));
$this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
}
public function testSchemeRequirementForcesAbsoluteUrl()
{
$routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('https')));
$this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
$routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('http')));
$this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
}
public function testSchemeRequirementCreatesUrlForFirstRequiredScheme()
{
$routes = $this->getRoutes('test', new Route('/', array(), array(), array(), '', array('Ftp', 'https')));
$this->assertEquals('ftp://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
}
public function testPathWithTwoStartingSlashes()
{
$routes = $this->getRoutes('test', new Route('//path-and-not-domain'));
// this must not generate '//path-and-not-domain' because that would be a network path
$this->assertSame('/path-and-not-domain', $this->getGenerator($routes, array('BaseUrl' => ''))->generate('test'));
}
public function testNoTrailingSlashForMultipleOptionalParameters()
{
$routes = $this->getRoutes('test', new Route('/category/{slug1}/{slug2}/{slug3}', array('slug2' => null, 'slug3' => null)));
$this->assertEquals('/app.php/category/foo', $this->getGenerator($routes)->generate('test', array('slug1' => 'foo')));
}
public function testWithAnIntegerAsADefaultValue()
{
$routes = $this->getRoutes('test', new Route('/{default}', array('default' => 0)));
$this->assertEquals('/app.php/foo', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
}
public function testNullForOptionalParameterIsIgnored()
{
$routes = $this->getRoutes('test', new Route('/test/{default}', array('default' => 0)));
$this->assertEquals('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => null)));
}
public function testQueryParamSameAsDefault()
{
$routes = $this->getRoutes('test', new Route('/test', array('default' => 'value')));
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'foo')));
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test', array('default' => 'value')));
$this->assertSame('/app.php/test', $this->getGenerator($routes)->generate('test'));
}
public function testGenerateWithSpecialRouteName()
{
$routes = $this->getRoutes('$péß^a|', new Route('/bar'));
$this->assertSame('/app.php/bar', $this->getGenerator($routes)->generate('$péß^a|'));
}
public function testUrlEncoding()
{
// This tests the encoding of reserved characters that are used for delimiting of URI components (defined in RFC 3986)
// and other special ASCII chars. These chars are tested as static text path, variable path and query param.
$chars = '@:[]/()*\'" +,;-._~&$<>|{}%\\^`!?foo=bar#id';
$routes = $this->getRoutes('test', new Route("/$chars/{varpath}", array(), array('varpath' => '.+')));
$this->assertSame('/app.php/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
.'/@:%5B%5D/%28%29*%27%22%20+,;-._~%26%24%3C%3E|%7B%7D%25%5C%5E%60!%3Ffoo=bar%23id'
.'?query=%40%3A%5B%5D/%28%29%2A%27%22+%2B%2C%3B-._%7E%26%24%3C%3E%7C%7B%7D%25%5C%5E%60%21%3Ffoo%3Dbar%23id',
$this->getGenerator($routes)->generate('test', array(
'varpath' => $chars,
'query' => $chars,
))
);
}
public function testEncodingOfRelativePathSegments()
{
$routes = $this->getRoutes('test', new Route('/dir/../dir/..'));
$this->assertSame('/app.php/dir/%2E%2E/dir/%2E%2E', $this->getGenerator($routes)->generate('test'));
$routes = $this->getRoutes('test', new Route('/dir/./dir/.'));
$this->assertSame('/app.php/dir/%2E/dir/%2E', $this->getGenerator($routes)->generate('test'));
$routes = $this->getRoutes('test', new Route('/a./.a/a../..a/...'));
$this->assertSame('/app.php/a./.a/a../..a/...', $this->getGenerator($routes)->generate('test'));
}
public function testAdjacentVariables()
{
$routes = $this->getRoutes('test', new Route('/{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '\d+')));
$generator = $this->getGenerator($routes);
$this->assertSame('/app.php/foo123', $generator->generate('test', array('x' => 'foo', 'y' => '123')));
$this->assertSame('/app.php/foo123bar.xml', $generator->generate('test', array('x' => 'foo', 'y' => '123', 'z' => 'bar', '_format' => 'xml')));
// The default requirement for 'x' should not allow the separator '.' in this case because it would otherwise match everything
// and following optional variables like _format could never match.
$this->setExpectedException('Symfony\Component\Routing\Exception\InvalidParameterException');
$generator->generate('test', array('x' => 'do.t', 'y' => '123', 'z' => 'bar', '_format' => 'xml'));
}
public function testOptionalVariableWithNoRealSeparator()
{
$routes = $this->getRoutes('test', new Route('/get{what}', array('what' => 'All')));
$generator = $this->getGenerator($routes);
$this->assertSame('/app.php/get', $generator->generate('test'));
$this->assertSame('/app.php/getSites', $generator->generate('test', array('what' => 'Sites')));
}
public function testRequiredVariableWithNoRealSeparator()
{
$routes = $this->getRoutes('test', new Route('/get{what}Suffix'));
$generator = $this->getGenerator($routes);
$this->assertSame('/app.php/getSitesSuffix', $generator->generate('test', array('what' => 'Sites')));
}
public function testDefaultRequirementOfVariable()
{
$routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
$generator = $this->getGenerator($routes);
$this->assertSame('/app.php/index.mobile.html', $generator->generate('test', array('page' => 'index', '_format' => 'mobile.html')));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testDefaultRequirementOfVariableDisallowsSlash()
{
$routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
$this->getGenerator($routes)->generate('test', array('page' => 'index', '_format' => 'sl/ash'));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testDefaultRequirementOfVariableDisallowsNextSeparator()
{
$routes = $this->getRoutes('test', new Route('/{page}.{_format}'));
$this->getGenerator($routes)->generate('test', array('page' => 'do.t', '_format' => 'html'));
}
public function testWithHostDifferentFromContext()
{
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
$this->assertEquals('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test', array('name' => 'Fabien', 'locale' => 'fr')));
}
public function testWithHostSameAsContext()
{
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
$this->assertEquals('/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' => 'Fabien', 'locale' => 'fr')));
}
public function testWithHostSameAsContextAndAbsolute()
{
$routes = $this->getRoutes('test', new Route('/{name}', array(), array(), array(), '{locale}.example.com'));
$this->assertEquals('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test', array('name' => 'Fabien', 'locale' => 'fr'), true));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterInHost()
{
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterInHostWhenParamHasADefaultValue()
{
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'bar'), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
/**
* @expectedException \Symfony\Component\Routing\Exception\InvalidParameterException
*/
public function testUrlWithInvalidParameterEqualsDefaultValueInHost()
{
$routes = $this->getRoutes('test', new Route('/', array('foo' => 'baz'), array('foo' => 'bar'), array(), '{foo}.example.com'));
$this->getGenerator($routes)->generate('test', array('foo' => 'baz'), false);
}
public function testUrlWithInvalidParameterInHostInNonStrictMode()
{
$routes = $this->getRoutes('test', new Route('/', array(), array('foo' => 'bar'), array(), '{foo}.example.com'));
$generator = $this->getGenerator($routes);
$generator->setStrictRequirements(false);
$this->assertNull($generator->generate('test', array('foo' => 'baz'), false));
}
public function testHostIsCaseInsensitive()
{
$routes = $this->getRoutes('test', new Route('/', array(), array('locale' => 'en|de|fr'), array(), '{locale}.FooBar.com'));
$generator = $this->getGenerator($routes);
$this->assertSame('//EN.FooBar.com/app.php/', $generator->generate('test', array('locale' => 'EN'), UrlGeneratorInterface::NETWORK_PATH));
}
/**
* @group legacy
*/
public function testLegacyGenerateNetworkPath()
{
$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')));
$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 testGenerateRelativePath()
{
$routes = new RouteCollection();
$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('scheme', new Route('/{author}/blog', array(), array(), array(), '', array('https')));
$routes->add('unrelated', new Route('/about'));
$generator = $this->getGenerator($routes, array('host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/'));
$this->assertSame('comments', $generator->generate('comments',
array('author' => 'fabien', 'article' => 'symfony-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
);
$this->assertSame('comments?page=2', $generator->generate('comments',
array('author' => 'fabien', 'article' => 'symfony-is-great', 'page' => 2), UrlGeneratorInterface::RELATIVE_PATH)
);
$this->assertSame('../twig-is-great/', $generator->generate('article',
array('author' => 'fabien', 'article' => 'twig-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
);
$this->assertSame('../../bernhard/forms-are-great/', $generator->generate('article',
array('author' => 'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH)
);
$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/blog', $generator->generate('scheme',
array('author' => 'bernhard'), UrlGeneratorInterface::RELATIVE_PATH)
);
$this->assertSame('../../about', $generator->generate('unrelated',
array(), UrlGeneratorInterface::RELATIVE_PATH)
);
}
/**
* @dataProvider provideRelativePaths
*/
public function testGetRelativePath($sourcePath, $targetPath, $expectedPath)
{
$this->assertSame($expectedPath, UrlGenerator::getRelativePath($sourcePath, $targetPath));
}
public function provideRelativePaths()
{
return array(
array(
'/same/dir/',
'/same/dir/',
'',
),
array(
'/same/file',
'/same/file',
'',
),
array(
'/',
'/file',
'file',
),
array(
'/',
'/dir/file',
'dir/file',
),
array(
'/dir/file.html',
'/dir/different-file.html',
'different-file.html',
),
array(
'/same/dir/extra-file',
'/same/dir/',
'./',
),
array(
'/parent/dir/',
'/parent/',
'../',
),
array(
'/parent/dir/extra-file',
'/parent/',
'../',
),
array(
'/a/b/',
'/x/y/z/',
'../../x/y/z/',
),
array(
'/a/b/c/d/e',
'/a/c/d',
'../../../c/d',
),
array(
'/a/b/c//',
'/a/b/c/',
'../',
),
array(
'/a/b/c/',
'/a/b/c//',
'.//',
),
array(
'/root/a/b/c/',
'/root/x/b/c/',
'../../../x/b/c/',
),
array(
'/a/b/c/d/',
'/a',
'../../../../a',
),
array(
'/special-chars/sp%20ce/1€/mäh/e=mc²',
'/special-chars/sp%20ce/1€/<µ>/e=mc²',
'../<µ>/e=mc²',
),
array(
'not-rooted',
'dir/file',
'dir/file',
),
array(
'//dir/',
'',
'../../',
),
array(
'/dir/',
'/dir/file:with-colon',
'./file:with-colon',
),
array(
'/dir/',
'/dir/subdir/file:with-colon',
'subdir/file:with-colon',
),
array(
'/dir/',
'/dir/:subdir/',
'./:subdir/',
),
);
}
protected function getGenerator(RouteCollection $routes, array $parameters = array(), $logger = null)
{
$context = new RequestContext('/app.php');
foreach ($parameters as $key => $value) {
$method = 'set'.$key;
$context->$method($value);
}
$generator = new UrlGenerator($routes, $context, $logger);
return $generator;
}
protected function getRoutes($name, Route $route)
{
$routes = new RouteCollection();
$routes->add($name, $route);
return $routes;
}
}

View file

@ -1,31 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
abstract class AbstractAnnotationLoaderTest extends \PHPUnit_Framework_TestCase
{
public function getReader()
{
return $this->getMockBuilder('Doctrine\Common\Annotations\Reader')
->disableOriginalConstructor()
->getMock()
;
}
public function getClassLoader($reader)
{
return $this->getMockBuilder('Symfony\Component\Routing\Loader\AnnotationClassLoader')
->setConstructorArgs(array($reader))
->getMockForAbstractClass()
;
}
}

View file

@ -1,188 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Routing\Annotation\Route;
class AnnotationClassLoaderTest extends AbstractAnnotationLoaderTest
{
protected $loader;
private $reader;
protected function setUp()
{
parent::setUp();
$this->reader = $this->getReader();
$this->loader = $this->getClassLoader($this->reader);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadMissingClass()
{
$this->loader->load('MissingClass');
}
/**
* @expectedException \InvalidArgumentException
*/
public function testLoadAbstractClass()
{
$this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\AbstractClass');
}
/**
* @dataProvider provideTestSupportsChecksResource
*/
public function testSupportsChecksResource($resource, $expectedSupports)
{
$this->assertSame($expectedSupports, $this->loader->supports($resource), '->supports() returns true if the resource is loadable');
}
public function provideTestSupportsChecksResource()
{
return array(
array('class', true),
array('\fully\qualified\class\name', true),
array('namespaced\class\without\leading\slash', true),
array('ÿClassWithLegalSpecialCharacters', true),
array('5', false),
array('foo.foo', false),
array(null, false),
);
}
public function testSupportsChecksTypeIfSpecified()
{
$this->assertTrue($this->loader->supports('class', 'annotation'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports('class', 'foo'), '->supports() checks the resource type if specified');
}
public function getLoadTests()
{
return array(
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('name' => 'route1', 'path' => '/path'),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('defaults' => array('arg2' => 'foo'), 'requirements' => array('arg3' => '\w+')),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('options' => array('foo' => 'bar')),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('schemes' => array('https'), 'methods' => array('GET')),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
),
array(
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
array('condition' => 'context.getMethod() == "GET"'),
array('arg2' => 'defaultValue2', 'arg3' => 'defaultValue3'),
),
);
}
/**
* @dataProvider getLoadTests
*/
public function testLoad($className, $routeData = array(), $methodArgs = array())
{
$routeData = array_replace(array(
'name' => 'route',
'path' => '/',
'requirements' => array(),
'options' => array(),
'defaults' => array(),
'schemes' => array(),
'methods' => array(),
'condition' => '',
), $routeData);
$this->reader
->expects($this->once())
->method('getMethodAnnotations')
->will($this->returnValue(array($this->getAnnotatedRoute($routeData))))
;
$routeCollection = $this->loader->load($className);
$route = $routeCollection->get($routeData['name']);
$this->assertSame($routeData['path'], $route->getPath(), '->load preserves path annotation');
$this->assertCount(
count($routeData['requirements']),
array_intersect_assoc($routeData['requirements'], $route->getRequirements()),
'->load preserves requirements annotation'
);
$this->assertCount(
count($routeData['options']),
array_intersect_assoc($routeData['options'], $route->getOptions()),
'->load preserves options annotation'
);
$defaults = array_replace($methodArgs, $routeData['defaults']);
$this->assertCount(
count($defaults),
array_intersect_assoc($defaults, $route->getDefaults()),
'->load preserves defaults annotation and merges them with default arguments in method signature'
);
$this->assertEquals($routeData['schemes'], $route->getSchemes(), '->load preserves schemes annotation');
$this->assertEquals($routeData['methods'], $route->getMethods(), '->load preserves methods annotation');
$this->assertSame($routeData['condition'], $route->getCondition(), '->load preserves condition annotation');
}
public function testClassRouteLoad()
{
$classRouteData = array(
'path' => '/prefix',
'schemes' => array('https'),
'methods' => array('GET'),
);
$methodRouteData = array(
'name' => 'route1',
'path' => '/path',
'schemes' => array('http'),
'methods' => array('POST', 'PUT'),
);
$this->reader
->expects($this->once())
->method('getClassAnnotation')
->will($this->returnValue($this->getAnnotatedRoute($classRouteData)))
;
$this->reader
->expects($this->once())
->method('getMethodAnnotations')
->will($this->returnValue(array($this->getAnnotatedRoute($methodRouteData))))
;
$routeCollection = $this->loader->load('Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass');
$route = $routeCollection->get($methodRouteData['name']);
$this->assertSame($classRouteData['path'].$methodRouteData['path'], $route->getPath(), '->load concatenates class and method route path');
$this->assertEquals(array_merge($classRouteData['schemes'], $methodRouteData['schemes']), $route->getSchemes(), '->load merges class and method route schemes');
$this->assertEquals(array_merge($classRouteData['methods'], $methodRouteData['methods']), $route->getMethods(), '->load merges class and method route methods');
}
private function getAnnotatedRoute($data)
{
return new Route($data);
}
}

View file

@ -1,53 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
use Symfony\Component\Config\FileLocator;
class AnnotationDirectoryLoaderTest extends AbstractAnnotationLoaderTest
{
protected $loader;
protected $reader;
protected function setUp()
{
parent::setUp();
$this->reader = $this->getReader();
$this->loader = new AnnotationDirectoryLoader(new FileLocator(), $this->getClassLoader($this->reader));
}
public function testLoad()
{
$this->reader->expects($this->exactly(2))->method('getClassAnnotation');
$this->reader
->expects($this->any())
->method('getMethodAnnotations')
->will($this->returnValue(array()))
;
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
}
public function testSupports()
{
$fixturesDir = __DIR__.'/../Fixtures';
$this->assertTrue($this->loader->supports($fixturesDir), '->supports() returns true if the resource is loadable');
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
}
}

View file

@ -1,61 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Routing\Loader\AnnotationFileLoader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Annotation\Route;
class AnnotationFileLoaderTest extends AbstractAnnotationLoaderTest
{
protected $loader;
protected $reader;
protected function setUp()
{
parent::setUp();
$this->reader = $this->getReader();
$this->loader = new AnnotationFileLoader(new FileLocator(), $this->getClassLoader($this->reader));
}
public function testLoad()
{
$this->reader->expects($this->once())->method('getClassAnnotation');
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
}
/**
* @requires PHP 5.6
*/
public function testLoadVariadic()
{
$route = new Route(array('path' => '/path/to/{id}'));
$this->reader->expects($this->once())->method('getClassAnnotation');
$this->reader->expects($this->once())->method('getMethodAnnotations')
->will($this->returnValue(array($route)));
$this->loader->load(__DIR__.'/../Fixtures/OtherAnnotatedClasses/VariadicClass.php');
}
public function testSupports()
{
$fixture = __DIR__.'/../Fixtures/annotated.php';
$this->assertTrue($this->loader->supports($fixture), '->supports() returns true if the resource is loadable');
$this->assertFalse($this->loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($this->loader->supports($fixture, 'annotation'), '->supports() checks the resource type if specified');
$this->assertFalse($this->loader->supports($fixture, 'foo'), '->supports() checks the resource type if specified');
}
}

View file

@ -1,48 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Routing\Loader\ClosureLoader;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ClosureLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testSupports()
{
$loader = new ClosureLoader();
$closure = function () {};
$this->assertTrue($loader->supports($closure), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($loader->supports($closure, 'closure'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports($closure, 'foo'), '->supports() checks the resource type if specified');
}
public function testLoad()
{
$loader = new ClosureLoader();
$route = new Route('/');
$routes = $loader->load(function () use ($route) {
$routes = new RouteCollection();
$routes->add('foo', $route);
return $routes;
});
$this->assertEquals($route, $routes->get('foo'), '->load() loads a \Closure resource');
}
}

View file

@ -1,82 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\PhpFileLoader;
class PhpFileLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testSupports()
{
$loader = new PhpFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
$this->assertTrue($loader->supports('foo.php'), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($loader->supports('foo.php', 'php'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports('foo.php', 'foo'), '->supports() checks the resource type if specified');
}
public function testLoadWithRoute()
{
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.php');
$routes = $routeCollection->all();
$this->assertCount(1, $routes, 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
foreach ($routes as $route) {
$this->assertSame('/blog/{slug}', $route->getPath());
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
$this->assertSame('{locale}.example.com', $route->getHost());
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
$this->assertEquals(array('https'), $route->getSchemes());
}
}
public function testLoadWithImport()
{
$loader = new PhpFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.php');
$routes = $routeCollection->all();
$this->assertCount(1, $routes, 'One route is loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
foreach ($routes as $route) {
$this->assertSame('/prefix/blog/{slug}', $route->getPath());
$this->assertSame('MyBlogBundle:Blog:show', $route->getDefault('_controller'));
$this->assertSame('{locale}.example.com', $route->getHost());
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
$this->assertEquals(array('GET', 'POST', 'PUT', 'OPTIONS'), $route->getMethods());
$this->assertEquals(array('https'), $route->getSchemes());
}
}
public function testThatDefiningVariableInConfigFileHasNoSideEffects()
{
$locator = new FileLocator(array(__DIR__.'/../Fixtures'));
$loader = new PhpFileLoader($locator);
$routeCollection = $loader->load('with_define_path_variable.php');
$resources = $routeCollection->getResources();
$this->assertCount(1, $resources);
$this->assertContainsOnly('Symfony\Component\Config\Resource\ResourceInterface', $resources);
$fileResource = reset($resources);
$this->assertSame(
realpath($locator->locate('with_define_path_variable.php')),
(string) $fileResource
);
}
}

View file

@ -1,152 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\XmlFileLoader;
use Symfony\Component\Routing\Tests\Fixtures\CustomXmlFileLoader;
class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testSupports()
{
$loader = new XmlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
$this->assertTrue($loader->supports('foo.xml'), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($loader->supports('foo.xml', 'xml'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports('foo.xml', 'foo'), '->supports() checks the resource type if specified');
}
public function testLoadWithRoute()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.xml');
$route = $routeCollection->get('blog_show');
$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());
}
/**
* @group legacy
*/
public function testLegacyRouteDefinitionLoading()
{
$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()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('namespaceprefix.xml');
$this->assertCount(1, $routeCollection->all(), 'One route is loaded');
$route = $routeCollection->get('blog_show');
$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('slug'));
$this->assertSame('en|fr|de', $route->getRequirement('_locale'));
$this->assertNull($route->getDefault('slug'));
$this->assertSame('RouteCompiler', $route->getOption('compiler_class'));
}
public function testLoadWithImport()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.xml');
$routes = $routeCollection->all();
$this->assertCount(2, $routes, 'Two routes are loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
foreach ($routes as $route) {
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
$this->assertSame('123', $route->getDefault('foo'));
$this->assertSame('\d+', $route->getRequirement('foo'));
$this->assertSame('bar', $route->getOption('foo'));
$this->assertSame('', $route->getHost());
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
}
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider getPathsToInvalidFiles
*/
public function testLoadThrowsExceptionWithInvalidFile($filePath)
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load($filePath);
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider getPathsToInvalidFiles
*/
public function testLoadThrowsExceptionWithInvalidFileEvenWithoutSchemaValidation($filePath)
{
$loader = new CustomXmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load($filePath);
}
public function getPathsToInvalidFiles()
{
return array(array('nonvalidnode.xml'), array('nonvalidroute.xml'), array('nonvalid.xml'), array('missing_id.xml'), array('missing_path.xml'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Document types are not allowed.
*/
public function testDocTypeIsNotAllowed()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load('withdoctype.xml');
}
public function testNullValues()
{
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('null_values.xml');
$route = $routeCollection->get('blog_show');
$this->assertTrue($route->hasDefault('foo'));
$this->assertNull($route->getDefault('foo'));
$this->assertTrue($route->hasDefault('bar'));
$this->assertNull($route->getDefault('bar'));
$this->assertEquals('foo', $route->getDefault('foobar'));
$this->assertEquals('bar', $route->getDefault('baz'));
}
}

View file

@ -1,130 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Loader;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Routing\Loader\YamlFileLoader;
use Symfony\Component\Config\Resource\FileResource;
class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase
{
public function testSupports()
{
$loader = new YamlFileLoader($this->getMock('Symfony\Component\Config\FileLocator'));
$this->assertTrue($loader->supports('foo.yml'), '->supports() returns true if the resource is loadable');
$this->assertTrue($loader->supports('foo.yaml'), '->supports() returns true if the resource is loadable');
$this->assertFalse($loader->supports('foo.foo'), '->supports() returns true if the resource is loadable');
$this->assertTrue($loader->supports('foo.yml', 'yaml'), '->supports() checks the resource type if specified');
$this->assertTrue($loader->supports('foo.yaml', 'yaml'), '->supports() checks the resource type if specified');
$this->assertFalse($loader->supports('foo.yml', 'foo'), '->supports() checks the resource type if specified');
}
public function testLoadDoesNothingIfEmpty()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$collection = $loader->load('empty.yml');
$this->assertEquals(array(), $collection->all());
$this->assertEquals(array(new FileResource(realpath(__DIR__.'/../Fixtures/empty.yml'))), $collection->getResources());
}
/**
* @expectedException \InvalidArgumentException
* @dataProvider getPathsToInvalidFiles
*/
public function testLoadThrowsExceptionWithInvalidFile($filePath)
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$loader->load($filePath);
}
public function getPathsToInvalidFiles()
{
return array(
array('nonvalid.yml'),
array('nonvalid2.yml'),
array('incomplete.yml'),
array('nonvalidkeys.yml'),
array('nonesense_resource_plus_path.yml'),
array('nonesense_type_without_resource.yml'),
array('bad_format.yml'),
);
}
public function testLoadSpecialRouteName()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('special_route_name.yml');
$route = $routeCollection->get('#$péß^a|');
$this->assertInstanceOf('Symfony\Component\Routing\Route', $route);
$this->assertSame('/true', $route->getPath());
}
public function testLoadWithRoute()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validpattern.yml');
$route = $routeCollection->get('blog_show');
$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());
}
/**
* @group legacy
*/
public function testLegacyRouteDefinitionLoading()
{
$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()
{
$loader = new YamlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
$routeCollection = $loader->load('validresource.yml');
$routes = $routeCollection->all();
$this->assertCount(2, $routes, 'Two routes are loaded');
$this->assertContainsOnly('Symfony\Component\Routing\Route', $routes);
foreach ($routes as $route) {
$this->assertSame('/{foo}/blog/{slug}', $route->getPath());
$this->assertSame('123', $route->getDefault('foo'));
$this->assertSame('\d+', $route->getRequirement('foo'));
$this->assertSame('bar', $route->getOption('foo'));
$this->assertSame('', $route->getHost());
$this->assertSame('context.getMethod() == "POST"', $route->getCondition());
}
}
}

View file

@ -1,33 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
use Symfony\Component\Routing\Matcher\Dumper\DumperCollection;
class DumperCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testGetRoot()
{
$a = new DumperCollection();
$b = new DumperCollection();
$a->add($b);
$c = new DumperCollection();
$b->add($c);
$d = new DumperCollection();
$c->add($d);
$this->assertSame($a, $c->getRoot());
}
}

View file

@ -1,123 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\Matcher\Dumper\DumperPrefixCollection;
use Symfony\Component\Routing\Matcher\Dumper\DumperRoute;
use Symfony\Component\Routing\Matcher\Dumper\DumperCollection;
class DumperPrefixCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testAddPrefixRoute()
{
$coll = new DumperPrefixCollection();
$coll->setPrefix('');
$route = new DumperRoute('bar', new Route('/foo/bar'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('bar2', new Route('/foo/bar'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('qux', new Route('/foo/qux'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('bar3', new Route('/foo/bar'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('bar4', new Route(''));
$result = $coll->addPrefixRoute($route);
$expect = <<<'EOF'
|-coll /
| |-coll /f
| | |-coll /fo
| | | |-coll /foo
| | | | |-coll /foo/
| | | | | |-coll /foo/b
| | | | | | |-coll /foo/ba
| | | | | | | |-coll /foo/bar
| | | | | | | | |-route bar /foo/bar
| | | | | | | | |-route bar2 /foo/bar
| | | | | |-coll /foo/q
| | | | | | |-coll /foo/qu
| | | | | | | |-coll /foo/qux
| | | | | | | | |-route qux /foo/qux
| | | | | |-coll /foo/b
| | | | | | |-coll /foo/ba
| | | | | | | |-coll /foo/bar
| | | | | | | | |-route bar3 /foo/bar
| |-route bar4 /
EOF;
$this->assertSame($expect, $this->collectionToString($result->getRoot(), ' '));
}
public function testMergeSlashNodes()
{
$coll = new DumperPrefixCollection();
$coll->setPrefix('');
$route = new DumperRoute('bar', new Route('/foo/bar'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('bar2', new Route('/foo/bar'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('qux', new Route('/foo/qux'));
$coll = $coll->addPrefixRoute($route);
$route = new DumperRoute('bar3', new Route('/foo/bar'));
$result = $coll->addPrefixRoute($route);
$result->getRoot()->mergeSlashNodes();
$expect = <<<'EOF'
|-coll /f
| |-coll /fo
| | |-coll /foo
| | | |-coll /foo/b
| | | | |-coll /foo/ba
| | | | | |-coll /foo/bar
| | | | | | |-route bar /foo/bar
| | | | | | |-route bar2 /foo/bar
| | | |-coll /foo/q
| | | | |-coll /foo/qu
| | | | | |-coll /foo/qux
| | | | | | |-route qux /foo/qux
| | | |-coll /foo/b
| | | | |-coll /foo/ba
| | | | | |-coll /foo/bar
| | | | | | |-route bar3 /foo/bar
EOF;
$this->assertSame($expect, $this->collectionToString($result->getRoot(), ' '));
}
private function collectionToString(DumperCollection $collection, $prefix)
{
$string = '';
foreach ($collection as $route) {
if ($route instanceof DumperCollection) {
$string .= sprintf("%s|-coll %s\n", $prefix, $route->getPrefix());
$string .= $this->collectionToString($route, $prefix.'| ');
} else {
$string .= sprintf("%s|-route %s %s\n", $prefix, $route->getName(), $route->getRoute()->getPath());
}
}
return $string;
}
}

View file

@ -1,215 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Matcher\Dumper\ApacheMatcherDumper;
/**
* @group legacy
*/
class LegacyApacheMatcherDumperTest extends \PHPUnit_Framework_TestCase
{
protected static $fixturesPath;
public static function setUpBeforeClass()
{
self::$fixturesPath = realpath(__DIR__.'/../../Fixtures/');
}
public function testDump()
{
$dumper = new ApacheMatcherDumper($this->getRouteCollection());
$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher1.apache', $dumper->dump(), '->dump() dumps basic routes to the correct apache format.');
}
/**
* @dataProvider provideEscapeFixtures
*/
public function testEscapePattern($src, $dest, $char, $with, $message)
{
$r = new \ReflectionMethod(new ApacheMatcherDumper($this->getRouteCollection()), 'escape');
$r->setAccessible(true);
$this->assertEquals($dest, $r->invoke(null, $src, $char, $with), $message);
}
public function provideEscapeFixtures()
{
return array(
array('foo', 'foo', ' ', '-', 'Preserve string that should not be escaped'),
array('fo-o', 'fo-o', ' ', '-', 'Preserve string that should not be escaped'),
array('fo o', 'fo- o', ' ', '-', 'Escape special characters'),
array('fo-- o', 'fo--- o', ' ', '-', 'Escape special characters'),
array('fo- o', 'fo- o', ' ', '-', 'Do not escape already escaped string'),
);
}
public function testEscapeScriptName()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$dumper = new ApacheMatcherDumper($collection);
$this->assertStringEqualsFile(self::$fixturesPath.'/dumper/url_matcher2.apache', $dumper->dump(array('script_name' => 'ap p_d\ ev.php')));
}
private function getRouteCollection()
{
$collection = new RouteCollection();
// defaults and requirements
$collection->add('foo', new Route(
'/foo/{bar}',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
// defaults parameters in pattern
$collection->add('foobar', new Route(
'/foo/{bar}',
array('bar' => 'toto')
));
// method requirement
$collection->add('bar', new Route(
'/bar/{foo}',
array(),
array(),
array(),
'',
array(),
array('GET', 'head')
));
// method requirement (again)
$collection->add('baragain', new Route(
'/baragain/{foo}',
array(),
array(),
array(),
'',
array(),
array('get', 'post')
));
// simple
$collection->add('baz', new Route(
'/test/baz'
));
// simple with extension
$collection->add('baz2', new Route(
'/test/baz.html'
));
// trailing slash
$collection->add('baz3', new Route(
'/test/baz3/'
));
// trailing slash with variable
$collection->add('baz4', new Route(
'/test/{foo}/'
));
// trailing slash and safe method
$collection->add('baz5', new Route(
'/test/{foo}/',
array(),
array(),
array(),
'',
array(),
array('GET')
));
// trailing slash and unsafe method
$collection->add('baz5unsafe', new Route(
'/testunsafe/{foo}/',
array(),
array(),
array(),
'',
array(),
array('post')
));
// complex
$collection->add('baz6', new Route(
'/test/baz',
array('foo' => 'bar baz')
));
// space in path
$collection->add('baz7', new Route(
'/te st/baz'
));
// space preceded with \ in path
$collection->add('baz8', new Route(
'/te\\ st/baz'
));
// space preceded with \ in requirement
$collection->add('baz9', new Route(
'/test/{baz}',
array(),
array(
'baz' => 'te\\\\ st',
)
));
$collection1 = new RouteCollection();
$route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
$collection1->add('route1', $route1);
$collection2 = new RouteCollection();
$route2 = new Route('/route2', array(), array(), array(), 'a.example.com');
$collection2->add('route2', $route2);
$route3 = new Route('/route3', array(), array(), array(), 'b.example.com');
$collection2->add('route3', $route3);
$collection2->addPrefix('/c2');
$collection1->addCollection($collection2);
$route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
$collection1->add('route4', $route4);
$route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
$collection1->add('route5', $route5);
$route6 = new Route('/route6', array(), array(), array(), null);
$collection1->add('route6', $route6);
$collection->addCollection($collection1);
// host and variables
$collection1 = new RouteCollection();
$route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
$collection1->add('route11', $route11);
$route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
$collection1->add('route12', $route12);
$route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
$collection1->add('route13', $route13);
$route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
$collection1->add('route14', $route14);
$route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
$collection1->add('route15', $route15);
$route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
$collection1->add('route16', $route16);
$route17 = new Route('/route17', array(), array(), array(), null);
$collection1->add('route17', $route17);
$collection->addCollection($collection1);
return $collection;
}
}

View file

@ -1,289 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher\Dumper;
use Symfony\Component\Routing\Matcher\Dumper\PhpMatcherDumper;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class PhpMatcherDumperTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \LogicException
*/
public function testDumpWhenSchemeIsUsedWithoutAProperDumper()
{
$collection = new RouteCollection();
$collection->add('secure', new Route(
'/secure',
array(),
array(),
array(),
'',
array('https')
));
$dumper = new PhpMatcherDumper($collection);
$dumper->dump();
}
/**
* @dataProvider getRouteCollections
*/
public function testDump(RouteCollection $collection, $fixture, $options = array())
{
$basePath = __DIR__.'/../../Fixtures/dumper/';
$dumper = new PhpMatcherDumper($collection);
$this->assertStringEqualsFile($basePath.$fixture, $dumper->dump($options), '->dump() correctly dumps routes as optimized PHP code.');
}
public function getRouteCollections()
{
/* test case 1 */
$collection = new RouteCollection();
$collection->add('overridden', new Route('/overridden'));
// defaults and requirements
$collection->add('foo', new Route(
'/foo/{bar}',
array('def' => 'test'),
array('bar' => 'baz|symfony')
));
// method requirement
$collection->add('bar', new Route(
'/bar/{foo}',
array(),
array(),
array(),
'',
array(),
array('GET', 'head')
));
// GET method requirement automatically adds HEAD as valid
$collection->add('barhead', new Route(
'/barhead/{foo}',
array(),
array(),
array(),
'',
array(),
array('GET')
));
// simple
$collection->add('baz', new Route(
'/test/baz'
));
// simple with extension
$collection->add('baz2', new Route(
'/test/baz.html'
));
// trailing slash
$collection->add('baz3', new Route(
'/test/baz3/'
));
// trailing slash with variable
$collection->add('baz4', new Route(
'/test/{foo}/'
));
// trailing slash and method
$collection->add('baz5', new Route(
'/test/{foo}/',
array(),
array(),
array(),
'',
array(),
array('post')
));
// complex name
$collection->add('baz.baz6', new Route(
'/test/{foo}/',
array(),
array(),
array(),
'',
array(),
array('put')
));
// defaults without variable
$collection->add('foofoo', new Route(
'/foofoo',
array('def' => 'test')
));
// pattern with quotes
$collection->add('quoter', new Route(
'/{quoter}',
array(),
array('quoter' => '[\']+')
));
// space in pattern
$collection->add('space', new Route(
'/spa ce'
));
// prefixes
$collection1 = new RouteCollection();
$collection1->add('overridden', new Route('/overridden1'));
$collection1->add('foo1', new Route('/{foo}'));
$collection1->add('bar1', new Route('/{bar}'));
$collection1->addPrefix('/b\'b');
$collection2 = new RouteCollection();
$collection2->addCollection($collection1);
$collection2->add('overridden', new Route('/{var}', array(), array('var' => '.*')));
$collection1 = new RouteCollection();
$collection1->add('foo2', new Route('/{foo1}'));
$collection1->add('bar2', new Route('/{bar1}'));
$collection1->addPrefix('/b\'b');
$collection2->addCollection($collection1);
$collection2->addPrefix('/a');
$collection->addCollection($collection2);
// overridden through addCollection() and multiple sub-collections with no own prefix
$collection1 = new RouteCollection();
$collection1->add('overridden2', new Route('/old'));
$collection1->add('helloWorld', new Route('/hello/{who}', array('who' => 'World!')));
$collection2 = new RouteCollection();
$collection3 = new RouteCollection();
$collection3->add('overridden2', new Route('/new'));
$collection3->add('hey', new Route('/hey/'));
$collection2->addCollection($collection3);
$collection1->addCollection($collection2);
$collection1->addPrefix('/multi');
$collection->addCollection($collection1);
// "dynamic" prefix
$collection1 = new RouteCollection();
$collection1->add('foo3', new Route('/{foo}'));
$collection1->add('bar3', new Route('/{bar}'));
$collection1->addPrefix('/b');
$collection1->addPrefix('{_locale}');
$collection->addCollection($collection1);
// route between collections
$collection->add('ababa', new Route('/ababa'));
// collection with static prefix but only one route
$collection1 = new RouteCollection();
$collection1->add('foo4', new Route('/{foo}'));
$collection1->addPrefix('/aba');
$collection->addCollection($collection1);
// prefix and host
$collection1 = new RouteCollection();
$route1 = new Route('/route1', array(), array(), array(), 'a.example.com');
$collection1->add('route1', $route1);
$collection2 = new RouteCollection();
$route2 = new Route('/c2/route2', array(), array(), array(), 'a.example.com');
$collection1->add('route2', $route2);
$route3 = new Route('/c2/route3', array(), array(), array(), 'b.example.com');
$collection1->add('route3', $route3);
$route4 = new Route('/route4', array(), array(), array(), 'a.example.com');
$collection1->add('route4', $route4);
$route5 = new Route('/route5', array(), array(), array(), 'c.example.com');
$collection1->add('route5', $route5);
$route6 = new Route('/route6', array(), array(), array(), null);
$collection1->add('route6', $route6);
$collection->addCollection($collection1);
// host and variables
$collection1 = new RouteCollection();
$route11 = new Route('/route11', array(), array(), array(), '{var1}.example.com');
$collection1->add('route11', $route11);
$route12 = new Route('/route12', array('var1' => 'val'), array(), array(), '{var1}.example.com');
$collection1->add('route12', $route12);
$route13 = new Route('/route13/{name}', array(), array(), array(), '{var1}.example.com');
$collection1->add('route13', $route13);
$route14 = new Route('/route14/{name}', array('var1' => 'val'), array(), array(), '{var1}.example.com');
$collection1->add('route14', $route14);
$route15 = new Route('/route15/{name}', array(), array(), array(), 'c.example.com');
$collection1->add('route15', $route15);
$route16 = new Route('/route16/{name}', array('var1' => 'val'), array(), array(), null);
$collection1->add('route16', $route16);
$route17 = new Route('/route17', array(), array(), array(), null);
$collection1->add('route17', $route17);
$collection->addCollection($collection1);
// multiple sub-collections with a single route and a prefix each
$collection1 = new RouteCollection();
$collection1->add('a', new Route('/a...'));
$collection2 = new RouteCollection();
$collection2->add('b', new Route('/{var}'));
$collection3 = new RouteCollection();
$collection3->add('c', new Route('/{var}'));
$collection3->addPrefix('/c');
$collection2->addCollection($collection3);
$collection2->addPrefix('/b');
$collection1->addCollection($collection2);
$collection1->addPrefix('/a');
$collection->addCollection($collection1);
/* test case 2 */
$redirectCollection = clone $collection;
// force HTTPS redirection
$redirectCollection->add('secure', new Route(
'/secure',
array(),
array(),
array(),
'',
array('https')
));
// force HTTP redirection
$redirectCollection->add('nonsecure', new Route(
'/nonsecure',
array(),
array(),
array(),
'',
array('http')
));
/* test case 3 */
$rootprefixCollection = new RouteCollection();
$rootprefixCollection->add('static', new Route('/test'));
$rootprefixCollection->add('dynamic', new Route('/{var}'));
$rootprefixCollection->addPrefix('rootprefix');
$route = new Route('/with-condition');
$route->setCondition('context.getMethod() == "GET"');
$rootprefixCollection->add('with-condition', $route);
return array(
array($collection, 'url_matcher1.php', array()),
array($redirectCollection, 'url_matcher2.php', array('base_class' => 'Symfony\Component\Routing\Tests\Fixtures\RedirectableUrlMatcher')),
array($rootprefixCollection, 'url_matcher3.php', array()),
);
}
}

View file

@ -1,155 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\ApacheUrlMatcher;
/**
* @group legacy
*/
class LegacyApacheUrlMatcherTest extends \PHPUnit_Framework_TestCase
{
protected $server;
protected function setUp()
{
$this->server = $_SERVER;
}
protected function tearDown()
{
$_SERVER = $this->server;
}
/**
* @dataProvider getMatchData
*/
public function testMatch($name, $pathinfo, $server, $expect)
{
$collection = new RouteCollection();
$context = new RequestContext();
$matcher = new ApacheUrlMatcher($collection, $context);
$_SERVER = $server;
$result = $matcher->match($pathinfo);
$this->assertSame(var_export($expect, true), var_export($result, true));
}
public function getMatchData()
{
return array(
array(
'Simple route',
'/hello/world',
array(
'_ROUTING_route' => 'hello',
'_ROUTING_param__controller' => 'AcmeBundle:Default:index',
'_ROUTING_param_name' => 'world',
),
array(
'_controller' => 'AcmeBundle:Default:index',
'name' => 'world',
'_route' => 'hello',
),
),
array(
'Route with params and defaults',
'/hello/hugo',
array(
'_ROUTING_route' => 'hello',
'_ROUTING_param__controller' => 'AcmeBundle:Default:index',
'_ROUTING_param_name' => 'hugo',
'_ROUTING_default_name' => 'world',
),
array(
'name' => 'hugo',
'_controller' => 'AcmeBundle:Default:index',
'_route' => 'hello',
),
),
array(
'Route with defaults only',
'/hello',
array(
'_ROUTING_route' => 'hello',
'_ROUTING_param__controller' => 'AcmeBundle:Default:index',
'_ROUTING_default_name' => 'world',
),
array(
'name' => 'world',
'_controller' => 'AcmeBundle:Default:index',
'_route' => 'hello',
),
),
array(
'Redirect with many ignored attributes',
'/legacy/{cat1}/{cat2}/{id}.html',
array(
'_ROUTING_route' => 'product_view',
'_ROUTING_param__controller' => 'FrameworkBundle:Redirect:redirect',
'_ROUTING_default_ignoreAttributes[0]' => 'attr_a',
'_ROUTING_default_ignoreAttributes[1]' => 'attr_b',
),
array(
'ignoreAttributes' => array('attr_a', 'attr_b'),
'_controller' => 'FrameworkBundle:Redirect:redirect',
'_route' => 'product_view',
),
),
array(
'REDIRECT_ envs',
'/hello/world',
array(
'REDIRECT__ROUTING_route' => 'hello',
'REDIRECT__ROUTING_param__controller' => 'AcmeBundle:Default:index',
'REDIRECT__ROUTING_param_name' => 'world',
),
array(
'_controller' => 'AcmeBundle:Default:index',
'name' => 'world',
'_route' => 'hello',
),
),
array(
'REDIRECT_REDIRECT_ envs',
'/hello/world',
array(
'REDIRECT_REDIRECT__ROUTING_route' => 'hello',
'REDIRECT_REDIRECT__ROUTING_param__controller' => 'AcmeBundle:Default:index',
'REDIRECT_REDIRECT__ROUTING_param_name' => 'world',
),
array(
'_controller' => 'AcmeBundle:Default:index',
'name' => 'world',
'_route' => 'hello',
),
),
array(
'REDIRECT_REDIRECT_ envs',
'/hello/world',
array(
'REDIRECT_REDIRECT__ROUTING_route' => 'hello',
'REDIRECT_REDIRECT__ROUTING_param__controller' => 'AcmeBundle:Default:index',
'REDIRECT_REDIRECT__ROUTING_param_name' => 'world',
),
array(
'_controller' => 'AcmeBundle:Default:index',
'name' => 'world',
'_route' => 'hello',
),
),
);
}
}

View file

@ -1,71 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class RedirectableUrlMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testRedirectWhenNoSlash()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/'));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
$matcher->expects($this->once())->method('redirect');
$matcher->match('/foo');
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testRedirectWhenNoSlashForNonSafeMethod()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/'));
$context = new RequestContext();
$context->setMethod('POST');
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, $context));
$matcher->match('/foo');
}
public function testSchemeRedirectRedirectsToFirstScheme()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('FTP', 'HTTPS')));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
$matcher
->expects($this->once())
->method('redirect')
->with('/foo', 'foo', 'ftp')
->will($this->returnValue(array('_route' => 'foo')))
;
$matcher->match('/foo');
}
public function testNoSchemaRedirectIfOnOfMultipleSchemesMatches()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https', 'http')));
$matcher = $this->getMockForAbstractClass('Symfony\Component\Routing\Matcher\RedirectableUrlMatcher', array($coll, new RequestContext()));
$matcher
->expects($this->never())
->method('redirect')
;
$matcher->match('/foo');
}
}

View file

@ -1,101 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Matcher\TraceableUrlMatcher;
class TraceableUrlMatcherTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$coll = new RouteCollection();
$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+'), 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"'));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($coll, $context);
$traces = $matcher->getTraces('/babar');
$this->assertSame(array(0, 0, 0, 0, 0, 0), $this->getLevels($traces));
$traces = $matcher->getTraces('/foo');
$this->assertSame(array(1, 0, 0, 2), $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/12');
$this->assertSame(array(0, 2), $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/dd');
$this->assertSame(array(0, 1, 1, 0, 0, 0), $this->getLevels($traces));
$traces = $matcher->getTraces('/foo1');
$this->assertSame(array(0, 0, 0, 0, 2), $this->getLevels($traces));
$context->setMethod('POST');
$traces = $matcher->getTraces('/foo');
$this->assertSame(array(2), $this->getLevels($traces));
$traces = $matcher->getTraces('/bar/dd');
$this->assertSame(array(0, 1, 2), $this->getLevels($traces));
$traces = $matcher->getTraces('/foo2');
$this->assertSame(array(0, 0, 0, 0, 0, 1), $this->getLevels($traces));
}
public function testMatchRouteOnMultipleHosts()
{
$routes = new RouteCollection();
$routes->add('first', new Route(
'/mypath/',
array('_controller' => 'MainBundle:Info:first'),
array(),
array(),
'some.example.com'
));
$routes->add('second', new Route(
'/mypath/',
array('_controller' => 'MainBundle:Info:second'),
array(),
array(),
'another.example.com'
));
$context = new RequestContext();
$context->setHost('baz');
$matcher = new TraceableUrlMatcher($routes, $context);
$traces = $matcher->getTraces('/mypath/');
$this->assertSame(
array(TraceableUrlMatcher::ROUTE_ALMOST_MATCHES, TraceableUrlMatcher::ROUTE_ALMOST_MATCHES),
$this->getLevels($traces)
);
}
public function getLevels($traces)
{
$levels = array();
foreach ($traces as $trace) {
$levels[] = $trace['level'];
}
return $levels;
}
}

View file

@ -1,419 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests\Matcher;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Matcher\UrlMatcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RequestContext;
class UrlMatcherTest extends \PHPUnit_Framework_TestCase
{
public function testNoMethodSoAllowed()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo'));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertInternalType('array', $matcher->match('/foo'));
}
public function testMethodNotAllowed()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('post')));
$matcher = new UrlMatcher($coll, new RequestContext());
try {
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('POST'), $e->getAllowedMethods());
}
}
public function testHeadAllowedWhenRequirementContainsGet()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array(), array('get')));
$matcher = new UrlMatcher($coll, new RequestContext('', 'head'));
$this->assertInternalType('array', $matcher->match('/foo'));
}
public function testMethodNotAllowedAggregatesAllowedMethods()
{
$coll = new RouteCollection();
$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());
try {
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
$this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
}
}
public function testMatch()
{
// test the patterns are matched and parameters are returned
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}'));
$matcher = new UrlMatcher($collection, new RequestContext());
try {
$matcher->match('/no-match');
$this->fail();
} catch (ResourceNotFoundException $e) {
}
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz'), $matcher->match('/foo/baz'));
// test that defaults are merged
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo/{bar}', array('def' => 'test')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'foo', 'bar' => 'baz', 'def' => 'test'), $matcher->match('/foo/baz'));
// 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(), array(), '', array(), array('get', 'head')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertInternalType('array', $matcher->match('/foo'));
// route does not match with POST method context
$matcher = new UrlMatcher($collection, new RequestContext('', 'post'));
try {
$matcher->match('/foo');
$this->fail();
} catch (MethodNotAllowedException $e) {
}
// route does match with GET or HEAD method context
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertInternalType('array', $matcher->match('/foo'));
$matcher = new UrlMatcher($collection, new RequestContext('', 'head'));
$this->assertInternalType('array', $matcher->match('/foo'));
// route with an optional variable as the first segment
$collection = new RouteCollection();
$collection->add('bar', new Route('/{bar}/foo', array('bar' => 'bar'), array('bar' => 'foo|bar')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/bar/foo'));
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo/foo'));
$collection = new RouteCollection();
$collection->add('bar', new Route('/{bar}', array('bar' => 'bar'), array('bar' => 'foo|bar')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
// route with only optional variables
$collection = new RouteCollection();
$collection->add('bar', new Route('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar'), array()));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'bar', 'foo' => 'foo', 'bar' => 'bar'), $matcher->match('/'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'bar'), $matcher->match('/a'));
$this->assertEquals(array('_route' => 'bar', 'foo' => 'a', 'bar' => 'b'), $matcher->match('/a/b'));
}
public function testMatchWithPrefixes()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/{foo}'));
$collection->addPrefix('/b');
$collection->addPrefix('/a');
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
}
public function testMatchWithDynamicPrefix()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/{foo}'));
$collection->addPrefix('/b');
$collection->addPrefix('/{_locale}');
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
}
public function testMatchSpecialRouteName()
{
$collection = new RouteCollection();
$collection->add('$péß^a|', new Route('/bar'));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => '$péß^a|'), $matcher->match('/bar'));
}
public function testMatchNonAlpha()
{
$collection = new RouteCollection();
$chars = '!"$%éà &\'()*+,./:;<=>@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\[]^_`abcdefghijklmnopqrstuvwxyz{|}~-';
$collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '['.preg_quote($chars).']+')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.rawurlencode($chars).'/bar'));
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25')).'/bar'));
}
public function testMatchWithDotMetacharacterInRequirements()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/{foo}/bar', array(), array('foo' => '.+')));
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'foo', 'foo' => "\n"), $matcher->match('/'.urlencode("\n").'/bar'), 'linefeed character is matched');
}
public function testMatchOverriddenRoute()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/foo1'));
$collection->addCollection($collection1);
$matcher = new UrlMatcher($collection, new RequestContext());
$this->assertEquals(array('_route' => 'foo'), $matcher->match('/foo1'));
$this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
$this->assertEquals(array(), $matcher->match('/foo'));
}
public function testMatchRegression()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}'));
$coll->add('bar', new Route('/foo/bar/{foo}'));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar'), $matcher->match('/foo/bar/bar'));
$collection = new RouteCollection();
$collection->add('foo', new Route('/{bar}'));
$matcher = new UrlMatcher($collection, new RequestContext());
try {
$matcher->match('/');
$this->fail();
} catch (ResourceNotFoundException $e) {
}
}
public function testDefaultRequirementForOptionalVariables()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}', array('page' => 'index', '_format' => 'html')));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('page' => 'my-page', '_format' => 'xml', '_route' => 'test'), $matcher->match('/my-page.xml'));
}
public function testMatchingIsEager()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{foo}-{bar}-', array(), array('foo' => '.+', 'bar' => '.+')));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('foo' => 'text1-text2-text3', 'bar' => 'text4', '_route' => 'test'), $matcher->match('/text1-text2-text3-text4-'));
}
public function testAdjacentVariables()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => 'y|Y')));
$matcher = new UrlMatcher($coll, new RequestContext());
// 'w' eagerly matches as much as possible and the other variables match the remaining chars.
// This also shows that the variables w-z must all exclude the separating char (the dot '.' in this case) by default requirement.
// Otherwise they would also consume '.xml' and _format would never match as it's an optional variable.
$this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'Y', 'z' => 'Z', '_format' => 'xml', '_route' => 'test'), $matcher->match('/wwwwwxYZ.xml'));
// As 'y' has custom requirement and can only be of value 'y|Y', it will leave 'ZZZ' to variable z.
// So with carefully chosen requirements adjacent variables, can be useful.
$this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'ZZZ', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxyZZZ'));
// z and _format are optional.
$this->assertEquals(array('w' => 'wwwww', 'x' => 'x', 'y' => 'y', 'z' => 'default-z', '_format' => 'html', '_route' => 'test'), $matcher->match('/wwwwwxy'));
$this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
$matcher->match('/wxy.html');
}
public function testOptionalVariableWithNoRealSeparator()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/get{what}', array('what' => 'All')));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('what' => 'All', '_route' => 'test'), $matcher->match('/get'));
$this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSites'));
// Usually the character in front of an optional parameter can be left out, e.g. with pattern '/get/{what}' just '/get' would match.
// But here the 't' in 'get' is not a separating character, so it makes no sense to match without it.
$this->setExpectedException('Symfony\Component\Routing\Exception\ResourceNotFoundException');
$matcher->match('/ge');
}
public function testRequiredVariableWithNoRealSeparator()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/get{what}Suffix'));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('what' => 'Sites', '_route' => 'test'), $matcher->match('/getSitesSuffix'));
}
public function testDefaultRequirementOfVariable()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}'));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('page' => 'index', '_format' => 'mobile.html', '_route' => 'test'), $matcher->match('/index.mobile.html'));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testDefaultRequirementOfVariableDisallowsSlash()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}'));
$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/index.sl/ash');
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testDefaultRequirementOfVariableDisallowsNextSeparator()
{
$coll = new RouteCollection();
$coll->add('test', new Route('/{page}.{_format}', array(), array('_format' => 'html|xml')));
$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/do.t.html');
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testSchemeRequirement()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo', array(), array(), array(), '', array('https')));
$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/foo');
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testCondition()
{
$coll = new RouteCollection();
$route = new Route('/foo');
$route->setCondition('context.getMethod() == "POST"');
$coll->add('foo', $route);
$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/foo');
}
public function testDecodeOnce()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}'));
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('foo' => 'bar%23', '_route' => 'foo'), $matcher->match('/foo/bar%2523'));
}
public function testCannotRelyOnPrefix()
{
$coll = new RouteCollection();
$subColl = new RouteCollection();
$subColl->add('bar', new Route('/bar'));
$subColl->addPrefix('/prefix');
// overwrite the pattern, so the prefix is not valid anymore for this route in the collection
$subColl->get('bar')->setPath('/new');
$coll->addCollection($subColl);
$matcher = new UrlMatcher($coll, new RequestContext());
$this->assertEquals(array('_route' => 'bar'), $matcher->match('/new'));
}
public function testWithHost()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
}
public function testWithHostOnRouteCollection()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}'));
$coll->add('bar', new Route('/bar/{foo}', array(), array(), array(), '{locale}.example.net'));
$coll->setHost('{locale}.example.com');
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'foo', 'locale' => 'en'), $matcher->match('/foo/bar'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('foo' => 'bar', '_route' => 'bar', 'locale' => 'en'), $matcher->match('/bar/bar'));
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testWithOutHostHostDoesNotMatch()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/foo/{foo}', array(), array(), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'example.com'));
$matcher->match('/foo/bar');
}
/**
* @expectedException \Symfony\Component\Routing\Exception\ResourceNotFoundException
*/
public function testPathIsCaseSensitive()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/locale', array(), array('locale' => 'EN|FR|DE')));
$matcher = new UrlMatcher($coll, new RequestContext());
$matcher->match('/en');
}
public function testHostIsCaseInsensitive()
{
$coll = new RouteCollection();
$coll->add('foo', new Route('/', array(), array('locale' => 'EN|FR|DE'), array(), '{locale}.example.com'));
$matcher = new UrlMatcher($coll, new RequestContext('', 'GET', 'en.example.com'));
$this->assertEquals(array('_route' => 'foo', 'locale' => 'en'), $matcher->match('/'));
}
}

View file

@ -1,159 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RequestContext;
class RequestContextTest extends \PHPUnit_Framework_TestCase
{
public function testConstruct()
{
$requestContext = new RequestContext(
'foo',
'post',
'foo.bar',
'HTTPS',
8080,
444,
'/baz',
'bar=foobar'
);
$this->assertEquals('foo', $requestContext->getBaseUrl());
$this->assertEquals('POST', $requestContext->getMethod());
$this->assertEquals('foo.bar', $requestContext->getHost());
$this->assertEquals('https', $requestContext->getScheme());
$this->assertSame(8080, $requestContext->getHttpPort());
$this->assertSame(444, $requestContext->getHttpsPort());
$this->assertEquals('/baz', $requestContext->getPathInfo());
$this->assertEquals('bar=foobar', $requestContext->getQueryString());
}
public function testFromRequest()
{
$request = Request::create('https://test.com:444/foo?bar=baz');
$requestContext = new RequestContext();
$requestContext->setHttpPort(123);
$requestContext->fromRequest($request);
$this->assertEquals('', $requestContext->getBaseUrl());
$this->assertEquals('GET', $requestContext->getMethod());
$this->assertEquals('test.com', $requestContext->getHost());
$this->assertEquals('https', $requestContext->getScheme());
$this->assertEquals('/foo', $requestContext->getPathInfo());
$this->assertEquals('bar=baz', $requestContext->getQueryString());
$this->assertSame(123, $requestContext->getHttpPort());
$this->assertSame(444, $requestContext->getHttpsPort());
$request = Request::create('http://test.com:8080/foo?bar=baz');
$requestContext = new RequestContext();
$requestContext->setHttpsPort(567);
$requestContext->fromRequest($request);
$this->assertSame(8080, $requestContext->getHttpPort());
$this->assertSame(567, $requestContext->getHttpsPort());
}
public function testGetParameters()
{
$requestContext = new RequestContext();
$this->assertEquals(array(), $requestContext->getParameters());
$requestContext->setParameters(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $requestContext->getParameters());
}
public function testHasParameter()
{
$requestContext = new RequestContext();
$requestContext->setParameters(array('foo' => 'bar'));
$this->assertTrue($requestContext->hasParameter('foo'));
$this->assertFalse($requestContext->hasParameter('baz'));
}
public function testGetParameter()
{
$requestContext = new RequestContext();
$requestContext->setParameters(array('foo' => 'bar'));
$this->assertEquals('bar', $requestContext->getParameter('foo'));
$this->assertNull($requestContext->getParameter('baz'));
}
public function testSetParameter()
{
$requestContext = new RequestContext();
$requestContext->setParameter('foo', 'bar');
$this->assertEquals('bar', $requestContext->getParameter('foo'));
}
public function testMethod()
{
$requestContext = new RequestContext();
$requestContext->setMethod('post');
$this->assertSame('POST', $requestContext->getMethod());
}
public function testScheme()
{
$requestContext = new RequestContext();
$requestContext->setScheme('HTTPS');
$this->assertSame('https', $requestContext->getScheme());
}
public function testHost()
{
$requestContext = new RequestContext();
$requestContext->setHost('eXampLe.com');
$this->assertSame('example.com', $requestContext->getHost());
}
public function testQueryString()
{
$requestContext = new RequestContext();
$requestContext->setQueryString(null);
$this->assertSame('', $requestContext->getQueryString());
}
public function testPort()
{
$requestContext = new RequestContext();
$requestContext->setHttpPort('123');
$requestContext->setHttpsPort('456');
$this->assertSame(123, $requestContext->getHttpPort());
$this->assertSame(456, $requestContext->getHttpsPort());
}
public function testFluentInterface()
{
$requestContext = new RequestContext();
$this->assertSame($requestContext, $requestContext->setBaseUrl('/app.php'));
$this->assertSame($requestContext, $requestContext->setPathInfo('/index'));
$this->assertSame($requestContext, $requestContext->setMethod('POST'));
$this->assertSame($requestContext, $requestContext->setScheme('https'));
$this->assertSame($requestContext, $requestContext->setHost('example.com'));
$this->assertSame($requestContext, $requestContext->setQueryString('foo=bar'));
$this->assertSame($requestContext, $requestContext->setHttpPort(80));
$this->assertSame($requestContext, $requestContext->setHttpsPort(443));
$this->assertSame($requestContext, $requestContext->setParameters(array()));
$this->assertSame($requestContext, $requestContext->setParameter('foo', 'bar'));
}
}

View file

@ -1,304 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Config\Resource\FileResource;
class RouteCollectionTest extends \PHPUnit_Framework_TestCase
{
public function testRoute()
{
$collection = new RouteCollection();
$route = new Route('/foo');
$collection->add('foo', $route);
$this->assertEquals(array('foo' => $route), $collection->all(), '->add() adds a route');
$this->assertEquals($route, $collection->get('foo'), '->get() returns a route by name');
$this->assertNull($collection->get('bar'), '->get() returns null if a route does not exist');
}
public function testOverriddenRoute()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection->add('foo', new Route('/foo1'));
$this->assertEquals('/foo1', $collection->get('foo')->getPath());
}
public function testDeepOverriddenRoute()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/foo1'));
$collection2 = new RouteCollection();
$collection2->add('foo', new Route('/foo2'));
$collection1->addCollection($collection2);
$collection->addCollection($collection1);
$this->assertEquals('/foo2', $collection1->get('foo')->getPath());
$this->assertEquals('/foo2', $collection->get('foo')->getPath());
}
public function testIterator()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('bar', $bar = new Route('/bar'));
$collection1->add('foo', $foo = new Route('/foo-new'));
$collection->addCollection($collection1);
$collection->add('last', $last = new Route('/last'));
$this->assertInstanceOf('\ArrayIterator', $collection->getIterator());
$this->assertSame(array('bar' => $bar, 'foo' => $foo, 'last' => $last), $collection->getIterator()->getArrayCopy());
}
public function testCount()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('bar', new Route('/bar'));
$collection->addCollection($collection1);
$this->assertCount(2, $collection);
}
public function testAddCollection()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('bar', $bar = new Route('/bar'));
$collection1->add('foo', $foo = new Route('/foo-new'));
$collection2 = new RouteCollection();
$collection2->add('grandchild', $grandchild = new Route('/grandchild'));
$collection1->addCollection($collection2);
$collection->addCollection($collection1);
$collection->add('last', $last = new Route('/last'));
$this->assertSame(array('bar' => $bar, 'foo' => $foo, 'grandchild' => $grandchild, 'last' => $last), $collection->all(),
'->addCollection() imports routes of another collection, overrides if necessary and adds them at the end');
}
public function testAddCollectionWithResources()
{
$collection = new RouteCollection();
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
$collection1 = new RouteCollection();
$collection1->addResource($foo1 = new FileResource(__DIR__.'/Fixtures/foo1.xml'));
$collection->addCollection($collection1);
$this->assertEquals(array($foo, $foo1), $collection->getResources(), '->addCollection() merges resources');
}
public function testAddDefaultsAndRequirementsAndOptions()
{
$collection = new RouteCollection();
$collection->add('foo', new Route('/{placeholder}'));
$collection1 = new RouteCollection();
$collection1->add('bar', new Route('/{placeholder}',
array('_controller' => 'fixed', 'placeholder' => 'default'), array('placeholder' => '.+'), array('option' => 'value'))
);
$collection->addCollection($collection1);
$collection->addDefaults(array('placeholder' => 'new-default'));
$this->assertEquals(array('placeholder' => 'new-default'), $collection->get('foo')->getDefaults(), '->addDefaults() adds defaults to all routes');
$this->assertEquals(array('_controller' => 'fixed', 'placeholder' => 'new-default'), $collection->get('bar')->getDefaults(),
'->addDefaults() adds defaults to all routes and overwrites existing ones');
$collection->addRequirements(array('placeholder' => '\d+'));
$this->assertEquals(array('placeholder' => '\d+'), $collection->get('foo')->getRequirements(), '->addRequirements() adds requirements to all routes');
$this->assertEquals(array('placeholder' => '\d+'), $collection->get('bar')->getRequirements(),
'->addRequirements() adds requirements to all routes and overwrites existing ones');
$collection->addOptions(array('option' => 'new-value'));
$this->assertEquals(
array('option' => 'new-value', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'),
$collection->get('bar')->getOptions(), '->addOptions() adds options to all routes and overwrites existing ones'
);
}
public function testAddPrefix()
{
$collection = new RouteCollection();
$collection->add('foo', $foo = new Route('/foo'));
$collection2 = new RouteCollection();
$collection2->add('bar', $bar = new Route('/bar'));
$collection->addCollection($collection2);
$collection->addPrefix(' / ');
$this->assertSame('/foo', $collection->get('foo')->getPath(), '->addPrefix() trims the prefix and a single slash has no effect');
$collection->addPrefix('/{admin}', array('admin' => 'admin'), array('admin' => '\d+'));
$this->assertEquals('/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() adds a prefix to all routes');
$this->assertEquals('/{admin}/bar', $collection->get('bar')->getPath(), '->addPrefix() adds a prefix to all routes');
$this->assertEquals(array('admin' => 'admin'), $collection->get('foo')->getDefaults(), '->addPrefix() adds defaults to all routes');
$this->assertEquals(array('admin' => 'admin'), $collection->get('bar')->getDefaults(), '->addPrefix() adds defaults to all routes');
$this->assertEquals(array('admin' => '\d+'), $collection->get('foo')->getRequirements(), '->addPrefix() adds requirements to all routes');
$this->assertEquals(array('admin' => '\d+'), $collection->get('bar')->getRequirements(), '->addPrefix() adds requirements to all routes');
$collection->addPrefix('0');
$this->assertEquals('/0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() ensures a prefix must start with a slash and must not end with a slash');
$collection->addPrefix('/ /');
$this->assertSame('/ /0/{admin}/foo', $collection->get('foo')->getPath(), '->addPrefix() can handle spaces if desired');
$this->assertSame('/ /0/{admin}/bar', $collection->get('bar')->getPath(), 'the route pattern of an added collection is in synch with the added prefix');
}
public function testAddPrefixOverridesDefaultsAndRequirements()
{
$collection = new RouteCollection();
$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('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()
{
$collection = new RouteCollection();
$collection->addResource($foo = new FileResource(__DIR__.'/Fixtures/foo.xml'));
$collection->addResource($bar = new FileResource(__DIR__.'/Fixtures/bar.xml'));
$collection->addResource(new FileResource(__DIR__.'/Fixtures/foo.xml'));
$this->assertEquals(array($foo, $bar), $collection->getResources(),
'->addResource() adds a resource and getResources() only returns unique ones by comparing the string representation');
}
public function testUniqueRouteWithGivenName()
{
$collection1 = new RouteCollection();
$collection1->add('foo', new Route('/old'));
$collection2 = new RouteCollection();
$collection3 = new RouteCollection();
$collection3->add('foo', $new = new Route('/new'));
$collection2->addCollection($collection3);
$collection1->addCollection($collection2);
$this->assertSame($new, $collection1->get('foo'), '->get() returns new route that overrode previous one');
// size of 1 because collection1 contains /new but not /old anymore
$this->assertCount(1, $collection1->getIterator(), '->addCollection() removes previous routes when adding new routes with the same name');
}
public function testGet()
{
$collection1 = new RouteCollection();
$collection1->add('a', $a = new Route('/a'));
$collection2 = new RouteCollection();
$collection2->add('b', $b = new Route('/b'));
$collection1->addCollection($collection2);
$collection1->add('$péß^a|', $c = new Route('/special'));
$this->assertSame($b, $collection1->get('b'), '->get() returns correct route in child collection');
$this->assertSame($c, $collection1->get('$péß^a|'), '->get() can handle special characters');
$this->assertNull($collection2->get('a'), '->get() does not return the route defined in parent collection');
$this->assertNull($collection1->get('non-existent'), '->get() returns null when route does not exist');
$this->assertNull($collection1->get(0), '->get() does not disclose internal child RouteCollection');
}
public function testRemove()
{
$collection = new RouteCollection();
$collection->add('foo', $foo = new Route('/foo'));
$collection1 = new RouteCollection();
$collection1->add('bar', $bar = new Route('/bar'));
$collection->addCollection($collection1);
$collection->add('last', $last = new Route('/last'));
$collection->remove('foo');
$this->assertSame(array('bar' => $bar, 'last' => $last), $collection->all(), '->remove() can remove a single route');
$collection->remove(array('bar', 'last'));
$this->assertSame(array(), $collection->all(), '->remove() accepts an array and can remove multiple routes at once');
}
public function testSetHost()
{
$collection = new RouteCollection();
$routea = new Route('/a');
$routeb = new Route('/b', array(), array(), array(), '{locale}.example.net');
$collection->add('a', $routea);
$collection->add('b', $routeb);
$collection->setHost('{locale}.example.com');
$this->assertEquals('{locale}.example.com', $routea->getHost());
$this->assertEquals('{locale}.example.com', $routeb->getHost());
}
public function testSetCondition()
{
$collection = new RouteCollection();
$routea = new Route('/a');
$routeb = new Route('/b', array(), array(), array(), '{locale}.example.net', array(), array(), 'context.getMethod() == "GET"');
$collection->add('a', $routea);
$collection->add('b', $routeb);
$collection->setCondition('context.getMethod() == "POST"');
$this->assertEquals('context.getMethod() == "POST"', $routea->getCondition());
$this->assertEquals('context.getMethod() == "POST"', $routeb->getCondition());
}
public function testClone()
{
$collection = new RouteCollection();
$collection->add('a', new Route('/a'));
$collection->add('b', new Route('/b', array('placeholder' => 'default'), array('placeholder' => '.+')));
$clonedCollection = clone $collection;
$this->assertCount(2, $clonedCollection);
$this->assertEquals($collection->get('a'), $clonedCollection->get('a'));
$this->assertNotSame($collection->get('a'), $clonedCollection->get('a'));
$this->assertEquals($collection->get('b'), $clonedCollection->get('b'));
$this->assertNotSame($collection->get('b'), $clonedCollection->get('b'));
}
public function testSetSchemes()
{
$collection = new RouteCollection();
$routea = new Route('/a', array(), array(), array(), '', 'http');
$routeb = new Route('/b');
$collection->add('a', $routea);
$collection->add('b', $routeb);
$collection->setSchemes(array('http', 'https'));
$this->assertEquals(array('http', 'https'), $routea->getSchemes());
$this->assertEquals(array('http', 'https'), $routeb->getSchemes());
}
public function testSetMethods()
{
$collection = new RouteCollection();
$routea = new Route('/a', array(), array(), array(), '', array(), array('GET', 'POST'));
$routeb = new Route('/b');
$collection->add('a', $routea);
$collection->add('b', $routeb);
$collection->setMethods('PUT');
$this->assertEquals(array('PUT'), $routea->getMethods());
$this->assertEquals(array('PUT'), $routeb->getMethods());
}
}

View file

@ -1,253 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\Routing\Route;
class RouteCompilerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideCompileData
*/
public function testCompile($name, $arguments, $prefix, $regex, $variables, $tokens)
{
$r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
$route = $r->newInstanceArgs($arguments);
$compiled = $route->compile();
$this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
$this->assertEquals($regex, $compiled->getRegex(), $name.' (regex)');
$this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
$this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
}
public function provideCompileData()
{
return array(
array(
'Static route',
array('/foo'),
'/foo', '#^/foo$#s', array(), array(
array('text', '/foo'),
),),
array(
'Route with a variable',
array('/foo/{bar}'),
'/foo', '#^/foo/(?P<bar>[^/]++)$#s', array('bar'), array(
array('variable', '/', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with a variable that has a default value',
array('/foo/{bar}', array('bar' => 'bar')),
'/foo', '#^/foo(?:/(?P<bar>[^/]++))?$#s', array('bar'), array(
array('variable', '/', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with several variables',
array('/foo/{bar}/{foobar}'),
'/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
array('variable', '/', '[^/]++', 'foobar'),
array('variable', '/', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with several variables that have default values',
array('/foo/{bar}/{foobar}', array('bar' => 'bar', 'foobar' => '')),
'/foo', '#^/foo(?:/(?P<bar>[^/]++)(?:/(?P<foobar>[^/]++))?)?$#s', array('bar', 'foobar'), array(
array('variable', '/', '[^/]++', 'foobar'),
array('variable', '/', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with several variables but some of them have no default values',
array('/foo/{bar}/{foobar}', array('bar' => 'bar')),
'/foo', '#^/foo/(?P<bar>[^/]++)/(?P<foobar>[^/]++)$#s', array('bar', 'foobar'), array(
array('variable', '/', '[^/]++', 'foobar'),
array('variable', '/', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with an optional variable as the first segment',
array('/{bar}', array('bar' => 'bar')),
'', '#^/(?P<bar>[^/]++)?$#s', array('bar'), array(
array('variable', '/', '[^/]++', 'bar'),
),),
array(
'Route with a requirement of 0',
array('/{bar}', array('bar' => null), array('bar' => '0')),
'', '#^/(?P<bar>0)?$#s', array('bar'), array(
array('variable', '/', '0', 'bar'),
),),
array(
'Route with an optional variable as the first segment with requirements',
array('/{bar}', array('bar' => 'bar'), array('bar' => '(foo|bar)')),
'', '#^/(?P<bar>(foo|bar))?$#s', array('bar'), array(
array('variable', '/', '(foo|bar)', 'bar'),
),),
array(
'Route with only optional variables',
array('/{foo}/{bar}', array('foo' => 'foo', 'bar' => 'bar')),
'', '#^/(?P<foo>[^/]++)?(?:/(?P<bar>[^/]++))?$#s', array('foo', 'bar'), array(
array('variable', '/', '[^/]++', 'bar'),
array('variable', '/', '[^/]++', 'foo'),
),),
array(
'Route with a variable in last position',
array('/foo-{bar}'),
'/foo', '#^/foo\-(?P<bar>[^/]++)$#s', array('bar'), array(
array('variable', '-', '[^/]++', 'bar'),
array('text', '/foo'),
),),
array(
'Route with nested placeholders',
array('/{static{var}static}'),
'/{static', '#^/\{static(?P<var>[^/]+)static\}$#s', array('var'), array(
array('text', 'static}'),
array('variable', '', '[^/]+', 'var'),
array('text', '/{static'),
),),
array(
'Route without separator between variables',
array('/{w}{x}{y}{z}.{_format}', array('z' => 'default-z', '_format' => 'html'), array('y' => '(y|Y)')),
'', '#^/(?P<w>[^/\.]+)(?P<x>[^/\.]+)(?P<y>(y|Y))(?:(?P<z>[^/\.]++)(?:\.(?P<_format>[^/]++))?)?$#s', array('w', 'x', 'y', 'z', '_format'), array(
array('variable', '.', '[^/]++', '_format'),
array('variable', '', '[^/\.]++', 'z'),
array('variable', '', '(y|Y)', 'y'),
array('variable', '', '[^/\.]+', 'x'),
array('variable', '/', '[^/\.]+', 'w'),
),),
array(
'Route with a format',
array('/foo/{bar}.{_format}'),
'/foo', '#^/foo/(?P<bar>[^/\.]++)\.(?P<_format>[^/]++)$#s', array('bar', '_format'), array(
array('variable', '.', '[^/]++', '_format'),
array('variable', '/', '[^/\.]++', 'bar'),
array('text', '/foo'),
),),
);
}
/**
* @expectedException \LogicException
*/
public function testRouteWithSameVariableTwice()
{
$route = new Route('/{name}/{name}');
$compiled = $route->compile();
}
/**
* @dataProvider getNumericVariableNames
* @expectedException \DomainException
*/
public function testRouteWithNumericVariableName($name)
{
$route = new Route('/{'.$name.'}');
$route->compile();
}
public function getNumericVariableNames()
{
return array(
array('09'),
array('123'),
array('1e2'),
);
}
/**
* @dataProvider provideCompileWithHostData
*/
public function testCompileWithHost($name, $arguments, $prefix, $regex, $variables, $pathVariables, $tokens, $hostRegex, $hostVariables, $hostTokens)
{
$r = new \ReflectionClass('Symfony\\Component\\Routing\\Route');
$route = $r->newInstanceArgs($arguments);
$compiled = $route->compile();
$this->assertEquals($prefix, $compiled->getStaticPrefix(), $name.' (static prefix)');
$this->assertEquals($regex, str_replace(array("\n", ' '), '', $compiled->getRegex()), $name.' (regex)');
$this->assertEquals($variables, $compiled->getVariables(), $name.' (variables)');
$this->assertEquals($pathVariables, $compiled->getPathVariables(), $name.' (path variables)');
$this->assertEquals($tokens, $compiled->getTokens(), $name.' (tokens)');
$this->assertEquals($hostRegex, str_replace(array("\n", ' '), '', $compiled->getHostRegex()), $name.' (host regex)');
$this->assertEquals($hostVariables, $compiled->getHostVariables(), $name.' (host variables)');
$this->assertEquals($hostTokens, $compiled->getHostTokens(), $name.' (host tokens)');
}
public function provideCompileWithHostData()
{
return array(
array(
'Route with host pattern',
array('/hello', array(), array(), array(), 'www.example.com'),
'/hello', '#^/hello$#s', array(), array(), array(
array('text', '/hello'),
),
'#^www\.example\.com$#si', array(), array(
array('text', 'www.example.com'),
),
),
array(
'Route with host pattern and some variables',
array('/hello/{name}', array(), array(), array(), 'www.example.{tld}'),
'/hello', '#^/hello/(?P<name>[^/]++)$#s', array('tld', 'name'), array('name'), array(
array('variable', '/', '[^/]++', 'name'),
array('text', '/hello'),
),
'#^www\.example\.(?P<tld>[^\.]++)$#si', array('tld'), array(
array('variable', '.', '[^\.]++', 'tld'),
array('text', 'www.example'),
),
),
array(
'Route with variable at beginning of host',
array('/hello', array(), array(), array(), '{locale}.example.{tld}'),
'/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
array('text', '/hello'),
),
'#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
array('variable', '.', '[^\.]++', 'tld'),
array('text', '.example'),
array('variable', '', '[^\.]++', 'locale'),
),
),
array(
'Route with host variables that has a default value',
array('/hello', array('locale' => 'a', 'tld' => 'b'), array(), array(), '{locale}.example.{tld}'),
'/hello', '#^/hello$#s', array('locale', 'tld'), array(), array(
array('text', '/hello'),
),
'#^(?P<locale>[^\.]++)\.example\.(?P<tld>[^\.]++)$#si', array('locale', 'tld'), array(
array('variable', '.', '[^\.]++', 'tld'),
array('text', '.example'),
array('variable', '', '[^\.]++', 'locale'),
),
),
);
}
}

View file

@ -1,284 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\Routing\Route;
class RouteTest extends \PHPUnit_Framework_TestCase
{
public function testConstructor()
{
$route = new Route('/{foo}', array('foo' => 'bar'), array('foo' => '\d+'), array('foo' => 'bar'), '{locale}.example.com');
$this->assertEquals('/{foo}', $route->getPath(), '__construct() takes a path as its first argument');
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '__construct() takes defaults as its second argument');
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '__construct() takes requirements as its third argument');
$this->assertEquals('bar', $route->getOption('foo'), '__construct() takes options as its fourth argument');
$this->assertEquals('{locale}.example.com', $route->getHost(), '__construct() takes a host pattern as its fifth argument');
$route = new Route('/', array(), array(), array(), '', array('Https'), array('POST', 'put'), 'context.getMethod() == "GET"');
$this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes schemes as its sixth argument and lowercases it');
$this->assertEquals(array('POST', 'PUT'), $route->getMethods(), '__construct() takes methods as its seventh argument and uppercases it');
$this->assertEquals('context.getMethod() == "GET"', $route->getCondition(), '__construct() takes a condition as its eight argument');
$route = new Route('/', array(), array(), array(), '', 'Https', 'Post');
$this->assertEquals(array('https'), $route->getSchemes(), '__construct() takes a single scheme as its sixth argument');
$this->assertEquals(array('POST'), $route->getMethods(), '__construct() takes a single method as its seventh argument');
}
public function testPath()
{
$route = new Route('/{foo}');
$route->setPath('/{bar}');
$this->assertEquals('/{bar}', $route->getPath(), '->setPath() sets the path');
$route->setPath('');
$this->assertEquals('/', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
$route->setPath('bar');
$this->assertEquals('/bar', $route->getPath(), '->setPath() adds a / at the beginning of the path if needed');
$this->assertEquals($route, $route->setPath(''), '->setPath() implements a fluent interface');
$route->setPath('//path');
$this->assertEquals('/path', $route->getPath(), '->setPath() does not allow two slashes "//" at the beginning of the path as it would be confused with a network path when generating the path from the route');
}
public function testOptions()
{
$route = new Route('/{foo}');
$route->setOptions(array('foo' => 'bar'));
$this->assertEquals(array_merge(array(
'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler',
), array('foo' => 'bar')), $route->getOptions(), '->setOptions() sets the options');
$this->assertEquals($route, $route->setOptions(array()), '->setOptions() implements a fluent interface');
$route->setOptions(array('foo' => 'foo'));
$route->addOptions(array('bar' => 'bar'));
$this->assertEquals($route, $route->addOptions(array()), '->addOptions() implements a fluent interface');
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar', 'compiler_class' => 'Symfony\\Component\\Routing\\RouteCompiler'), $route->getOptions(), '->addDefaults() keep previous defaults');
}
public function testOption()
{
$route = new Route('/{foo}');
$this->assertFalse($route->hasOption('foo'), '->hasOption() return false if option is not set');
$this->assertEquals($route, $route->setOption('foo', 'bar'), '->setOption() implements a fluent interface');
$this->assertEquals('bar', $route->getOption('foo'), '->setOption() sets the option');
$this->assertTrue($route->hasOption('foo'), '->hasOption() return true if option is set');
}
public function testDefaults()
{
$route = new Route('/{foo}');
$route->setDefaults(array('foo' => 'bar'));
$this->assertEquals(array('foo' => 'bar'), $route->getDefaults(), '->setDefaults() sets the defaults');
$this->assertEquals($route, $route->setDefaults(array()), '->setDefaults() implements a fluent interface');
$route->setDefault('foo', 'bar');
$this->assertEquals('bar', $route->getDefault('foo'), '->setDefault() sets a default value');
$route->setDefault('foo2', 'bar2');
$this->assertEquals('bar2', $route->getDefault('foo2'), '->getDefault() return the default value');
$this->assertNull($route->getDefault('not_defined'), '->getDefault() return null if default value is not set');
$route->setDefault('_controller', $closure = function () { return 'Hello'; });
$this->assertEquals($closure, $route->getDefault('_controller'), '->setDefault() sets a default value');
$route->setDefaults(array('foo' => 'foo'));
$route->addDefaults(array('bar' => 'bar'));
$this->assertEquals($route, $route->addDefaults(array()), '->addDefaults() implements a fluent interface');
$this->assertEquals(array('foo' => 'foo', 'bar' => 'bar'), $route->getDefaults(), '->addDefaults() keep previous defaults');
}
public function testRequirements()
{
$route = new Route('/{foo}');
$route->setRequirements(array('foo' => '\d+'));
$this->assertEquals(array('foo' => '\d+'), $route->getRequirements(), '->setRequirements() sets the requirements');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() returns a requirement');
$this->assertNull($route->getRequirement('bar'), '->getRequirement() returns null if a requirement is not defined');
$route->setRequirements(array('foo' => '^\d+$'));
$this->assertEquals('\d+', $route->getRequirement('foo'), '->getRequirement() removes ^ and $ from the path');
$this->assertEquals($route, $route->setRequirements(array()), '->setRequirements() implements a fluent interface');
$route->setRequirements(array('foo' => '\d+'));
$route->addRequirements(array('bar' => '\d+'));
$this->assertEquals($route, $route->addRequirements(array()), '->addRequirements() implements a fluent interface');
$this->assertEquals(array('foo' => '\d+', 'bar' => '\d+'), $route->getRequirements(), '->addRequirement() keep previous requirements');
}
public function testRequirement()
{
$route = new Route('/{foo}');
$this->assertFalse($route->hasRequirement('foo'), '->hasRequirement() return false if requirement is not set');
$route->setRequirement('foo', '^\d+$');
$this->assertEquals('\d+', $route->getRequirement('foo'), '->setRequirement() removes ^ and $ from the path');
$this->assertTrue($route->hasRequirement('foo'), '->hasRequirement() return true if requirement is set');
}
/**
* @dataProvider getInvalidRequirements
* @expectedException \InvalidArgumentException
*/
public function testSetInvalidRequirement($req)
{
$route = new Route('/{foo}');
$route->setRequirement('foo', $req);
}
public function getInvalidRequirements()
{
return array(
array(''),
array(array()),
array('^$'),
array('^'),
array('$'),
);
}
public function testHost()
{
$route = new Route('/');
$route->setHost('{locale}.example.net');
$this->assertEquals('{locale}.example.net', $route->getHost(), '->setHost() sets the host pattern');
}
public function testScheme()
{
$route = new Route('/');
$this->assertEquals(array(), $route->getSchemes(), 'schemes is initialized with array()');
$this->assertFalse($route->hasScheme('http'));
$route->setSchemes('hTTp');
$this->assertEquals(array('http'), $route->getSchemes(), '->setSchemes() accepts a single scheme string and lowercases it');
$this->assertTrue($route->hasScheme('htTp'));
$this->assertFalse($route->hasScheme('httpS'));
$route->setSchemes(array('HttpS', 'hTTp'));
$this->assertEquals(array('https', 'http'), $route->getSchemes(), '->setSchemes() accepts an array of schemes and lowercases them');
$this->assertTrue($route->hasScheme('htTp'));
$this->assertTrue($route->hasScheme('httpS'));
}
/**
* @group legacy
*/
public function testLegacySchemeRequirement()
{
$route = new Route('/');
$route->setRequirement('_scheme', 'http|https');
$this->assertEquals('http|https', $route->getRequirement('_scheme'));
$this->assertEquals(array('http', 'https'), $route->getSchemes());
$this->assertTrue($route->hasScheme('https'));
$this->assertTrue($route->hasScheme('http'));
$this->assertFalse($route->hasScheme('ftp'));
$route->setSchemes(array('hTTp'));
$this->assertEquals('http', $route->getRequirement('_scheme'));
$route->setSchemes(array());
$this->assertNull($route->getRequirement('_scheme'));
}
public function testMethod()
{
$route = new Route('/');
$this->assertEquals(array(), $route->getMethods(), 'methods is initialized with array()');
$route->setMethods('gEt');
$this->assertEquals(array('GET'), $route->getMethods(), '->setMethods() accepts a single method string and uppercases it');
$route->setMethods(array('gEt', 'PosT'));
$this->assertEquals(array('GET', 'POST'), $route->getMethods(), '->setMethods() accepts an array of methods and uppercases them');
}
/**
* @group legacy
*/
public function testLegacyMethodRequirement()
{
$route = new Route('/');
$route->setRequirement('_method', 'GET|POST');
$this->assertEquals('GET|POST', $route->getRequirement('_method'));
$this->assertEquals(array('GET', 'POST'), $route->getMethods());
$route->setMethods(array('gEt'));
$this->assertEquals('GET', $route->getRequirement('_method'));
$route->setMethods(array());
$this->assertNull($route->getRequirement('_method'));
}
public function testCondition()
{
$route = new Route('/');
$this->assertSame('', $route->getCondition());
$route->setCondition('context.getMethod() == "GET"');
$this->assertSame('context.getMethod() == "GET"', $route->getCondition());
}
public function testCompile()
{
$route = new Route('/{foo}');
$this->assertInstanceOf('Symfony\Component\Routing\CompiledRoute', $compiled = $route->compile(), '->compile() returns a compiled route');
$this->assertSame($compiled, $route->compile(), '->compile() only compiled the route once if unchanged');
$route->setRequirement('foo', '.*');
$this->assertNotSame($compiled, $route->compile(), '->compile() recompiles if the route was modified');
}
/**
* @group legacy
*/
public function testLegacyPattern()
{
$route = new Route('/{foo}');
$this->assertEquals('/{foo}', $route->getPattern());
$route->setPattern('/bar');
$this->assertEquals('/bar', $route->getPattern());
}
public function testSerialize()
{
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
$serialized = serialize($route);
$unserialized = unserialize($serialized);
$this->assertEquals($route, $unserialized);
$this->assertNotSame($route, $unserialized);
}
/**
* Tests that the compiled version is also serialized to prevent the overhead
* of compiling it again after unserialize.
*/
public function testSerializeWhenCompiled()
{
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
$route->setHost('{locale}.example.net');
$route->compile();
$serialized = serialize($route);
$unserialized = unserialize($serialized);
$this->assertEquals($route, $unserialized);
$this->assertNotSame($route, $unserialized);
}
/**
* Tests that the serialized representation of a route in one symfony version
* also works in later symfony versions, i.e. the unserialized route is in the
* same state as another, semantically equivalent, route.
*/
public function testSerializedRepresentationKeepsWorking()
{
$serialized = 'C:31:"Symfony\Component\Routing\Route":934:{a:8:{s:4:"path";s:13:"/prefix/{foo}";s:4:"host";s:20:"{locale}.example.net";s:8:"defaults";a:1:{s:3:"foo";s:7:"default";}s:12:"requirements";a:1:{s:3:"foo";s:3:"\d+";}s:7:"options";a:1:{s:14:"compiler_class";s:39:"Symfony\Component\Routing\RouteCompiler";}s:7:"schemes";a:0:{}s:7:"methods";a:0:{}s:8:"compiled";C:39:"Symfony\Component\Routing\CompiledRoute":569:{a:8:{s:4:"vars";a:2:{i:0;s:6:"locale";i:1;s:3:"foo";}s:11:"path_prefix";s:7:"/prefix";s:10:"path_regex";s:30:"#^/prefix(?:/(?P<foo>\d+))?$#s";s:11:"path_tokens";a:2:{i:0;a:4:{i:0;s:8:"variable";i:1;s:1:"/";i:2;s:3:"\d+";i:3;s:3:"foo";}i:1;a:2:{i:0;s:4:"text";i:1;s:7:"/prefix";}}s:9:"path_vars";a:1:{i:0;s:3:"foo";}s:10:"host_regex";s:39:"#^(?P<locale>[^\.]++)\.example\.net$#si";s:11:"host_tokens";a:2:{i:0;a:2:{i:0;s:4:"text";i:1;s:12:".example.net";}i:1;a:4:{i:0;s:8:"variable";i:1;s:0:"";i:2;s:7:"[^\.]++";i:3;s:6:"locale";}}s:9:"host_vars";a:1:{i:0;s:6:"locale";}}}}}';
$unserialized = unserialize($serialized);
$route = new Route('/prefix/{foo}', array('foo' => 'default'), array('foo' => '\d+'));
$route->setHost('{locale}.example.net');
$route->compile();
$this->assertEquals($route, $unserialized);
$this->assertNotSame($route, $unserialized);
}
}

View file

@ -1,161 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Routing\Tests;
use Symfony\Component\Routing\Router;
use Symfony\Component\HttpFoundation\Request;
class RouterTest extends \PHPUnit_Framework_TestCase
{
private $router = null;
private $loader = null;
protected function setUp()
{
$this->loader = $this->getMock('Symfony\Component\Config\Loader\LoaderInterface');
$this->router = new Router($this->loader, 'routing.yml');
}
public function testSetOptionsWithSupportedOptions()
{
$this->router->setOptions(array(
'cache_dir' => './cache',
'debug' => true,
'resource_type' => 'ResourceType',
));
$this->assertSame('./cache', $this->router->getOption('cache_dir'));
$this->assertTrue($this->router->getOption('debug'));
$this->assertSame('ResourceType', $this->router->getOption('resource_type'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The Router does not support the following options: "option_foo", "option_bar"
*/
public function testSetOptionsWithUnsupportedOptions()
{
$this->router->setOptions(array(
'cache_dir' => './cache',
'option_foo' => true,
'option_bar' => 'baz',
'resource_type' => 'ResourceType',
));
}
public function testSetOptionWithSupportedOption()
{
$this->router->setOption('cache_dir', './cache');
$this->assertSame('./cache', $this->router->getOption('cache_dir'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The Router does not support the "option_foo" option
*/
public function testSetOptionWithUnsupportedOption()
{
$this->router->setOption('option_foo', true);
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The Router does not support the "option_foo" option
*/
public function testGetOptionWithUnsupportedOption()
{
$this->router->getOption('option_foo', true);
}
public function testThatRouteCollectionIsLoaded()
{
$this->router->setOption('resource_type', 'ResourceType');
$routeCollection = $this->getMock('Symfony\Component\Routing\RouteCollection');
$this->loader->expects($this->once())
->method('load')->with('routing.yml', 'ResourceType')
->will($this->returnValue($routeCollection));
$this->assertSame($routeCollection, $this->router->getRouteCollection());
}
/**
* @dataProvider provideMatcherOptionsPreventingCaching
*/
public function testMatcherIsCreatedIfCacheIsNotConfigured($option)
{
$this->router->setOption($option, null);
$this->loader->expects($this->once())
->method('load')->with('routing.yml', null)
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
$this->assertInstanceOf('Symfony\\Component\\Routing\\Matcher\\UrlMatcher', $this->router->getMatcher());
}
public function provideMatcherOptionsPreventingCaching()
{
return array(
array('cache_dir'),
array('matcher_cache_class'),
);
}
/**
* @dataProvider provideGeneratorOptionsPreventingCaching
*/
public function testGeneratorIsCreatedIfCacheIsNotConfigured($option)
{
$this->router->setOption($option, null);
$this->loader->expects($this->once())
->method('load')->with('routing.yml', null)
->will($this->returnValue($this->getMock('Symfony\Component\Routing\RouteCollection')));
$this->assertInstanceOf('Symfony\\Component\\Routing\\Generator\\UrlGenerator', $this->router->getGenerator());
}
public function provideGeneratorOptionsPreventingCaching()
{
return array(
array('cache_dir'),
array('generator_cache_class'),
);
}
public function testMatchRequestWithUrlMatcherInterface()
{
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\UrlMatcherInterface');
$matcher->expects($this->once())->method('match');
$p = new \ReflectionProperty($this->router, 'matcher');
$p->setAccessible(true);
$p->setValue($this->router, $matcher);
$this->router->matchRequest(Request::create('/'));
}
public function testMatchRequestWithRequestMatcherInterface()
{
$matcher = $this->getMock('Symfony\Component\Routing\Matcher\RequestMatcherInterface');
$matcher->expects($this->once())->method('matchRequest');
$p = new \ReflectionProperty($this->router, 'matcher');
$p->setAccessible(true);
$p->setValue($this->router, $matcher);
$this->router->matchRequest(Request::create('/'));
}
}