4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Routing\Tests\Generator\Dumper;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16 use Symfony\Component\Routing\RouteCollection;
17 use Symfony\Component\Routing\Route;
18 use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
19 use Symfony\Component\Routing\RequestContext;
21 class PhpGeneratorDumperTest extends TestCase
24 * @var RouteCollection
26 private $routeCollection;
29 * @var PhpGeneratorDumper
31 private $generatorDumper;
36 private $testTmpFilepath;
41 private $largeTestTmpFilepath;
43 protected function setUp()
47 $this->routeCollection = new RouteCollection();
48 $this->generatorDumper = new PhpGeneratorDumper($this->routeCollection);
49 $this->testTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.php';
50 $this->largeTestTmpFilepath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'php_generator.'.$this->getName().'.large.php';
51 @unlink($this->testTmpFilepath);
52 @unlink($this->largeTestTmpFilepath);
55 protected function tearDown()
59 @unlink($this->testTmpFilepath);
61 $this->routeCollection = null;
62 $this->generatorDumper = null;
63 $this->testTmpFilepath = null;
66 public function testDumpWithRoutes()
68 $this->routeCollection->add('Test', new Route('/testing/{foo}'));
69 $this->routeCollection->add('Test2', new Route('/testing2'));
71 file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
72 include $this->testTmpFilepath;
74 $projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
76 $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
77 $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
78 $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
79 $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
81 $this->assertEquals('http://localhost/app.php/testing/bar', $absoluteUrlWithParameter);
82 $this->assertEquals('http://localhost/app.php/testing2', $absoluteUrlWithoutParameter);
83 $this->assertEquals('/app.php/testing/bar', $relativeUrlWithParameter);
84 $this->assertEquals('/app.php/testing2', $relativeUrlWithoutParameter);
87 public function testDumpWithTooManyRoutes()
89 if (defined('HHVM_VERSION_ID')) {
90 $this->markTestSkipped('HHVM consumes too much memory on this test.');
93 $this->routeCollection->add('Test', new Route('/testing/{foo}'));
94 for ($i = 0; $i < 32769; ++$i) {
95 $this->routeCollection->add('route_'.$i, new Route('/route_'.$i));
97 $this->routeCollection->add('Test2', new Route('/testing2'));
99 file_put_contents($this->largeTestTmpFilepath, $this->generatorDumper->dump(array(
100 'class' => 'ProjectLargeUrlGenerator',
102 $this->routeCollection = $this->generatorDumper = null;
103 include $this->largeTestTmpFilepath;
105 $projectUrlGenerator = new \ProjectLargeUrlGenerator(new RequestContext('/app.php'));
107 $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_URL);
108 $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_URL);
109 $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), UrlGeneratorInterface::ABSOLUTE_PATH);
110 $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
112 $this->assertEquals('http://localhost/app.php/testing/bar', $absoluteUrlWithParameter);
113 $this->assertEquals('http://localhost/app.php/testing2', $absoluteUrlWithoutParameter);
114 $this->assertEquals('/app.php/testing/bar', $relativeUrlWithParameter);
115 $this->assertEquals('/app.php/testing2', $relativeUrlWithoutParameter);
119 * @expectedException \InvalidArgumentException
121 public function testDumpWithoutRoutes()
123 file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
124 include $this->testTmpFilepath;
126 $projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
128 $projectUrlGenerator->generate('Test', array());
132 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
134 public function testGenerateNonExistingRoute()
136 $this->routeCollection->add('Test', new Route('/test'));
138 file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
139 include $this->testTmpFilepath;
141 $projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
142 $url = $projectUrlGenerator->generate('NonExisting', array());
145 public function testDumpForRouteWithDefaults()
147 $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
149 file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
150 include $this->testTmpFilepath;
152 $projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
153 $url = $projectUrlGenerator->generate('Test', array());
155 $this->assertEquals('/testing', $url);
158 public function testDumpWithSchemeRequirement()
160 $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
162 file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator')));
163 include $this->testTmpFilepath;
165 $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
167 $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
168 $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
170 $this->assertEquals('ftp://localhost/app.php/testing', $absoluteUrl);
171 $this->assertEquals('ftp://localhost/app.php/testing', $relativeUrl);
173 $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
175 $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
176 $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
178 $this->assertEquals('https://localhost/app.php/testing', $absoluteUrl);
179 $this->assertEquals('/app.php/testing', $relativeUrl);