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

Fix issue with plugin manager detection in ServiceLocatorAware initializer #114

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Service/ServiceManagerConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,11 @@ public function __construct(array $config = [])
},
'ServiceManagerAwareInitializer' => function ($first, $second) {
if ($first instanceof ContainerInterface) {
// zend-servicemanager v3
$container = $first;
$instance = $second;
} else {
// zend-servicemanager v2
$container = $second;
$instance = $first;
}
Expand All @@ -129,10 +131,16 @@ public function __construct(array $config = [])
}
},
'ServiceLocatorAwareInitializer' => function ($first, $second) {
if ($first instanceof ContainerInterface) {
if ($first instanceof AbstractPluginManager) {
// Edge case under zend-servicemanager v2
$container = $second;
$instance = $first;
} elseif ($first instanceof ContainerInterface) {
// zend-servicemanager v3
$container = $first;
$instance = $second;
} else {
// zend-servicemanager v2
$container = $second;
$instance = $first;
}
Expand Down
73 changes: 73 additions & 0 deletions test/Service/ServiceManagerConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use ReflectionClass;
use stdClass;
use Zend\EventManager\EventManager;
use Zend\Mvc\Controller\PluginManager;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\Factory\InvokableFactory;
use Zend\ServiceManager\ServiceManager;
Expand Down Expand Up @@ -42,6 +43,16 @@ protected function setUp()
$this->config->configureServiceManager($this->services);
}

/**
* Is this a v2 service manager?
*
* @return bool
*/
public function isV2ServiceManager()
{
return (! method_exists($this->services, 'configure'));
}

/**
* Create an event manager instance based on zend-eventmanager version
*
Expand Down Expand Up @@ -225,4 +236,66 @@ public function testCreatesAFactoryForTheServiceManagerThatReturnsIt()
$this->assertTrue($serviceManager->has('ServiceManager'), 'Missing ServiceManager service!');
$this->assertSame($serviceManager, $serviceManager->get('ServiceManager'));
}

/**
* @see https://github.com/zendframework/zend-servicemanager/issues/109
*/
public function testServiceLocatorAwareInitializerCanInjectPluginManagers()
{
if (! $this->isV2ServiceManager()) {
$this->markTestSkipped(sprintf(
'%s verifies backwards compatibility with the v2 series of zend-servicemanager',
__FUNCTION__
));
}

$this->services->setFactory('test-plugins', function () {
return new PluginManager();
});

$deprecated = false;
set_error_handler(function ($errno, $errstr) use (&$deprecated) {
$deprecated = true;
}, E_USER_DEPRECATED);

$plugins = $this->services->get('test-plugins');

restore_error_handler();

$this->assertSame($this->services, $plugins->getServiceLocator());
$this->assertTrue($deprecated, 'Deprecation notice for ServiceLocatorAwareInitializer was not triggered');
}

/**
* @see https://github.com/zendframework/zend-servicemanager/issues/109
*/
public function testServiceLocatorAwareInitializerWillNotReinjectPluginManagers()
{
if (! $this->isV2ServiceManager()) {
$this->markTestSkipped(sprintf(
'%s verifies backwards compatibility with the v2 series of zend-servicemanager',
__FUNCTION__
));
}

$altServices = new ServiceManager();
$this->services->setFactory('test-plugins', function () use ($altServices) {
return new PluginManager($altServices);
});

$deprecated = false;
set_error_handler(function ($errno, $errstr) use (&$deprecated) {
$deprecated = true;
}, E_USER_DEPRECATED);

$plugins = $this->services->get('test-plugins');

restore_error_handler();

$this->assertSame($altServices, $plugins->getServiceLocator());
$this->assertFalse(
$deprecated,
'Deprecation notice for ServiceLocatorAwareInitializer was triggered, but should not have been'
);
}
}