18894762ee
Commits
-------
03c7cfe
UrlGenerator no longer appends '?' if query string is empty
Discussion
----------
UrlGenerator no longer appends '?' if query string is empty
If you generate a URL using null parameters (`array('foo' => null, 'bar' => null')`), `http_build_query` returns an empty string, resulting in a trailing `?` at the end of the generated URL.
This fixes that so that, if there are `$extra` params & `http_build_query` is empty, the URL is no longer appended.
---------------------------------------------------------------------------
by fabpot at 2011/07/22 10:15:26 -0700
Can you add unit tests?
---------------------------------------------------------------------------
by ericclemmons at 2011/07/22 10:52:21 -0700
Yes sir, will do.
-Eric Clemmons
Sent from my iPad Nano
On Jul 22, 2011, at 12:15 PM, fabpot<reply@reply.github.com> wrote:
> Can you add unit tests?
>
> --
> Reply to this email directly or view it on GitHub:
> https://github.com/symfony/symfony/pull/1773#issuecomment-1633515
---------------------------------------------------------------------------
by ericclemmons at 2011/07/22 11:55:30 -0700
**Added passing test.**
Currently `master` fails test:
```
1) Symfony\Tests\Component\Routing\Generator\UrlGeneratorTest::testUrlWithNullExtraParameters
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-http://localhost/app.php/testing
+http://localhost/app.php/testing?
//tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php:114
```
229 lines
8.5 KiB
PHP
229 lines
8.5 KiB
PHP
<?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\Tests\Component\Routing\Generator;
|
|
|
|
use Symfony\Component\Routing\RouteCollection;
|
|
use Symfony\Component\Routing\Route;
|
|
use Symfony\Component\Routing\Generator\UrlGenerator;
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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 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);
|
|
}
|
|
|
|
/**
|
|
* @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 testGenerateForRouteWithInvalidManditoryParameter()
|
|
{
|
|
$routes = $this->getRoutes('test', new Route('/testing/{foo}', array(), array('foo' => 'd+')));
|
|
$this->getGenerator($routes)->generate('test', array('foo' => 'bar'), true);
|
|
}
|
|
|
|
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'));
|
|
}
|
|
|
|
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')));
|
|
}
|
|
|
|
protected function getGenerator(RouteCollection $routes, array $parameters = array())
|
|
{
|
|
$context = new RequestContext('/app.php');
|
|
foreach ($parameters as $key => $value) {
|
|
$method = 'set'.$key;
|
|
$context->$method($value);
|
|
}
|
|
$generator = new UrlGenerator($routes, $context);
|
|
|
|
return $generator;
|
|
}
|
|
|
|
protected function getRoutes($name, Route $route)
|
|
{
|
|
$routes = new RouteCollection();
|
|
$routes->add($name, $route);
|
|
|
|
return $routes;
|
|
}
|
|
}
|