From 50526deb3ea4bc76694b4004dab21216a58e8bf3 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Tue, 17 Dec 2013 17:57:36 +0100 Subject: [PATCH 1/2] Added test for format option --- .../PathExists/AutoIncrementPathTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php b/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php index ddeb678..4ae0816 100644 --- a/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php +++ b/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php @@ -99,4 +99,44 @@ public function testAutoIncrement($testUpdate) $this->aiPath->execute($this->routeStack); } + public function testFormatOption() + { + $this->routeStack->expects($this->once()) + ->method('getFullPath') + ->will($this->returnValue('/foo/bar')); + + $this->routeStack->expects($this->once()) + ->method('getContext') + ->will($this->returnValue($this->builderContext)); + + $this->dm->expects($this->at(0)) + ->method('find') + ->with(null, '/foo/bar') + ->will($this->returnValue($this->route1)); + + $this->route1->expects($this->once()) + ->method('getContent') + ->will($this->returnValue($this->content1)); + + $this->builderContext->expects($this->once()) + ->method('getContent') + ->will($this->returnValue($this->content2)); + + $this->dm->expects($this->at(1)) + ->method('find') + ->with(null, '/foo/bar/1') + ->will($this->returnValue(null)); + + $this->routeStack->expects($this->once()) + ->method('replaceLastPathElement') + ->with('bar/2'); + + $this->routeMaker->expects($this->once()) + ->method('make') + ->with($this->routeStack); + + $aiPath = new AutoIncrementPath($this->dm, $this->routeMaker); + $aiPath->init(array('format' => '/%d')); + $aiPath->execute($this->routeStack); + } } From 89ed6ceaed9659f114a21c454167364eb3be8511 Mon Sep 17 00:00:00 2001 From: Wouter J Date: Tue, 17 Dec 2013 18:00:39 +0100 Subject: [PATCH 2/2] Implemented format option --- AutoRoute/PathExists/AutoIncrementPath.php | 6 +++++- Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/AutoRoute/PathExists/AutoIncrementPath.php b/AutoRoute/PathExists/AutoIncrementPath.php index 6deabec..1250e0b 100644 --- a/AutoRoute/PathExists/AutoIncrementPath.php +++ b/AutoRoute/PathExists/AutoIncrementPath.php @@ -16,6 +16,7 @@ class AutoIncrementPath implements PathActionInterface { protected $dm; protected $routeMaker; + protected $format = '%s-%d'; public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker) { @@ -25,6 +26,9 @@ public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker public function init(array $options) { + if (isset($options['format'])) { + $this->format = '%s'.$options['format']; + } } public function execute(RouteStack $routeStack) @@ -42,7 +46,7 @@ public function execute(RouteStack $routeStack) } do { - $newPath = sprintf('%s-%d', $path, $inc++); + $newPath = sprintf($this->format, $path, $inc++); } while (null !== $this->dm->find(null, $newPath)); $routeStack->replaceLastPathElement(PathHelper::getNodeName($newPath)); diff --git a/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php b/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php index 4ae0816..d6f7b3b 100644 --- a/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php +++ b/Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php @@ -124,19 +124,19 @@ public function testFormatOption() $this->dm->expects($this->at(1)) ->method('find') - ->with(null, '/foo/bar/1') + ->with(null, '/foo/bar1') ->will($this->returnValue(null)); $this->routeStack->expects($this->once()) ->method('replaceLastPathElement') - ->with('bar/2'); + ->with('bar1'); $this->routeMaker->expects($this->once()) ->method('make') ->with($this->routeStack); $aiPath = new AutoIncrementPath($this->dm, $this->routeMaker); - $aiPath->init(array('format' => '/%d')); + $aiPath->init(array('format' => '%d')); $aiPath->execute($this->routeStack); } }