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

Commit b9f770c

Browse files
committed
Merge pull request #46 from WouterJ/issue_45
Added format option for AiPath action
2 parents ff8f3a8 + 89ed6ce commit b9f770c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

AutoRoute/PathExists/AutoIncrementPath.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class AutoIncrementPath implements PathActionInterface
1616
{
1717
protected $dm;
1818
protected $routeMaker;
19+
protected $format = '%s-%d';
1920

2021
public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker)
2122
{
@@ -25,6 +26,9 @@ public function __construct(DocumentManager $dm, RouteMakerInterface $routeMaker
2526

2627
public function init(array $options)
2728
{
29+
if (isset($options['format'])) {
30+
$this->format = '%s'.$options['format'];
31+
}
2832
}
2933

3034
public function execute(RouteStack $routeStack)
@@ -42,7 +46,7 @@ public function execute(RouteStack $routeStack)
4246
}
4347

4448
do {
45-
$newPath = sprintf('%s-%d', $path, $inc++);
49+
$newPath = sprintf($this->format, $path, $inc++);
4650
} while (null !== $this->dm->find(null, $newPath));
4751

4852
$routeStack->replaceLastPathElement(PathHelper::getNodeName($newPath));

Tests/Unit/AutoRoute/PathExists/AutoIncrementPathTest.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,44 @@ public function testAutoIncrement($testUpdate)
9999
$this->aiPath->execute($this->routeStack);
100100
}
101101

102+
public function testFormatOption()
103+
{
104+
$this->routeStack->expects($this->once())
105+
->method('getFullPath')
106+
->will($this->returnValue('/foo/bar'));
107+
108+
$this->routeStack->expects($this->once())
109+
->method('getContext')
110+
->will($this->returnValue($this->builderContext));
111+
112+
$this->dm->expects($this->at(0))
113+
->method('find')
114+
->with(null, '/foo/bar')
115+
->will($this->returnValue($this->route1));
116+
117+
$this->route1->expects($this->once())
118+
->method('getContent')
119+
->will($this->returnValue($this->content1));
120+
121+
$this->builderContext->expects($this->once())
122+
->method('getContent')
123+
->will($this->returnValue($this->content2));
124+
125+
$this->dm->expects($this->at(1))
126+
->method('find')
127+
->with(null, '/foo/bar1')
128+
->will($this->returnValue(null));
129+
130+
$this->routeStack->expects($this->once())
131+
->method('replaceLastPathElement')
132+
->with('bar1');
133+
134+
$this->routeMaker->expects($this->once())
135+
->method('make')
136+
->with($this->routeStack);
137+
138+
$aiPath = new AutoIncrementPath($this->dm, $this->routeMaker);
139+
$aiPath->init(array('format' => '%d'));
140+
$aiPath->execute($this->routeStack);
141+
}
102142
}

0 commit comments

Comments
 (0)