Skip to content

Commit e633e1a

Browse files
author
Chirs Warner
committed
Created Unit tests for getRoutesByName with defined idPrefix.
Fixed getRoutesByName to work if $idPrefix is not defined. Added Tests to cover those cases.
1 parent d20225e commit e633e1a

File tree

3 files changed

+119
-13
lines changed

3 files changed

+119
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ composer.lock
33
vendor
44
Tests/Resources/app/cache
55
Tests/Resources/app/logs
6+
.idea

Doctrine/Phpcr/RouteProvider.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,13 @@ protected function getCandidates($url)
119119
*/
120120
public function getRouteByName($name, $parameters = array())
121121
{
122+
122123
// $name is the route document path
123-
if ($this->idPrefix && 0 === strpos($name, $this->idPrefix)) {
124+
if ( '' === $this->idPrefix || 0 === strpos($name, $this->idPrefix) ) {
124125
$route = $this->getObjectManager()->find($this->className, $name);
125126
}
126127

127-
if (empty($route)) {
128+
if (!$route) {
128129
throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name));
129130
}
130131

@@ -140,15 +141,11 @@ public function getRouteByName($name, $parameters = array())
140141
*/
141142
public function getRoutesByNames($names, $parameters = array())
142143
{
143-
if ($this->idPrefix) {
144-
$routes = array();
145-
foreach ($names as $name) {
146-
if (0 === strpos($name, $this->idPrefix)) {
147-
$routes[] = $name;
148-
}
144+
$routes = array();
145+
foreach ($names as $name) {
146+
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
147+
$routes[] = $name;
149148
}
150-
} else {
151-
$routes = $names;
152149
}
153150

154151
$collection = $this->getObjectManager()->findMany($this->className, $routes);

Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function testGetRouteByName()
6666
$routeProvider = new RouteProvider($this->managerRegistry);
6767
$routeProvider->setManagerName('default');
6868

69+
$routeProvider->setPrefix('/cms/routes/');
6970
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
7071

7172
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
@@ -92,7 +93,7 @@ public function testGetRouteByNameNotFound()
9293

9394
$routeProvider = new RouteProvider($this->managerRegistry);
9495
$routeProvider->setManagerName('default');
95-
96+
$routeProvider->setPrefix('/cms/routes/');
9697
$routeProvider->getRouteByName('/cms/routes/test-route');
9798
}
9899

@@ -116,15 +117,120 @@ public function testGetRouteByNameNoRoute()
116117

117118
$routeProvider = new RouteProvider($this->managerRegistry);
118119
$routeProvider->setManagerName('default');
120+
$routeProvider->setPrefix('/cms/routes/');
119121

120122
$routeProvider->getRouteByName('/cms/routes/test-route');
121123
}
122124

123-
public function testGetRoutesByNames()
125+
/**
126+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
127+
*/
128+
public function testGetRouteByNameInvalidRoute()
124129
{
125-
$this->markTestIncomplete();
130+
$this->objectManager
131+
->expects($this->never())
132+
->method('find')
133+
;
134+
135+
$this->managerRegistry
136+
->expects($this->any())
137+
->method('getManager')
138+
->will($this->returnValue($this->objectManager))
139+
;
140+
141+
$routeProvider = new RouteProvider($this->managerRegistry);
142+
$routeProvider->setManagerName('default');
143+
144+
$routeProvider->setPrefix('/cms/routes');
145+
146+
$routeProvider->getRouteByName('invalid_route');
147+
148+
}
149+
150+
public function testGetRouteByNameIdPrefixEmptyString()
151+
{
152+
153+
$this->route
154+
->expects($this->any())
155+
->method('getPath')
156+
->will($this->returnValue('/cms/routes/test-route'))
157+
;
158+
159+
$this->objectManager
160+
->expects($this->any())
161+
->method('find')
162+
->with(null, '/cms/routes/test-route')
163+
->will($this->returnValue($this->route))
164+
;
165+
166+
$this->managerRegistry
167+
->expects($this->any())
168+
->method('getManager')
169+
->will($this->returnValue($this->objectManager))
170+
;
171+
172+
$routeProvider = new RouteProvider($this->managerRegistry);
173+
$routeProvider->setManagerName('default');
174+
175+
$routeProvider->setPrefix('');
176+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
177+
178+
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
179+
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
180+
126181
}
127182

183+
184+
/**
185+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
186+
*/
187+
public function testGetRouteByNameNotFoundIdPrefixEmptyString()
188+
{
189+
$this->objectManager
190+
->expects($this->any())
191+
->method('find')
192+
->with(null, '/cms/routes/test-route')
193+
->will($this->returnValue(null))
194+
;
195+
196+
$this->managerRegistry
197+
->expects($this->any())
198+
->method('getManager')
199+
->will($this->returnValue($this->objectManager))
200+
;
201+
202+
$routeProvider = new RouteProvider($this->managerRegistry);
203+
$routeProvider->setManagerName('default');
204+
$routeProvider->setPrefix('');
205+
$routeProvider->getRouteByName('/cms/routes/test-route');
206+
}
207+
208+
/**
209+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
210+
*/
211+
public function testGetRouteByNameNoRoutePrefixEmptyString()
212+
{
213+
$this->objectManager
214+
->expects($this->any())
215+
->method('find')
216+
->with(null, '/cms/routes/test-route')
217+
->will($this->returnValue($this))
218+
;
219+
220+
$this->managerRegistry
221+
->expects($this->any())
222+
->method('getManager')
223+
->will($this->returnValue($this->objectManager))
224+
;
225+
226+
$routeProvider = new RouteProvider($this->managerRegistry);
227+
$routeProvider->setManagerName('default');
228+
$routeProvider->setPrefix('');
229+
230+
$routeProvider->getRouteByName('/cms/routes/test-route');
231+
}
232+
233+
128234
/**
129235
* Use getRouteByName() with two different document managers.
130236
* The two document managers will return different route objects when searching for the same path.
@@ -173,6 +279,8 @@ function ($name) use ($objectManagers) {
173279
$routeProvider = new RouteProvider($this->managerRegistry);
174280

175281
$routeProvider->setManagerName('default');
282+
$routeProvider->setPrefix('/cms/routes/');
283+
176284
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
177285
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
178286
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());

0 commit comments

Comments
 (0)