Skip to content

Commit b1b3bb6

Browse files
committed
Merge pull request #189 from cdwlighting/optimize_get_route_by_name
Created Unit tests for getRoutesByName with defined idPrefix.
2 parents d20225e + 9d07008 commit b1b3bb6

File tree

2 files changed

+115
-5
lines changed

2 files changed

+115
-5
lines changed

Doctrine/Phpcr/RouteProvider.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ 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

@@ -140,10 +141,10 @@ public function getRouteByName($name, $parameters = array())
140141
*/
141142
public function getRoutesByNames($names, $parameters = array())
142143
{
143-
if ($this->idPrefix) {
144-
$routes = array();
144+
$routes = array();
145+
if ('' === $this->idPrefix) {
145146
foreach ($names as $name) {
146-
if (0 === strpos($name, $this->idPrefix)) {
147+
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
147148
$routes[] = $name;
148149
}
149150
}

Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php

Lines changed: 110 additions & 1 deletion
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,6 +117,112 @@ public function testGetRouteByNameNoRoute()
116117

117118
$routeProvider = new RouteProvider($this->managerRegistry);
118119
$routeProvider->setManagerName('default');
120+
$routeProvider->setPrefix('/cms/routes/');
121+
$routeProvider->getRouteByName('/cms/routes/test-route');
122+
}
123+
124+
/**
125+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
126+
*/
127+
public function testGetRouteByNameInvalidRoute()
128+
{
129+
$this->objectManager
130+
->expects($this->never())
131+
->method('find')
132+
;
133+
134+
$this->managerRegistry
135+
->expects($this->any())
136+
->method('getManager')
137+
->will($this->returnValue($this->objectManager))
138+
;
139+
140+
$routeProvider = new RouteProvider($this->managerRegistry);
141+
$routeProvider->setManagerName('default');
142+
143+
$routeProvider->setPrefix('/cms/routes');
144+
145+
$routeProvider->getRouteByName('invalid_route');
146+
}
147+
148+
public function testGetRouteByNameIdPrefixEmptyString()
149+
{
150+
151+
$this->route
152+
->expects($this->any())
153+
->method('getPath')
154+
->will($this->returnValue('/cms/routes/test-route'))
155+
;
156+
157+
$this->objectManager
158+
->expects($this->any())
159+
->method('find')
160+
->with(null, '/cms/routes/test-route')
161+
->will($this->returnValue($this->route))
162+
;
163+
164+
$this->managerRegistry
165+
->expects($this->any())
166+
->method('getManager')
167+
->will($this->returnValue($this->objectManager))
168+
;
169+
170+
$routeProvider = new RouteProvider($this->managerRegistry);
171+
$routeProvider->setManagerName('default');
172+
173+
$routeProvider->setPrefix('');
174+
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
175+
176+
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
177+
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
178+
}
179+
180+
181+
/**
182+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
183+
*/
184+
public function testGetRouteByNameNotFoundIdPrefixEmptyString()
185+
{
186+
$this->objectManager
187+
->expects($this->any())
188+
->method('find')
189+
->with(null, '/cms/routes/test-route')
190+
->will($this->returnValue(null))
191+
;
192+
193+
$this->managerRegistry
194+
->expects($this->any())
195+
->method('getManager')
196+
->will($this->returnValue($this->objectManager))
197+
;
198+
199+
$routeProvider = new RouteProvider($this->managerRegistry);
200+
$routeProvider->setManagerName('default');
201+
$routeProvider->setPrefix('');
202+
$routeProvider->getRouteByName('/cms/routes/test-route');
203+
}
204+
205+
/**
206+
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
207+
*/
208+
public function testGetRouteByNameNoRoutePrefixEmptyString()
209+
{
210+
$this->objectManager
211+
->expects($this->any())
212+
->method('find')
213+
->with(null, '/cms/routes/test-route')
214+
->will($this->returnValue($this))
215+
;
216+
217+
$this->managerRegistry
218+
->expects($this->any())
219+
->method('getManager')
220+
->will($this->returnValue($this->objectManager))
221+
;
222+
223+
$routeProvider = new RouteProvider($this->managerRegistry);
224+
$routeProvider->setManagerName('default');
225+
$routeProvider->setPrefix('');
119226

120227
$routeProvider->getRouteByName('/cms/routes/test-route');
121228
}
@@ -173,6 +280,8 @@ function ($name) use ($objectManagers) {
173280
$routeProvider = new RouteProvider($this->managerRegistry);
174281

175282
$routeProvider->setManagerName('default');
283+
$routeProvider->setPrefix('/cms/routes/');
284+
176285
$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
177286
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
178287
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());

0 commit comments

Comments
 (0)