|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Tests\Component\Routing\Generator\Dumper\PhpGeneratorDumper; |
| 13 | + |
| 14 | +use Symfony\Component\Routing\RouteCollection; |
| 15 | +use Symfony\Component\Routing\Route; |
| 16 | +use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper; |
| 17 | + |
| 18 | +class PhpGeneratorDumperTest extends \PHPUnit_Framework_TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var RouteCollection |
| 22 | + */ |
| 23 | + private $routeCollection; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var PhpGeneratorDumper |
| 27 | + */ |
| 28 | + private $generatorDumper; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var string |
| 32 | + */ |
| 33 | + private $testTmpFilepath; |
| 34 | + |
| 35 | + protected function setUp() |
| 36 | + { |
| 37 | + parent::setUp(); |
| 38 | + |
| 39 | + $this->routeCollection = new RouteCollection(); |
| 40 | + $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection); |
| 41 | + $this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.php'; |
| 42 | + @unlink($this->testTmpFilepath); |
| 43 | + } |
| 44 | + |
| 45 | + protected function tearDown() |
| 46 | + { |
| 47 | + parent::tearDown(); |
| 48 | + |
| 49 | + @unlink($this->testTmpFilepath); |
| 50 | + } |
| 51 | + |
| 52 | + public function testDumpWithRoutes() |
| 53 | + { |
| 54 | + $this->routeCollection->add('Test', new Route('/testing/{foo}')); |
| 55 | + $this->routeCollection->add('Test2', new Route('/testing2')); |
| 56 | + |
| 57 | + file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump()); |
| 58 | + include ($this->testTmpFilepath); |
| 59 | + |
| 60 | + $projectUrlGenerator = new \ProjectUrlGenerator(array( |
| 61 | + 'base_url' => '/app.php', |
| 62 | + 'method' => 'GET', |
| 63 | + 'host' => 'localhost', |
| 64 | + 'port' => 80, |
| 65 | + 'is_secure' => false |
| 66 | + )); |
| 67 | + |
| 68 | + $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true); |
| 69 | + $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true); |
| 70 | + $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), false); |
| 71 | + $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), false); |
| 72 | + |
| 73 | + $this->assertEquals($absoluteUrlWithParameter, 'http://localhost/app.php/testing/bar'); |
| 74 | + $this->assertEquals($absoluteUrlWithoutParameter, 'http://localhost/app.php/testing2'); |
| 75 | + $this->assertEquals($relativeUrlWithParameter, '/app.php/testing/bar'); |
| 76 | + $this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @expectedException \InvalidArgumentException |
| 81 | + */ |
| 82 | + public function testDumpWithoutRoutes() |
| 83 | + { |
| 84 | + file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator'))); |
| 85 | + include ($this->testTmpFilepath); |
| 86 | + |
| 87 | + $projectUrlGenerator = new \WithoutRoutesUrlGenerator(array( |
| 88 | + 'base_url' => '/app.php', |
| 89 | + 'method' => 'GET', |
| 90 | + 'host' => 'localhost', |
| 91 | + 'port' => 80, |
| 92 | + 'is_secure' => false |
| 93 | + )); |
| 94 | + |
| 95 | + $projectUrlGenerator->generate('Test', array()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testDumpForRouteWithDefaults() |
| 99 | + { |
| 100 | + $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar'))); |
| 101 | + |
| 102 | + file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator'))); |
| 103 | + include ($this->testTmpFilepath); |
| 104 | + |
| 105 | + $projectUrlGenerator = new \DefaultRoutesUrlGenerator(array()); |
| 106 | + $url = $projectUrlGenerator->generate('Test', array()); |
| 107 | + |
| 108 | + $this->assertEquals($url, '/testing'); |
| 109 | + } |
| 110 | +} |
0 commit comments