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

Commit 1db9ddb

Browse files
committed
Ensure navigation plugin manager initializer works properly
- Adds a test to ensure that the parent container is injected into navigation helpers by the initializer created in the navigation PluginManager constructor, and fixes the code to work correctly.
1 parent df50e15 commit 1db9ddb

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/Helper/Navigation/PluginManager.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,31 @@ class PluginManager extends HelperPluginManager
6565
*/
6666
public function __construct($configOrContainerInstance = null, array $v3config = [])
6767
{
68-
$this->initializers[] = function ($container, $instance) {
68+
$this->initializers[] = function ($first, $second) {
69+
// v2 vs v3 argument order
70+
if ($first instanceof ContainerInterface) {
71+
// v3
72+
$container = $first;
73+
$instance = $second;
74+
} else {
75+
// v2
76+
$container = $second;
77+
$instance = $first;
78+
}
79+
6980
if (! $instance instanceof AbstractHelper) {
7081
return;
7182
}
7283

84+
// This initializer was written with v2 functionality in mind; as such,
85+
// we need to test and see if we're called in a v2 context, and, if so,
86+
// set the service locator to the parent locator.
87+
//
88+
// Under v3, the parent locator is what is passed to the method already.
89+
if (method_exists($container, 'getServiceLocator') && $container->getServiceLocator()) {
90+
$container = $container->getServiceLocator();
91+
}
92+
7393
$instance->setServiceLocator($container);
7494
};
7595

test/Helper/Navigation/PluginManagerCompatibilityTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Zend\ServiceManager\Test\CommonPluginManagerTrait;
1515
use Zend\View\Exception\InvalidHelperException;
1616
use Zend\View\Helper\Navigation\AbstractHelper;
17+
use Zend\View\Helper\Navigation\Breadcrumbs;
1718
use Zend\View\Helper\Navigation\PluginManager;
1819

1920
/**
@@ -65,4 +66,26 @@ public function testConstructorAllowsConfigInstanceAsFirstArgumentUnderV2()
6566
$helpers = new PluginManager(new Config([]));
6667
$this->assertInstanceOf(PluginManager::class, $helpers);
6768
}
69+
70+
public function testInjectsParentContainerIntoHelpersUnderV2()
71+
{
72+
$helpers = $this->getPluginManager();
73+
if (method_exists($helpers, 'configure')) {
74+
$this->markTestSkipped('This test is specific to v2 functionality');
75+
}
76+
77+
$config = new Config([
78+
'navigation' => [
79+
'default' => [],
80+
],
81+
]);
82+
83+
$services = new ServiceManager();
84+
$config->configureServiceManager($services);
85+
$helpers = new PluginManager($services);
86+
87+
$helper = $helpers->get('breadcrumbs');
88+
$this->assertInstanceOf(Breadcrumbs::class, $helper);
89+
$this->assertSame($services, $helper->getServiceLocator());
90+
}
6891
}

0 commit comments

Comments
 (0)