From 6c595f41fa84d61d8933edef3ed547064365f99c Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sat, 15 Mar 2014 11:39:54 +0100 Subject: [PATCH 1/3] implement getting a route by uuid too --- Doctrine/Phpcr/RouteProvider.php | 20 ++++++-- .../Unit/Doctrine/Phpcr/RouteProviderTest.php | 48 +++++++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Doctrine/Phpcr/RouteProvider.php b/Doctrine/Phpcr/RouteProvider.php index 09cdd9fd..5e4f1fdc 100644 --- a/Doctrine/Phpcr/RouteProvider.php +++ b/Doctrine/Phpcr/RouteProvider.php @@ -19,6 +19,7 @@ use PHPCR\Query\RowInterface; +use PHPCR\Util\UUIDHelper; use Symfony\Component\Routing\Route as SymfonyRoute; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\Exception\RouteNotFoundException; @@ -123,20 +124,29 @@ protected function getCandidates($url) /** * {@inheritDoc} + * + * @param string $name The absolute path or uuid of the Route document. */ public function getRouteByName($name) { - // $name is the route document path - if ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) { + if (UUIDHelper::isUUID($name)) { + $route = $this->getObjectManager()->find($this->className, $name); + if ($route + && '' !== $this->idPrefix + && 0 !== strpos($name, $this->getObjectManager()->getUnitOfWork()->getDocumentId($route), $this->idPrefix) + ) { + $route = null; + } + } elseif ('' === $this->idPrefix || 0 === strpos($name, $this->idPrefix)) { $route = $this->getObjectManager()->find($this->className, $name); } if (empty($route)) { - throw new RouteNotFoundException(sprintf('No route found for path "%s"', $name)); + throw new RouteNotFoundException(sprintf('No route found at "%s"', $name)); } if (!$route instanceof SymfonyRoute) { - throw new RouteNotFoundException(sprintf('Document at path "%s" is no route', $name)); + throw new RouteNotFoundException(sprintf('Document at "%s" is no route', $name)); } return $route; @@ -188,7 +198,7 @@ public function getRoutesByNames($names = null) if ('' !== $this->idPrefix) { foreach ($names as $key => $name) { - if (0 !== strpos($name, $this->idPrefix)) { + if (!UUIDHelper::isUUID($name) && 0 !== strpos($name, $this->idPrefix)) { unset($names[$key]); } } diff --git a/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php b/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php index f035306e..cebda666 100644 --- a/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php +++ b/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php @@ -12,6 +12,7 @@ namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\Doctrine\Phpcr; +use PHPCR\Util\UUIDHelper; use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\RouteProvider; class RouteProviderTest extends \PHPUnit_Framework_Testcase @@ -73,6 +74,53 @@ public function testGetRouteByName() $this->assertEquals('/cms/routes/test-route', $foundRoute->getPath()); } + public function testGetRouteByNameUuid() + { + $uuid = UUIDHelper::generateUUID(); + $this->route + ->expects($this->any()) + ->method('getPath') + ->will($this->returnValue('/cms/routes/test-route')) + ; + $objectManager = $this + ->getMockBuilder('Doctrine\ODM\PHPCR\DocumentManager') + ->disableOriginalConstructor() + ->getMock() + ; + $uow = $this->getMock('Doctrine\ODM\PHPCR\UnitOfWork'); + $objectManager + ->expects($this->any()) + ->method('find') + ->with(null, $uuid) + ->will($this->returnValue($this->route)) + ; + $objectManager + ->expects($this->any()) + ->method('getUnitOfWork') + ->will($this->returnValue($uow)) + ; + $uow + ->expects($this->any()) + ->method('getDocumentId') + ->will($this->returnValue('/cms/routes/test-route')) + ; + + $this->managerRegistry + ->expects($this->any()) + ->method('getManager') + ->will($this->returnValue($objectManager)) + ; + + $routeProvider = new RouteProvider($this->managerRegistry); + $routeProvider->setManagerName('default'); + + $routeProvider->setPrefix('/cms/routes/'); + $foundRoute = $routeProvider->getRouteByName($uuid); + + $this->assertInstanceOf('Symfony\Component\Routing\Route', $foundRoute); + $this->assertEquals('/cms/routes/test-route', $foundRoute->getPath()); + } + /** * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException */ From ebe0a47099fa9306341b99c15188ba2a78c23c17 Mon Sep 17 00:00:00 2001 From: Lukas Kahwe Smith Date: Sun, 23 Mar 2014 15:46:25 +0100 Subject: [PATCH 2/3] fix tests --- Doctrine/Phpcr/RouteProvider.php | 2 +- Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Doctrine/Phpcr/RouteProvider.php b/Doctrine/Phpcr/RouteProvider.php index 5e4f1fdc..f06b0403 100644 --- a/Doctrine/Phpcr/RouteProvider.php +++ b/Doctrine/Phpcr/RouteProvider.php @@ -133,7 +133,7 @@ public function getRouteByName($name) $route = $this->getObjectManager()->find($this->className, $name); if ($route && '' !== $this->idPrefix - && 0 !== strpos($name, $this->getObjectManager()->getUnitOfWork()->getDocumentId($route), $this->idPrefix) + && 0 !== strpos($this->getObjectManager()->getUnitOfWork()->getDocumentId($route), $this->idPrefix) ) { $route = null; } diff --git a/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php b/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php index cebda666..2247fb19 100644 --- a/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php +++ b/Tests/Unit/Doctrine/Phpcr/RouteProviderTest.php @@ -87,7 +87,11 @@ public function testGetRouteByNameUuid() ->disableOriginalConstructor() ->getMock() ; - $uow = $this->getMock('Doctrine\ODM\PHPCR\UnitOfWork'); + $uow = $this + ->getMockBuilder('Doctrine\ODM\PHPCR\UnitOfWork') + ->disableOriginalConstructor() + ->getMock() + ; $objectManager ->expects($this->any()) ->method('find') From c2d6bee888ae137e99955031c62e34e5e9762844 Mon Sep 17 00:00:00 2001 From: David Buchmann Date: Sun, 23 Mar 2014 22:24:10 +0100 Subject: [PATCH 3/3] adding changelog entry --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 56850554..7a14ab9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,9 @@ Changelog ========= +* **2014-03-23**: When using PHPCR-ODM, routes can now be generated with their + uuid as route name as well, in addition to the repository path. + * **2013-11-28**: [BC BREAK] the alias attribute of the is renamed to class in the bundle configuration.