2010-06-24 07:46:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2010-06-24 07:46:50 +01:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Tests\Component\Routing\Matcher;
|
2010-06-24 07:46:50 +01:00
|
|
|
|
2011-05-17 09:11:27 +01:00
|
|
|
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
|
2011-05-17 15:51:56 +01:00
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
|
2010-08-20 22:09:55 +01:00
|
|
|
use Symfony\Component\Routing\Matcher\UrlMatcher;
|
|
|
|
use Symfony\Component\Routing\Route;
|
2011-03-21 12:56:53 +00:00
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
2011-04-20 12:54:39 +01:00
|
|
|
use Symfony\Component\Routing\RequestContext;
|
2010-06-24 07:46:50 +01:00
|
|
|
|
|
|
|
class UrlMatcherTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
2011-03-22 19:56:55 +00:00
|
|
|
public function testNoMethodSoAllowed()
|
|
|
|
{
|
|
|
|
$coll = new RouteCollection();
|
|
|
|
$coll->add('foo', new Route('/foo'));
|
|
|
|
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($coll, new RequestContext());
|
2011-03-22 19:56:55 +00:00
|
|
|
$matcher->match('/foo');
|
|
|
|
}
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
public function testMethodNotAllowed()
|
|
|
|
{
|
|
|
|
$coll = new RouteCollection();
|
|
|
|
$coll->add('foo', new Route('/foo', array(), array('_method' => 'post')));
|
|
|
|
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($coll, new RequestContext());
|
2011-03-21 12:56:53 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$matcher->match('/foo');
|
|
|
|
$this->fail();
|
|
|
|
} catch (MethodNotAllowedException $e) {
|
2011-06-04 17:59:57 +01:00
|
|
|
$this->assertEquals(array('POST'), $e->getAllowedMethods());
|
2011-03-21 12:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-04 11:28:01 +01:00
|
|
|
public function testHeadAllowedWhenRequirementContainsGet()
|
|
|
|
{
|
|
|
|
$coll = new RouteCollection();
|
|
|
|
$coll->add('foo', new Route('/foo', array(), array('_method' => 'get')));
|
|
|
|
|
|
|
|
$matcher = new UrlMatcher($coll, new RequestContext('', 'head'));
|
|
|
|
$matcher->match('/foo');
|
|
|
|
}
|
|
|
|
|
2011-03-21 12:56:53 +00:00
|
|
|
public function testMethodNotAllowedAggregatesAllowedMethods()
|
|
|
|
{
|
|
|
|
$coll = new RouteCollection();
|
|
|
|
$coll->add('foo1', new Route('/foo', array(), array('_method' => 'post')));
|
|
|
|
$coll->add('foo2', new Route('/foo', array(), array('_method' => 'put|delete')));
|
|
|
|
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($coll, new RequestContext());
|
2011-03-21 12:56:53 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
$matcher->match('/foo');
|
|
|
|
$this->fail();
|
|
|
|
} catch (MethodNotAllowedException $e) {
|
2011-06-04 17:59:57 +01:00
|
|
|
$this->assertEquals(array('POST', 'PUT', 'DELETE'), $e->getAllowedMethods());
|
2011-03-21 12:56:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-25 02:50:21 +00:00
|
|
|
public function testMatch()
|
|
|
|
{
|
2011-03-21 12:56:53 +00:00
|
|
|
// test the patterns are matched are parameters are returned
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', new Route('/foo/{bar}'));
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
try {
|
|
|
|
$matcher->match('/no-match');
|
|
|
|
$this->fail();
|
2011-05-17 15:51:56 +01:00
|
|
|
} catch (ResourceNotFoundException $e) {}
|
2011-03-21 12:56:53 +00:00
|
|
|
$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')));
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
$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('_method' => 'GET|head')));
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
$this->assertInternalType('array', $matcher->match('/foo'));
|
|
|
|
|
|
|
|
// route does not match with POST method context
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext('', 'post'), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
try {
|
|
|
|
$matcher->match('/foo');
|
|
|
|
$this->fail();
|
|
|
|
} catch (MethodNotAllowedException $e) {}
|
|
|
|
|
|
|
|
// route does match with GET or HEAD method context
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
$this->assertInternalType('array', $matcher->match('/foo'));
|
2011-04-20 12:54:39 +01:00
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext('', 'head'), array());
|
2011-03-21 12:56:53 +00:00
|
|
|
$this->assertInternalType('array', $matcher->match('/foo'));
|
2011-05-03 07:54:33 +01:00
|
|
|
|
|
|
|
// 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(), array());
|
|
|
|
$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(), array());
|
|
|
|
$this->assertEquals(array('_route' => 'bar', 'bar' => 'foo'), $matcher->match('/foo'));
|
|
|
|
$this->assertEquals(array('_route' => 'bar', 'bar' => 'bar'), $matcher->match('/'));
|
2010-11-25 02:50:21 +00:00
|
|
|
}
|
2011-04-26 08:01:25 +01:00
|
|
|
|
2011-05-04 22:29:47 +01:00
|
|
|
public function testMatchWithPrefixes()
|
|
|
|
{
|
|
|
|
$collection1 = new RouteCollection();
|
|
|
|
$collection1->add('foo', new Route('/{foo}'));
|
|
|
|
|
|
|
|
$collection2 = new RouteCollection();
|
|
|
|
$collection2->addCollection($collection1, '/b');
|
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->addCollection($collection2, '/a');
|
|
|
|
|
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
|
|
|
$this->assertEquals(array('_route' => 'foo', 'foo' => 'foo'), $matcher->match('/a/b/foo'));
|
|
|
|
}
|
|
|
|
|
2011-06-04 18:44:04 +01:00
|
|
|
public function testMatchWithDynamicPrefix()
|
|
|
|
{
|
|
|
|
$collection1 = new RouteCollection();
|
|
|
|
$collection1->add('foo', new Route('/{foo}'));
|
|
|
|
|
|
|
|
$collection2 = new RouteCollection();
|
|
|
|
$collection2->addCollection($collection1, '/b');
|
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->addCollection($collection2, '/{_locale}');
|
|
|
|
|
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
|
|
|
$this->assertEquals(array('_locale' => 'fr', '_route' => 'foo', 'foo' => 'foo'), $matcher->match('/fr/b/foo'));
|
|
|
|
}
|
|
|
|
|
2011-06-29 02:01:08 +01:00
|
|
|
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(), array());
|
|
|
|
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.urlencode($chars).'/bar'));
|
|
|
|
$this->assertEquals(array('_route' => 'foo', 'foo' => $chars), $matcher->match('/'.strtr($chars, array('%' => '%25', '+' => '%2B')).'/bar'));
|
|
|
|
}
|
|
|
|
|
2011-04-26 08:01:25 +01:00
|
|
|
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'));
|
2011-05-03 13:48:08 +01:00
|
|
|
|
|
|
|
$collection = new RouteCollection();
|
|
|
|
$collection->add('foo', new Route('/{bar}'));
|
|
|
|
$matcher = new UrlMatcher($collection, new RequestContext(), array());
|
|
|
|
try {
|
|
|
|
$matcher->match('/');
|
|
|
|
$this->fail();
|
2011-05-17 15:51:56 +01:00
|
|
|
} catch (ResourceNotFoundException $e) {
|
2011-05-03 13:48:08 +01:00
|
|
|
}
|
2011-04-26 08:01:25 +01:00
|
|
|
}
|
2010-06-24 07:46:50 +01:00
|
|
|
}
|