Skip to content

Commit a95f72f

Browse files
committed
Merge remote branch 'l3l0/routing-tests'
* l3l0/routing-tests: [Routing] added tearDown with cleanup the file system tmp directory [Routing] moved clean up the directory from tearDown to setUp. Removed setUpBeforeClass method [Routing] modified place when we store temporary file [Routing] modified unit test for PhpGeneratorDumper class [Routing] add unit test for PhpGeneratorDumper class [Routing] added more tests for UrlGenerator class
2 parents e159c47 + 05a105f commit a95f72f

File tree

2 files changed

+208
-1
lines changed

2 files changed

+208
-1
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,101 @@ public function testAbsoluteSecureUrlWithNonStandardPort()
8989

9090
$this->assertEquals('https://localhost:8080/app.php/testing', $url);
9191
}
92-
}
92+
93+
public function testRelativeUrlWithoutParameters()
94+
{
95+
$this->routeCollection->add('test', new Route('/testing'));
96+
$this->generator->setContext(array(
97+
'base_url'=>'/app.php',
98+
'method'=>'GET',
99+
'host'=>'localhost',
100+
'port'=>80,
101+
'is_secure'=>false));
102+
103+
$url = $this->generator->generate('test', array(), false);
104+
105+
$this->assertEquals('/app.php/testing', $url);
106+
}
107+
108+
public function testRelativeUrlWithParameter()
109+
{
110+
$this->routeCollection->add('test', new Route('/testing/{foo}'));
111+
$this->generator->setContext(array(
112+
'base_url'=>'/app.php',
113+
'method'=>'GET',
114+
'host'=>'localhost',
115+
'port'=>80,
116+
'is_secure'=>false));
117+
118+
$url = $this->generator->generate('test', array('foo' => 'bar'), false);
119+
120+
$this->assertEquals('/app.php/testing/bar', $url);
121+
}
122+
123+
public function testRelativeUrlWithExtraParameters()
124+
{
125+
$this->routeCollection->add('test', new Route('/testing'));
126+
$this->generator->setContext(array(
127+
'base_url'=>'/app.php',
128+
'method'=>'GET',
129+
'host'=>'localhost',
130+
'port'=>80,
131+
'is_secure'=>false));
132+
133+
$url = $this->generator->generate('test', array('foo' => 'bar'), false);
134+
135+
$this->assertEquals('/app.php/testing?foo=bar', $url);
136+
}
137+
138+
public function testAbsoluteUrlWithExtraParameters()
139+
{
140+
$this->routeCollection->add('test', new Route('/testing'));
141+
$this->generator->setContext(array(
142+
'base_url'=>'/app.php',
143+
'method'=>'GET',
144+
'host'=>'localhost',
145+
'port'=>80,
146+
'is_secure'=>false));
147+
148+
$url = $this->generator->generate('test', array('foo' => 'bar'), true);
149+
150+
$this->assertEquals('http://localhost/app.php/testing?foo=bar', $url);
151+
}
152+
153+
/**
154+
* @expectedException \InvalidArgumentException
155+
*/
156+
public function testGenerateWithoutRoutes()
157+
{
158+
$this->generator->generate('test', array(), true);
159+
}
160+
161+
/**
162+
* @expectedException \InvalidArgumentException
163+
*/
164+
public function testGenerateForRouteWithoutManditoryParameter()
165+
{
166+
$this->routeCollection->add('test', new Route('/testing/{foo}'));
167+
$this->generator->generate('test', array(), true);
168+
}
169+
170+
/**
171+
* @expectedException \InvalidArgumentException
172+
*/
173+
public function testGenerateForRouteWithInvalidOptionalParameter()
174+
{
175+
$route = new Route('/testing/{foo}', array('foo' => '1'), array('foo' => 'd+'));
176+
$this->routeCollection->add('test', $route);
177+
$this->generator->generate('test', array('foo' => 'bar'), true);
178+
}
179+
180+
/**
181+
* @expectedException \InvalidArgumentException
182+
*/
183+
public function testGenerateForRouteWithInvalidManditoryParameter()
184+
{
185+
$route = new Route('/testing/{foo}', array(), array('foo' => 'd+'));
186+
$this->routeCollection->add('test', $route);
187+
$this->generator->generate('test', array('foo' => 'bar'), true);
188+
}
189+
}

0 commit comments

Comments
 (0)