123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?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
- {
- /** @var RouteCollection */
- private $routeCollection;
- /** @var UrlGenerator */
- private $generator;
- protected function setUp()
- {
- parent::setUp();
- $this->routeCollection = new RouteCollection();
- $this->generator = new UrlGenerator($this->routeCollection);
- }
- public function testAbsoluteUrlWithPort80()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array(), true);
- $this->assertEquals('http://localhost/app.php/testing', $url);
- }
- public function testAbsoluteSecureUrlWithPort443()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>443,
- 'is_secure'=>true));
- $url = $this->generator->generate('test', array(), true);
- $this->assertEquals('https://localhost/app.php/testing', $url);
- }
- public function testAbsoluteUrlWithNonStandardPort()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>8080,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array(), true);
- $this->assertEquals('http://localhost:8080/app.php/testing', $url);
- }
- public function testAbsoluteSecureUrlWithNonStandardPort()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>8080,
- 'is_secure'=>true));
- $url = $this->generator->generate('test', array(), true);
- $this->assertEquals('https://localhost:8080/app.php/testing', $url);
- }
- public function testRelativeUrlWithoutParameters()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array(), false);
- $this->assertEquals('/app.php/testing', $url);
- }
- public function testRelativeUrlWithParameter()
- {
- $this->routeCollection->add('test', new Route('/testing/{foo}'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array('foo' => 'bar'), false);
- $this->assertEquals('/app.php/testing/bar', $url);
- }
- public function testRelativeUrlWithNullParameter()
- {
- $this->routeCollection->add('test', new Route('/testing.{format}', array('format' => null)));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array(), false);
- $this->assertEquals('/app.php/testing', $url);
- }
- public function testRelativeUrlWithExtraParameters()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array('foo' => 'bar'), false);
- $this->assertEquals('/app.php/testing?foo=bar', $url);
- }
- public function testAbsoluteUrlWithExtraParameters()
- {
- $this->routeCollection->add('test', new Route('/testing'));
- $this->generator->setContext(array(
- 'base_url'=>'/app.php',
- 'method'=>'GET',
- 'host'=>'localhost',
- 'port'=>80,
- 'is_secure'=>false));
- $url = $this->generator->generate('test', array('foo' => 'bar'), true);
- $this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testGenerateWithoutRoutes()
- {
- $this->generator->generate('test', array(), true);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testGenerateForRouteWithoutManditoryParameter()
- {
- $this->routeCollection->add('test', new Route('/testing/{foo}'));
- $this->generator->generate('test', array(), true);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testGenerateForRouteWithInvalidOptionalParameter()
- {
- $route = new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'));
- $this->routeCollection->add('test', $route);
- $this->generator->generate('test', array('foo' => 'bar'), true);
- }
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testGenerateForRouteWithInvalidManditoryParameter()
- {
- $route = new Route('/testing/{foo}', array(), array('foo' => 'd+'));
- $this->routeCollection->add('test', $route);
- $this->generator->generate('test', array('foo' => 'bar'), true);
- }
- }
|