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 #47 from sroze/empty-pathprovider
Browse files Browse the repository at this point in the history
Allow path providers that don't add path elements each time
  • Loading branch information
dantleech committed Dec 21, 2013
2 parents f843523 + 4f38cd7 commit ff8f3a8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
5 changes: 4 additions & 1 deletion AutoRoute/BuilderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ public function getFullPath()
{
$paths = array();
foreach ($this->routeStacks as $routeStack) {
$paths[] = $routeStack->getPath();
$path = $routeStack->getPath();
if (!empty($path)) {
$paths[] = $path;
}
}

$path = implode('/', $paths);
Expand Down
12 changes: 10 additions & 2 deletions AutoRoute/RouteStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class RouteStack
{
protected $pathElements;
protected $pathElements = array();
protected $routes = array();
protected $context;
protected $existingRoute;
Expand Down Expand Up @@ -90,6 +90,10 @@ public function getPathElements()
*/
public function getPaths()
{
if (empty($this->pathElements)) {
return array();
}

$tmp = array();

foreach ($this->pathElements as $pathElement) {
Expand Down Expand Up @@ -135,7 +139,11 @@ public function getFullPath()
$fullPath = $this->getPath();

if ($parentPath) {
$fullPath = $parentPath.'/'.$fullPath;
if (empty($fullPath)) {
$fullPath = $parentPath;
} else {
$fullPath = $parentPath.'/'.$fullPath;
}
}

return $fullPath;
Expand Down
27 changes: 27 additions & 0 deletions Tests/Unit/AutoRoute/BuilderContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,33 @@ public function testStageAndCommitRouteStack()
$this->assertCount(1, $this->builderContext->getRouteStacks());
}

public function testIgnoreEmptyPath()
{
$this->routeStack->expects($this->at(3))
->method('getPath')
->will($this->returnValue('route'));

$this->routeStack->expects($this->at(4))
->method('getPath')
->will($this->returnValue(''));

$this->routeStack->expects($this->at(5))
->method('getPath')
->will($this->returnValue('foo/bar'));

$this->routeStack->expects($this->exactly(3))
->method('isClosed')
->will($this->returnValue(true));

for ($i = 0; $i < 3; $i++) {
$this->builderContext->stageRouteStack($this->routeStack);
$this->builderContext->commitRouteStack();
}

$this->assertCount(3, $this->builderContext->getRouteStacks());
$this->assertEquals('route/foo/bar', $this->builderContext->getFullPath());
}

/**
* @expectedException \RuntimeException
*/
Expand Down
6 changes: 6 additions & 0 deletions Tests/Unit/AutoRoute/RouteStackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public function setUp()
$this->route2 = new \stdClass;
}

public function testGetEmptyPath()
{
$this->assertEmpty($this->routeStack->getPaths());
$this->assertEquals('', $this->routeStack->getFullPath());
}

public function testAddPathElement()
{
$this->routeStack->addPathElements(array('foo', 'bar'));
Expand Down

0 comments on commit ff8f3a8

Please sign in to comment.