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
12namespace Symfony\Component\Routing\Tests\Generator\Dumper;
13
14use PHPUnit\Framework\TestCase;
15use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
16use Symfony\Component\Routing\RouteCollection;
17use Symfony\Component\Routing\Route;
18use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper;
19use Symfony\Component\Routing\RequestContext;
20
21class PhpGeneratorDumperTest extends TestCase
22{
23    /**
24     * @var RouteCollection
25     */
26    private $routeCollection;
27
28    /**
29     * @var PhpGeneratorDumper
30     */
31    private $generatorDumper;
32
33    /**
34     * @var string
35     */
36    private $testTmpFilepath;
37
38    /**
39     * @var string
40     */
41    private $largeTestTmpFilepath;
42
43    protected function setUp()
44    {
45        parent::setUp();
46
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);
53    }
54
55    protected function tearDown()
56    {
57        parent::tearDown();
58
59        @unlink($this->testTmpFilepath);
60
61        $this->routeCollection = null;
62        $this->generatorDumper = null;
63        $this->testTmpFilepath = null;
64    }
65
66    public function testDumpWithRoutes()
67    {
68        $this->routeCollection->add('Test', new Route('/testing/{foo}'));
69        $this->routeCollection->add('Test2', new Route('/testing2'));
70
71        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump());
72        include $this->testTmpFilepath;
73
74        $projectUrlGenerator = new \ProjectUrlGenerator(new RequestContext('/app.php'));
75
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);
80
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);
85    }
86
87    public function testDumpWithTooManyRoutes()
88    {
89        if (defined('HHVM_VERSION_ID')) {
90            $this->markTestSkipped('HHVM consumes too much memory on this test.');
91        }
92
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));
96        }
97        $this->routeCollection->add('Test2', new Route('/testing2'));
98
99        file_put_contents($this->largeTestTmpFilepath, $this->generatorDumper->dump(array(
100            'class' => 'ProjectLargeUrlGenerator',
101        )));
102        $this->routeCollection = $this->generatorDumper = null;
103        include $this->largeTestTmpFilepath;
104
105        $projectUrlGenerator = new \ProjectLargeUrlGenerator(new RequestContext('/app.php'));
106
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);
111
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);
116    }
117
118    /**
119     * @expectedException \InvalidArgumentException
120     */
121    public function testDumpWithoutRoutes()
122    {
123        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'WithoutRoutesUrlGenerator')));
124        include $this->testTmpFilepath;
125
126        $projectUrlGenerator = new \WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
127
128        $projectUrlGenerator->generate('Test', array());
129    }
130
131    /**
132     * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
133     */
134    public function testGenerateNonExistingRoute()
135    {
136        $this->routeCollection->add('Test', new Route('/test'));
137
138        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
139        include $this->testTmpFilepath;
140
141        $projectUrlGenerator = new \NonExistingRoutesUrlGenerator(new RequestContext());
142        $url = $projectUrlGenerator->generate('NonExisting', array());
143    }
144
145    public function testDumpForRouteWithDefaults()
146    {
147        $this->routeCollection->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
148
149        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'DefaultRoutesUrlGenerator')));
150        include $this->testTmpFilepath;
151
152        $projectUrlGenerator = new \DefaultRoutesUrlGenerator(new RequestContext());
153        $url = $projectUrlGenerator->generate('Test', array());
154
155        $this->assertEquals('/testing', $url);
156    }
157
158    public function testDumpWithSchemeRequirement()
159    {
160        $this->routeCollection->add('Test1', new Route('/testing', array(), array(), array(), '', array('ftp', 'https')));
161
162        file_put_contents($this->testTmpFilepath, $this->generatorDumper->dump(array('class' => 'SchemeUrlGenerator')));
163        include $this->testTmpFilepath;
164
165        $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php'));
166
167        $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
168        $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
169
170        $this->assertEquals('ftp://localhost/app.php/testing', $absoluteUrl);
171        $this->assertEquals('ftp://localhost/app.php/testing', $relativeUrl);
172
173        $projectUrlGenerator = new \SchemeUrlGenerator(new RequestContext('/app.php', 'GET', 'localhost', 'https'));
174
175        $absoluteUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_URL);
176        $relativeUrl = $projectUrlGenerator->generate('Test1', array(), UrlGeneratorInterface::ABSOLUTE_PATH);
177
178        $this->assertEquals('https://localhost/app.php/testing', $absoluteUrl);
179        $this->assertEquals('/app.php/testing', $relativeUrl);
180    }
181}
182