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

Commit 9058a15

Browse files
committed
Ensure that lazy-instantiated plugin manager gets parent service locator
Adds code to the `getPluginManager()` implementation to ensure that if a service locator is composed, it is used to inject a lazy-instantiated navigation plugin manager. This should fix the last lingering issues as reported in #49.
1 parent 5291697 commit 9058a15

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ before_install:
5353
- if [[ $ZEND_EVENTMANAGER_VERSION == '' ]]; then composer require --no-update "zendframework/zend-eventmanager:^3.0" ; fi
5454
- if [[ $ZEND_SERVICEMANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$ZEND_SERVICEMANAGER_VERSION" ; fi
5555
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
56-
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-mvc zendframework/zend-session ; fi
56+
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-modulemanager zendframework/zend-mvc zendframework/zend-session ; fi
5757

5858
install:
5959
- travis_retry composer install --no-interaction --ignore-platform-reqs

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"zendframework/zend-i18n": "^2.6",
3131
"zendframework/zend-json": "^2.6.1",
3232
"zendframework/zend-log": "^2.7",
33+
"zendframework/zend-modulemanager": "^2.5",
3334
"zendframework/zend-mvc": "^2.6.1",
3435
"zendframework/zend-navigation": "^2.5",
3536
"zendframework/zend-paginator": "^2.5",

src/Helper/Navigation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ public function setPluginManager(Navigation\PluginManager $plugins)
322322
public function getPluginManager()
323323
{
324324
if (null === $this->plugins) {
325-
$this->setPluginManager(new Navigation\PluginManager());
325+
$this->setPluginManager(new Navigation\PluginManager($this->getServiceLocator()));
326326
}
327327

328328
return $this->plugins;

test/Helper/Navigation/AbstractTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ abstract class AbstractTest extends \PHPUnit_Framework_TestCase
8686
*/
8787
protected function setUp()
8888
{
89-
if (! class_exists(PluginFlashMessenger::class)) {
89+
if (! class_exists(ServiceManagerConfig::class)) {
9090
$this->markTestSkipped(
9191
'Skipping zend-mvc-related tests until that component is updated '
9292
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
@@ -139,7 +139,10 @@ protected function setUp()
139139
],
140140
];
141141

142-
$sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfig);
142+
$sm = $this->serviceManager = new ServiceManager();
143+
$sm->setAllowOverride(true);
144+
145+
(new ServiceManagerConfig())->configureServiceManager($sm);
143146
$sm->setService('ApplicationConfig', $smConfig);
144147
$sm->get('ModuleManager')->loadModules();
145148
$sm->get('Application')->bootstrap();
@@ -148,6 +151,8 @@ protected function setUp()
148151
$sm->setService('nav1', $this->_nav1);
149152
$sm->setService('nav2', $this->_nav2);
150153

154+
$sm->setAllowOverride(false);
155+
151156
$app = $this->serviceManager->get('Application');
152157
$app->getMvcEvent()->setRouteMatch(new RouteMatch([
153158
'controller' => 'post',

test/Helper/Navigation/NavigationTest.php

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

1010
namespace ZendTest\View\Helper\Navigation;
1111

12+
use Interop\Container\ContainerInterface;
1213
use Zend\Navigation\Navigation as Container;
14+
use Zend\Navigation\Page;
1315
use Zend\Permissions\Acl;
1416
use Zend\Permissions\Acl\Role;
1517
use Zend\ServiceManager\ServiceManager;
@@ -61,11 +63,10 @@ public function testAcceptAclShouldReturnGracefullyWithUnknownResource()
6163
$this->_helper->setRole($acl['role']);
6264

6365
$accepted = $this->_helper->accept(
64-
new \Zend\Navigation\Page\Uri([
66+
new Page\Uri([
6567
'resource' => 'unknownresource',
6668
'privilege' => 'someprivilege'
67-
],
68-
false)
69+
], false)
6970
);
7071

7172
$this->assertEquals($accepted, false);
@@ -144,10 +145,9 @@ public function testServiceManagerIsUsedToRetrieveContainer()
144145
$serviceManager = new ServiceManager;
145146
$serviceManager->setService('navigation', $container);
146147

147-
$pluginManager = new View\HelperPluginManager;
148-
$pluginManager->setServiceLocator($serviceManager);
148+
$pluginManager = new View\HelperPluginManager($serviceManager);
149149

150-
$this->_helper->setServiceLocator($pluginManager);
150+
$this->_helper->setServiceLocator($serviceManager);
151151
$this->_helper->setContainer('navigation');
152152

153153
$expected = $this->_helper->getContainer();
@@ -558,7 +558,7 @@ public function testMultipleNavigationsWithSameHelperAndSameContainer()
558558

559559
public function testSetPluginManagerAndView()
560560
{
561-
$pluginManager = new \Zend\View\Helper\Navigation\PluginManager();
561+
$pluginManager = new Navigation\PluginManager();
562562
$view = new PhpRenderer();
563563

564564
$helper = new $this->_helperName;
@@ -568,6 +568,27 @@ public function testSetPluginManagerAndView()
568568
$this->assertEquals($view, $pluginManager->getRenderer());
569569
}
570570

571+
/**
572+
* @group 49
573+
*/
574+
public function testInjectsLazyInstantiatedPluginManagerWithCurrentServiceLocator()
575+
{
576+
$services = $this->prophesize(ContainerInterface::class)->reveal();
577+
$helper = new $this->_helperName;
578+
$helper->setServiceLocator($services);
579+
580+
$plugins = $helper->getPluginManager();
581+
$this->assertInstanceOf(Navigation\PluginManager::class, $plugins);
582+
583+
if (method_exists($plugins, 'configure')) {
584+
// v3
585+
$this->assertAttributeSame($services, 'creationContext', $plugins);
586+
} else {
587+
// v2
588+
$this->assertSame($services, $plugins->getServiceLocator());
589+
}
590+
}
591+
571592
/**
572593
* Returns the contens of the expected $file, normalizes newlines
573594
* @param string $file

0 commit comments

Comments
 (0)