Skip to content
This repository has been archived by the owner on Sep 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #46 from WouterJ/issue_45
Browse files Browse the repository at this point in the history
Added format option for AiPath action
  • Loading branch information
dantleech committed Dec 21, 2013
2 parents ff8f3a8 + 89ed6ce commit b9f770c
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
6 changes: 5 additions & 1 deletion AutoRoute/PathExists/AutoIncrementPath.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AutoIncrementPath implements PathActionInterface
{
protected $dm;
protected $routeMaker;
protected $format = '%s-%d';

public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker)
{
Expand All @@ -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)
Expand All @@ -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));
Expand Down
40 changes: 40 additions & 0 deletions Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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/bar1')
->will($this->returnValue(null));

$this->routeStack->expects($this->once())
->method('replaceLastPathElement')
->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->execute($this->routeStack);
}
}

0 comments on commit b9f770c

Please sign in to comment.