Skip to content

Commit 7db07d9

Browse files
committed
[Routing] added tests for generating relative paths and network paths
1 parent 75f59eb commit 7db07d9

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Routing\RouteCollection;
1515
use Symfony\Component\Routing\Route;
1616
use Symfony\Component\Routing\Generator\UrlGenerator;
17+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1718
use Symfony\Component\Routing\RequestContext;
1819

1920
class UrlGeneratorTest extends \PHPUnit_Framework_TestCase
@@ -439,6 +440,58 @@ public function testUrlWithInvalidParameterInHostnameInNonStrictMode()
439440
$this->assertNull($generator->generate('test', array('foo' => 'baz'), false));
440441
}
441442

443+
public function testGenerateNetworkPath()
444+
{
445+
$routes = $this->getRoutes('test', new Route('/{name}', array(), array('_scheme' => 'http'), array(), '{locale}.example.com'));
446+
447+
$this->assertSame('//fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
448+
array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'network path with different host'
449+
);
450+
$this->assertSame('//fr.example.com/app.php/Fabien?query=string', $this->getGenerator($routes, array('host' => 'fr.example.com'))->generate('test',
451+
array('name' =>'Fabien', 'locale' => 'fr', 'query' => 'string'), UrlGeneratorInterface::NETWORK_PATH), 'network path although host same as context'
452+
);
453+
$this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes, array('scheme' => 'https'))->generate('test',
454+
array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::NETWORK_PATH), 'absolute URL because scheme requirement does not match context'
455+
);
456+
$this->assertSame('http://fr.example.com/app.php/Fabien', $this->getGenerator($routes)->generate('test',
457+
array('name' =>'Fabien', 'locale' => 'fr'), UrlGeneratorInterface::ABSOLUTE_URL), 'absolute URL with same scheme because it is requested'
458+
);
459+
}
460+
461+
public function testGenerateRelativePath()
462+
{
463+
$routes = new RouteCollection();
464+
$routes->add('article', new Route('/{author}/{article}/'));
465+
$routes->add('comments', new Route('/{author}/{article}/comments'));
466+
$routes->add('hostname', new Route('/{article}', array(), array(), array(), '{author}.example.com'));
467+
$routes->add('scheme', new Route('/{author}', array(), array('_scheme' => 'https')));
468+
$routes->add('unrelated', new Route('/about'));
469+
470+
$generator = $this->getGenerator($routes, array('host' => 'example.com', 'pathInfo' => '/fabien/symfony-is-great/'));
471+
472+
$this->assertSame('comments', $generator->generate('comments',
473+
array('author' =>'fabien', 'article' => 'symfony-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
474+
);
475+
$this->assertSame('comments?page=2', $generator->generate('comments',
476+
array('author' =>'fabien', 'article' => 'symfony-is-great', 'page' => 2), UrlGeneratorInterface::RELATIVE_PATH)
477+
);
478+
$this->assertSame('../twig-is-great/', $generator->generate('article',
479+
array('author' =>'fabien', 'article' => 'twig-is-great'), UrlGeneratorInterface::RELATIVE_PATH)
480+
);
481+
$this->assertSame('../../bernhard/forms-are-great/', $generator->generate('article',
482+
array('author' =>'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH)
483+
);
484+
$this->assertSame('//bernhard.example.com/app.php/forms-are-great', $generator->generate('hostname',
485+
array('author' =>'bernhard', 'article' => 'forms-are-great'), UrlGeneratorInterface::RELATIVE_PATH)
486+
);
487+
$this->assertSame('https://example.com/app.php/bernhard', $generator->generate('scheme',
488+
array('author' =>'bernhard'), UrlGeneratorInterface::RELATIVE_PATH)
489+
);
490+
$this->assertSame('../../about', $generator->generate('unrelated',
491+
array(), UrlGeneratorInterface::RELATIVE_PATH)
492+
);
493+
}
494+
442495
/**
443496
* @dataProvider provideRelativePaths
444497
*/

0 commit comments

Comments
 (0)