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 Symfony\Component\Routing\RouteCollection
;
15 use Symfony\Component\Routing\Route
;
16 use Symfony\Component\Routing\Generator\Dumper\PhpGeneratorDumper
;
17 use Symfony\Component\Routing\RequestContext
;
19 class PhpGeneratorDumperTest
extends \PHPUnit_Framework_TestCase
22 * @var RouteCollection
24 private $routeCollection;
27 * @var PhpGeneratorDumper
29 private $generatorDumper;
34 private $testTmpFilepath;
36 protected function setUp()
40 $this->routeCollection
= new RouteCollection();
41 $this->generatorDumper
= new PhpGeneratorDumper($this->routeCollection
);
42 $this->testTmpFilepath
= sys_get_temp_dir().DIRECTORY_SEPARATOR
.'php_generator.php';
43 @unlink($this->testTmpFilepath
);
46 protected function tearDown()
50 @unlink($this->testTmpFilepath
);
52 $this->routeCollection
= null;
53 $this->generatorDumper
= null;
54 $this->testTmpFilepath
= null;
57 public function testDumpWithRoutes()
59 $this->routeCollection
->add('Test', new Route('/testing/{foo}'));
60 $this->routeCollection
->add('Test2', new Route('/testing2'));
62 file_put_contents($this->testTmpFilepath
, $this->generatorDumper
->dump());
63 include ($this->testTmpFilepath
);
65 $projectUrlGenerator = new \
ProjectUrlGenerator(new RequestContext('/app.php'));
67 $absoluteUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), true);
68 $absoluteUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), true);
69 $relativeUrlWithParameter = $projectUrlGenerator->generate('Test', array('foo' => 'bar'), false);
70 $relativeUrlWithoutParameter = $projectUrlGenerator->generate('Test2', array(), false);
72 $this->assertEquals($absoluteUrlWithParameter, 'http://localhost/app.php/testing/bar');
73 $this->assertEquals($absoluteUrlWithoutParameter, 'http://localhost/app.php/testing2');
74 $this->assertEquals($relativeUrlWithParameter, '/app.php/testing/bar');
75 $this->assertEquals($relativeUrlWithoutParameter, '/app.php/testing2');
79 * @expectedException \InvalidArgumentException
81 public function testDumpWithoutRoutes()
83 file_put_contents($this->testTmpFilepath
, $this->generatorDumper
->dump(array('class' => 'WithoutRoutesUrlGenerator')));
84 include ($this->testTmpFilepath
);
86 $projectUrlGenerator = new \
WithoutRoutesUrlGenerator(new RequestContext('/app.php'));
88 $projectUrlGenerator->generate('Test', array());
92 * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
94 public function testGenerateNonExistingRoute()
96 $this->routeCollection
->add('Test', new Route('/test'));
98 file_put_contents($this->testTmpFilepath
, $this->generatorDumper
->dump(array('class' => 'NonExistingRoutesUrlGenerator')));
99 include ($this->testTmpFilepath
);
101 $projectUrlGenerator = new \
NonExistingRoutesUrlGenerator(new RequestContext());
102 $url = $projectUrlGenerator->generate('NonExisting', array());
105 public function testDumpForRouteWithDefaults()
107 $this->routeCollection
->add('Test', new Route('/testing/{foo}', array('foo' => 'bar')));
109 file_put_contents($this->testTmpFilepath
, $this->generatorDumper
->dump(array('class' => 'DefaultRoutesUrlGenerator')));
110 include ($this->testTmpFilepath
);
112 $projectUrlGenerator = new \
DefaultRoutesUrlGenerator(new RequestContext());
113 $url = $projectUrlGenerator->generate('Test', array());
115 $this->assertEquals($url, '/testing');