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

Commit 2888591

Browse files
committed
Ensure that we get the parent locator
Some tests are injecting the plugin manager as the service locator, when, in fact, the two instances should be discretely different. This patch ensures the tests continue to pass by updating `setServiceLocator()` to test if the instance is a plugin manager. If it is, it pulls the parent context and uses that instead.
1 parent 9058a15 commit 2888591

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/Helper/Navigation/AbstractHelper.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Interop\Container\ContainerInterface;
1313
use RecursiveIteratorIterator;
14+
use ReflectionProperty;
1415
use Zend\EventManager\EventManager;
1516
use Zend\EventManager\EventManagerAwareInterface;
1617
use Zend\EventManager\EventManagerInterface;
@@ -19,6 +20,7 @@
1920
use Zend\Navigation;
2021
use Zend\Navigation\Page\AbstractPage;
2122
use Zend\Permissions\Acl;
23+
use Zend\ServiceManager\AbstractPluginManager;
2224
use Zend\View;
2325
use Zend\View\Exception;
2426

@@ -753,6 +755,26 @@ public function hasRole()
753755
*/
754756
public function setServiceLocator(ContainerInterface $serviceLocator)
755757
{
758+
// If we are provided a plugin manager, we should pull the parent
759+
// context from it.
760+
// @todo We should update tests and code to ensure that this situation
761+
// doesn't happen in the future.
762+
if ($serviceLocator instanceof AbstractPluginManager
763+
&& ! method_exists($serviceLocator, 'configure')
764+
&& $serviceLocator->getServiceLocator()
765+
) {
766+
$serviceLocator = $serviceLocator->getServiceLocator();
767+
}
768+
769+
// v3 variant; likely won't be needed.
770+
if ($serviceLocator instanceof AbstractPluginManager
771+
&& method_exists($serviceLocator, 'configure')
772+
) {
773+
$r = new ReflectionProperty($serviceLocator, 'creationContext');
774+
$r->setAccessible(true);
775+
$serviceLocator = $r->getValue($serviceLocator);
776+
}
777+
756778
$this->serviceLocator = $serviceLocator;
757779
return $this;
758780
}

0 commit comments

Comments
 (0)