Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 3988a5b

Browse files
committed
Added fallback for navigation container names in view helpers
- calling the navigation helper with a parameter `navigation` is confusing - name corresponds to the documentation
1 parent d4b8bf0 commit 3988a5b

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/Helper/Navigation/AbstractHelper.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,21 @@ protected function parseContainer(&$container = null)
251251
));
252252
}
253253

254+
// Fallback
255+
if (in_array($container, ['default', 'navigation'], true)) {
256+
// Uses class name
257+
if ($services->has(Navigation\Navigation::class)) {
258+
$container = $services->get(Navigation\Navigation::class);
259+
return;
260+
}
261+
262+
// Uses old service name
263+
if ($services->has('navigation')) {
264+
$container = $services->get('navigation');
265+
return;
266+
}
267+
}
268+
254269
/**
255270
* Load the navigation container from the root service locator
256271
*/

test/Helper/Navigation/AbstractHelperTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace ZendTest\View\Helper\Navigation;
1111

12+
use Zend\Navigation\Navigation;
13+
use Zend\ServiceManager\ServiceManager;
14+
1215
class AbstractHelperTest extends AbstractTest
1316
{
1417
/**
@@ -81,4 +84,29 @@ public function testEventManagerIsNullByDefault()
8184
{
8285
$this->assertNull($this->_helper->getEventManager());
8386
}
87+
88+
public function testFallBackForContainerNames()
89+
{
90+
// Register navigation service with name equal to the documentation
91+
$this->serviceManager->setAllowOverride(true);
92+
$this->serviceManager->setService(
93+
'navigation',
94+
$this->serviceManager->get('Navigation')
95+
);
96+
$this->serviceManager->setAllowOverride(false);
97+
98+
$this->_helper->setServiceLocator($this->serviceManager);
99+
100+
$this->_helper->setContainer('navigation');
101+
$this->assertInstanceOf(
102+
Navigation::class,
103+
$this->_helper->getContainer()
104+
);
105+
106+
$this->_helper->setContainer('default');
107+
$this->assertInstanceOf(
108+
Navigation::class,
109+
$this->_helper->getContainer()
110+
);
111+
}
84112
}

0 commit comments

Comments
 (0)