Skip to content

Created Unit tests for getRoutesByName with defined idPrefix. #189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from Oct 8, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Doctrine/Phpcr/RouteProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ protected function getCandidates($url)
*/
public function getRouteByName($name, $parameters = array())
{

// $name is the route document path
if ($this->idPrefix && 0 === strpos($name, $this->idPrefix)) {
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
$route = $this->getObjectManager()->find($this->className, $name);
}

Expand All @@ -140,10 +141,10 @@ public function getRouteByName($name, $parameters = array())
*/
public function getRoutesByNames($names, $parameters = array())
{
if ($this->idPrefix) {
$routes = array();
$routes = array();
if ('' === $this->idPrefix) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was correct as lukas did it. what you do is only go into the loop if there is no prefix, effectively never filtering

foreach ($names as $name) {
if (0 === strpos($name, $this->idPrefix)) {
if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, but you still need to undo the changes here, the existing code was correct.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove the idPrefix check here .. since its redundant

$routes[] = $name;
}
}
Expand Down
111 changes: 110 additions & 1 deletion Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function testGetRouteByName()
$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');

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

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

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');

$routeProvider->setPrefix('/cms/routes/');
$routeProvider->getRouteByName('/cms/routes/test-route');
}

Expand All @@ -116,6 +117,112 @@ public function testGetRouteByNameNoRoute()

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');
$routeProvider->setPrefix('/cms/routes/');
$routeProvider->getRouteByName('/cms/routes/test-route');
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 spaces

*/
public function testGetRouteByNameInvalidRoute()
{
$this->objectManager
->expects($this->never())
->method('find')
;

$this->managerRegistry
->expects($this->any())
->method('getManager')
->will($this->returnValue($this->objectManager))
;

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');

$routeProvider->setPrefix('/cms/routes');

$routeProvider->getRouteByName('invalid_route');
}

public function testGetRouteByNameIdPrefixEmptyString()
{

$this->route
->expects($this->any())
->method('getPath')
->will($this->returnValue('/cms/routes/test-route'))
;

$this->objectManager
->expects($this->any())
->method('find')
->with(null, '/cms/routes/test-route')
->will($this->returnValue($this->route))
;

$this->managerRegistry
->expects($this->any())
->method('getManager')
->will($this->returnValue($this->objectManager))
;

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');

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

$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line to much

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGetRouteByNameNotFoundIdPrefixEmptyString()
{
$this->objectManager
->expects($this->any())
->method('find')
->with(null, '/cms/routes/test-route')
->will($this->returnValue(null))
;

$this->managerRegistry
->expects($this->any())
->method('getManager')
->will($this->returnValue($this->objectManager))
;

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');
$routeProvider->setPrefix('');
$routeProvider->getRouteByName('/cms/routes/test-route');
}

/**
* @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException
*/
public function testGetRouteByNameNoRoutePrefixEmptyString()
{
$this->objectManager
->expects($this->any())
->method('find')
->with(null, '/cms/routes/test-route')
->will($this->returnValue($this))
;

$this->managerRegistry
->expects($this->any())
->method('getManager')
->will($this->returnValue($this->objectManager))
;

$routeProvider = new RouteProvider($this->managerRegistry);
$routeProvider->setManagerName('default');
$routeProvider->setPrefix('');

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

$routeProvider->setManagerName('default');
$routeProvider->setPrefix('/cms/routes/');

$foundRoute = $routeProvider->getRouteByName('/cms/routes/test-route');
$this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute);
$this->assertEquals('/cms/routes/test-route', $foundRoute->getPath());
Expand Down