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

Commit

Permalink
Ensure that lazy-instantiated plugin manager gets parent service locator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
weierophinney committed Feb 22, 2016
1 parent 5291697 commit 9058a15
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ before_install:
- if [[ $ZEND_EVENTMANAGER_VERSION == '' ]]; then composer require --no-update "zendframework/zend-eventmanager:^3.0" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION != '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:$ZEND_SERVICEMANAGER_VERSION" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer require --dev --no-update "zendframework/zend-servicemanager:^3.0.3" ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-mvc zendframework/zend-session ; fi
- if [[ $ZEND_SERVICEMANAGER_VERSION == '' ]]; then composer remove --dev --no-update zendframework/zend-modulemanager zendframework/zend-mvc zendframework/zend-session ; fi

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"zendframework/zend-i18n": "^2.6",
"zendframework/zend-json": "^2.6.1",
"zendframework/zend-log": "^2.7",
"zendframework/zend-modulemanager": "^2.5",
"zendframework/zend-mvc": "^2.6.1",
"zendframework/zend-navigation": "^2.5",
"zendframework/zend-paginator": "^2.5",
Expand Down
2 changes: 1 addition & 1 deletion src/Helper/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public function setPluginManager(Navigation\PluginManager $plugins)
public function getPluginManager()
{
if (null === $this->plugins) {
$this->setPluginManager(new Navigation\PluginManager());
$this->setPluginManager(new Navigation\PluginManager($this->getServiceLocator()));
}

return $this->plugins;
Expand Down
9 changes: 7 additions & 2 deletions test/Helper/Navigation/AbstractTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ abstract class AbstractTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
if (! class_exists(PluginFlashMessenger::class)) {
if (! class_exists(ServiceManagerConfig::class)) {
$this->markTestSkipped(
'Skipping zend-mvc-related tests until that component is updated '
. 'to be forwards-compatible with zend-eventmanager, zend-stdlib, '
Expand Down Expand Up @@ -139,7 +139,10 @@ protected function setUp()
],
];

$sm = $this->serviceManager = new ServiceManager(new ServiceManagerConfig);
$sm = $this->serviceManager = new ServiceManager();
$sm->setAllowOverride(true);

(new ServiceManagerConfig())->configureServiceManager($sm);
$sm->setService('ApplicationConfig', $smConfig);
$sm->get('ModuleManager')->loadModules();
$sm->get('Application')->bootstrap();
Expand All @@ -148,6 +151,8 @@ protected function setUp()
$sm->setService('nav1', $this->_nav1);
$sm->setService('nav2', $this->_nav2);

$sm->setAllowOverride(false);

$app = $this->serviceManager->get('Application');
$app->getMvcEvent()->setRouteMatch(new RouteMatch([
'controller' => 'post',
Expand Down
35 changes: 28 additions & 7 deletions test/Helper/Navigation/NavigationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

namespace ZendTest\View\Helper\Navigation;

use Interop\Container\ContainerInterface;
use Zend\Navigation\Navigation as Container;
use Zend\Navigation\Page;
use Zend\Permissions\Acl;
use Zend\Permissions\Acl\Role;
use Zend\ServiceManager\ServiceManager;
Expand Down Expand Up @@ -61,11 +63,10 @@ public function testAcceptAclShouldReturnGracefullyWithUnknownResource()
$this->_helper->setRole($acl['role']);

$accepted = $this->_helper->accept(
new \Zend\Navigation\Page\Uri([
new Page\Uri([
'resource' => 'unknownresource',
'privilege' => 'someprivilege'
],
false)
], false)
);

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

$pluginManager = new View\HelperPluginManager;
$pluginManager->setServiceLocator($serviceManager);
$pluginManager = new View\HelperPluginManager($serviceManager);

$this->_helper->setServiceLocator($pluginManager);
$this->_helper->setServiceLocator($serviceManager);
$this->_helper->setContainer('navigation');

$expected = $this->_helper->getContainer();
Expand Down Expand Up @@ -558,7 +558,7 @@ public function testMultipleNavigationsWithSameHelperAndSameContainer()

public function testSetPluginManagerAndView()
{
$pluginManager = new \Zend\View\Helper\Navigation\PluginManager();
$pluginManager = new Navigation\PluginManager();
$view = new PhpRenderer();

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

/**
* @group 49
*/
public function testInjectsLazyInstantiatedPluginManagerWithCurrentServiceLocator()
{
$services = $this->prophesize(ContainerInterface::class)->reveal();
$helper = new $this->_helperName;
$helper->setServiceLocator($services);

$plugins = $helper->getPluginManager();
$this->assertInstanceOf(Navigation\PluginManager::class, $plugins);

if (method_exists($plugins, 'configure')) {
// v3
$this->assertAttributeSame($services, 'creationContext', $plugins);
} else {
// v2
$this->assertSame($services, $plugins->getServiceLocator());
}
}

/**
* Returns the contens of the expected $file, normalizes newlines
* @param string $file
Expand Down

0 comments on commit 9058a15

Please sign in to comment.