2011-02-03 15:21:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-03-06 11:40:06 +00:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-03 15:21:41 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Tests\Component\Routing\Generator;
|
|
|
|
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
|
|
use Symfony\Component\Routing\Route;
|
|
|
|
use Symfony\Component\Routing\Generator\UrlGenerator;
|
2011-04-20 12:54:39 +01:00
|
|
|
use Symfony\Component\Routing\RequestContext;
|
2011-02-03 15:21:41 +00:00
|
|
|
|
|
|
|
class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
public function testAbsoluteUrlWithPort80()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array(), true);
|
2011-02-03 15:21:41 +00:00
|
|
|
|
|
|
|
$this->assertEquals('http://localhost/app.php/testing', $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAbsoluteSecureUrlWithPort443()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
2011-04-19 23:25:45 +01:00
|
|
|
$url = $this->getGenerator($routes, array('scheme' => 'https'))->generate('test', array(), true);
|
2011-02-03 15:21:41 +00:00
|
|
|
|
|
|
|
$this->assertEquals('https://localhost/app.php/testing', $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAbsoluteUrlWithNonStandardPort()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
2011-04-20 12:54:39 +01:00
|
|
|
$url = $this->getGenerator($routes, array('httpPort' => 8080))->generate('test', array(), true);
|
2011-02-03 15:21:41 +00:00
|
|
|
|
|
|
|
$this->assertEquals('http://localhost:8080/app.php/testing', $url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAbsoluteSecureUrlWithNonStandardPort()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
2011-04-20 12:54:39 +01:00
|
|
|
$url = $this->getGenerator($routes, array('httpsPort' => 8080, 'scheme' => 'https'))->generate('test', array(), true);
|
2011-02-03 15:21:41 +00:00
|
|
|
|
|
|
|
$this->assertEquals('https://localhost:8080/app.php/testing', $url);
|
|
|
|
}
|
2011-03-19 12:04:32 +00:00
|
|
|
|
|
|
|
public function testRelativeUrlWithoutParameters()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array(), false);
|
2011-03-19 12:04:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals('/app.php/testing', $url);
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
public function testRelativeUrlWithParameter()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
|
2011-03-19 12:04:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals('/app.php/testing/bar', $url);
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-04-18 14:35:05 +01:00
|
|
|
public function testRelativeUrlWithNullParameter()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing.{format}', array('format' => null)));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array(), false);
|
2011-04-18 14:35:05 +01:00
|
|
|
|
|
|
|
$this->assertEquals('/app.php/testing', $url);
|
|
|
|
}
|
|
|
|
|
2011-04-24 12:06:02 +01:00
|
|
|
public function testRelativeUrlWithNullParameterButNotOptional()
|
|
|
|
{
|
|
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}/bar', array('foo' => null)));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array(), false);
|
|
|
|
|
|
|
|
$this->assertEquals('/app.php/testing//bar', $url);
|
|
|
|
}
|
|
|
|
|
2011-04-26 08:50:57 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
public function testRelativeUrlWithExtraParameters()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), false);
|
2011-03-19 12:04:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals('/app.php/testing?foo=bar', $url);
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
public function testAbsoluteUrlWithExtraParameters()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing'));
|
|
|
|
$url = $this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
2011-03-19 12:04:32 +00:00
|
|
|
|
|
|
|
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
|
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-07-22 17:51:08 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-04-21 08:52:35 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
/**
|
2011-05-17 15:51:56 +01:00
|
|
|
* @expectedException Symfony\Component\Routing\Exception\RouteNotFoundException
|
2011-03-19 12:04:32 +00:00
|
|
|
*/
|
|
|
|
public function testGenerateWithoutRoutes()
|
2011-04-15 20:12:02 +01:00
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
|
|
|
|
$this->getGenerator($routes)->generate('test', array(), true);
|
2011-03-19 12:04:32 +00:00
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
/**
|
2011-05-13 16:46:31 +01:00
|
|
|
* @expectedException Symfony\Component\Routing\Exception\MissingMandatoryParametersException
|
2011-03-19 12:04:32 +00:00
|
|
|
*/
|
2011-06-12 21:52:53 +01:00
|
|
|
public function testGenerateForRouteWithoutMandatoryParameter()
|
2011-03-19 12:04:32 +00:00
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
|
|
|
|
$this->getGenerator($routes)->generate('test', array(), true);
|
2011-03-19 12:04:32 +00:00
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
/**
|
2011-05-13 16:46:31 +01:00
|
|
|
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
|
2011-03-19 12:04:32 +00:00
|
|
|
*/
|
|
|
|
public function testGenerateForRouteWithInvalidOptionalParameter()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+')));
|
|
|
|
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
2011-03-19 12:04:32 +00:00
|
|
|
}
|
2011-04-15 20:12:02 +01:00
|
|
|
|
2011-03-19 12:04:32 +00:00
|
|
|
/**
|
2011-05-13 16:46:31 +01:00
|
|
|
* @expectedException Symfony\Component\Routing\Exception\InvalidParameterException
|
2011-03-19 12:04:32 +00:00
|
|
|
*/
|
|
|
|
public function testGenerateForRouteWithInvalidManditoryParameter()
|
|
|
|
{
|
2011-04-19 22:21:22 +01:00
|
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
|
|
|
|
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
|
|
|
}
|
|
|
|
|
2011-04-19 23:25:45 +01:00
|
|
|
public function testSchemeRequirementDoesNothingIfSameCurrentScheme()
|
|
|
|
{
|
|
|
|
$routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
|
|
|
|
$this->assertEquals('/app.php/', $this->getGenerator($routes)->generate('test'));
|
|
|
|
|
|
|
|
$routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
|
|
|
|
$this->assertEquals('/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSchemeRequirementForcesAbsoluteUrl()
|
|
|
|
{
|
|
|
|
$routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'https')));
|
|
|
|
$this->assertEquals('https://localhost/app.php/', $this->getGenerator($routes)->generate('test'));
|
|
|
|
|
|
|
|
$routes = $this->getRoutes('test', new Route('/', array(), array('_scheme' => 'http')));
|
|
|
|
$this->assertEquals('http://localhost/app.php/', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test'));
|
|
|
|
}
|
|
|
|
|
2011-06-03 02:22:33 +01:00
|
|
|
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')));
|
|
|
|
}
|
|
|
|
|
2011-07-17 16:29:41 +01:00
|
|
|
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')));
|
|
|
|
}
|
|
|
|
|
2011-04-20 12:54:39 +01:00
|
|
|
protected function getGenerator(RouteCollection $routes, array $parameters = array())
|
2011-04-19 22:21:22 +01:00
|
|
|
{
|
2011-04-20 12:54:39 +01:00
|
|
|
$context = new RequestContext('/app.php');
|
|
|
|
foreach ($parameters as $key => $value) {
|
|
|
|
$method = 'set'.$key;
|
|
|
|
$context->$method($value);
|
|
|
|
}
|
|
|
|
$generator = new UrlGenerator($routes, $context);
|
2011-04-19 22:21:22 +01:00
|
|
|
|
|
|
|
return $generator;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getRoutes($name, Route $route)
|
|
|
|
{
|
|
|
|
$routes = new RouteCollection();
|
|
|
|
$routes->add($name, $route);
|
|
|
|
|
|
|
|
return $routes;
|
2011-04-15 20:12:02 +01:00
|
|
|
}
|
2011-03-19 12:04:32 +00:00
|
|
|
}
|