This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php
Fabien Potencier 07aae98495 [Routing] added support for _scheme requirement
The _scheme requirement can be used to force routes to always match one given scheme
and to always be generated with the given scheme.

So, if _scheme is set to https, URL generation will force an absolute URL if the
current scheme is http. And if you request the URL with http, you will be redirected
to the https URL.
2011-04-20 10:49:32 +02:00

169 lines
6.0 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;
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('http_port' => 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('https_port' => 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 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);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGenerateWithoutRoutes()
{
$routes = $this->getRoutes('foo', new Route('/testing/{foo}'));
$this->getGenerator($routes)->generate('test', array(), true);
}
/**
* @expectedException \InvalidArgumentException
*/
public function testGenerateForRouteWithoutManditoryParameter()
{
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$this->getGenerator($routes)->generate('test', array(), true);
}
/**
* @expectedException \InvalidArgumentException
*/
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 \InvalidArgumentException
*/
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'));
}
protected function getGenerator(RouteCollection $routes, array $context = array())
{
$generator = new UrlGenerator($routes);
$generator->setContext(array_replace(array(
'base_url' => '/app.php',
'method' => 'GET',
'host' => 'localhost',
'http_port' => 80,
'https_port' => 443,
'scheme' => 'http',
), $context));
return $generator;
}
protected function getRoutes($name, Route $route)
{
$routes = new RouteCollection();
$routes->add($name, $route);
return $routes;
}
}