Skip to content

Commit b964de2

Browse files
committed
Improved test coverage
1 parent 12b2090 commit b964de2

File tree

4 files changed

+130
-1
lines changed

4 files changed

+130
-1
lines changed

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<file>./src/DependencyInjection/ExtendableInterface.php</file>
2424
<file>./src/Router/DispatcherFactoryInterface.php</file>
2525
<file>./src/Router/RouteCollectorInterface.php</file>
26+
<file>./src/Router/RouteMapperInterface.php</file>
2627
<file>./src/DependencyInjection/ContainerInitializerInterface.php</file>
2728
<file>./src/Security/AuthenticatorInterface.php</file>
2829
<file>./src/Router/NamedDataGeneratorInterface.php</file>

src/Router/UrlGenerator/SimpleUrlGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ public function generate($name, array $parameters = array(), $absolute = false)
7070
}
7171
}
7272

73-
return ($this->request ? (($absolute ? $this->request->getSchemeAndHttpHost() : '').$this->request->getBaseUrl()) : '').$path;
73+
if ($this->request) {
74+
$path = $this->request->getBaseUrl().$path;
75+
76+
if ($absolute) {
77+
$path = $this->request->getSchemeAndHttpHost().$path;
78+
}
79+
}
80+
81+
return $path;
7482
}
7583

7684
/**

tests/Router/RouterSubscriberTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,22 @@ public function testMethodNotAllowed()
6363
$subscriber->onKernelRequest($event);
6464
}
6565

66+
/**
67+
* Test a Request that already has a _controller
68+
*/
69+
public function testRequestAlreadyResolved()
70+
{
71+
$subscriber = $this->getSubscriber();
72+
$request = Request::create('/', 'POST');
73+
$request->attributes->set('_controller', 'handler0');
74+
75+
$event = $this->getEvent($request);
76+
77+
$subscriber->onKernelRequest($event);
78+
79+
$this->assertEquals('handler0', $request->attributes->get('_controller'));
80+
}
81+
6682
/**
6783
* Test not found
6884
*/
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
3+
/*
4+
* Copyright (c) Tyler Sommer
5+
*
6+
* For the full copyright and license information, please view the LICENSE file
7+
* that was distributed with this source code.
8+
*/
9+
10+
namespace Nice\Tests\Router\UrlGenerator;
11+
12+
use Nice\Router\UrlGenerator\SimpleUrlGenerator;
13+
use Symfony\Component\HttpFoundation\Request;
14+
15+
class SimpleUrlGeneratorTest extends \PHPUnit_Framework_TestCase
16+
{
17+
/**
18+
* Test basic functionality
19+
*/
20+
public function testFunctionality()
21+
{
22+
$generator = $this->getGenerator();
23+
24+
$this->assertEquals('/', $generator->generate('home'));
25+
}
26+
27+
/**
28+
* Test base URL functionality
29+
*/
30+
public function testBaseUrl()
31+
{
32+
$generator = $this->getGenerator();
33+
34+
$request = Request::create('https://www.example.com/subdirectory/somepage', 'GET', array(), array(), array(), array(
35+
'SCRIPT_FILENAME' => 'index.php',
36+
'PHP_SELF' => '/subdirectory/index.php'
37+
));
38+
$generator->setRequest($request);
39+
40+
$this->assertEquals('/subdirectory/', $generator->generate('home'));
41+
}
42+
43+
/**
44+
* Test absolute URL functionality
45+
*/
46+
public function testAbsoluteUrl()
47+
{
48+
$generator = $this->getGenerator();
49+
50+
$request = Request::create('https://www.example.com/subdirectory/somepage', 'GET', array(), array(), array(), array(
51+
'SCRIPT_FILENAME' => 'index.php',
52+
'PHP_SELF' => '/subdirectory/index.php'
53+
));
54+
$generator->setRequest($request);
55+
56+
$this->assertEquals('https://www.example.com/subdirectory/', $generator->generate('home', array(), true));
57+
}
58+
59+
/**
60+
* Test a dynamic route
61+
*/
62+
public function testDynamicRoute()
63+
{
64+
$generator = $this->getGenerator(array(
65+
'user_edit' => array(
66+
'params' => array(
67+
'id'
68+
),
69+
'path' => '/user/{id}/edit'
70+
)
71+
));
72+
73+
$this->assertEquals('/user/123/edit', $generator->generate('user_edit', array('id' => 123)));
74+
}
75+
76+
/**
77+
* Test a dynamic route with a missing parameter
78+
*/
79+
public function testDynamicRouteWithMissingParameter()
80+
{
81+
$generator = $this->getGenerator(array(
82+
'user_edit' => array(
83+
'params' => array(
84+
'id'
85+
),
86+
'path' => '/user/{id}/edit'
87+
)
88+
));
89+
90+
$this->setExpectedException('RuntimeException', 'Missing required parameter');
91+
92+
$this->assertEquals('/user/123/edit', $generator->generate('user_edit'));
93+
}
94+
95+
private function getGenerator(array $routes = array('home' => '/'))
96+
{
97+
$dataGenerator = $this->getMockForAbstractClass('Nice\Router\UrlGenerator\DataGeneratorInterface');
98+
$dataGenerator->expects($this->once())
99+
->method('getData')
100+
->will($this->returnValue($routes));
101+
102+
return new SimpleUrlGenerator($dataGenerator);
103+
}
104+
}

0 commit comments

Comments
 (0)