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

Commit 8f74f93

Browse files
committed
Ensure plugin managers validate service instances
@ezimuel noted when working on zend-validator updates that `setService()` was no longer raising an exception when the plugin instance was not valid for the plugin manager. This patch fixes the regression.
1 parent 9e11bbc commit 8f74f93

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/AbstractPluginManager.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ public function __construct($configInstanceOrParentLocator = null, array $config
9292
: $this;
9393
}
9494

95+
/**
96+
* Override configure() to validate service instances.
97+
*
98+
* If an instance passed in the `services` configuration is invalid for the
99+
* plugin manager, this method will raise an InvalidServiceException.
100+
*
101+
* {@inheritDoc}
102+
* @throws InvalidServiceException
103+
*/
104+
public function configure(array $config)
105+
{
106+
if (isset($config['services'])) {
107+
foreach ($config['services'] as $service) {
108+
$this->validate($service);
109+
}
110+
}
111+
112+
parent::configure($config);
113+
114+
return $this;
115+
}
116+
95117
/**
96118
* {@inheritDoc}
97119
*

test/AbstractPluginManagerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,20 @@ public function testValidateWillFallBackToValidatePluginWhenDefinedAndEmitDeprec
333333
$this->assertTrue($assertionCalled, 'Assertion was not called by validatePlugin!');
334334
$this->assertTrue($errorHandlerCalled, 'Error handler was not triggered by validatePlugin!');
335335
}
336+
337+
public function testSetServiceShouldRaiseExceptionForInvalidPlugin()
338+
{
339+
$pluginManager = new TestAsset\SimplePluginManager(new ServiceManager());
340+
$this->setExpectedException(InvalidServiceException::class);
341+
$pluginManager->setService(stdClass::class, new stdClass());
342+
}
343+
344+
public function testPassingServiceInstanceViaConfigureShouldRaiseExceptionForInvalidPlugin()
345+
{
346+
$pluginManager = new TestAsset\SimplePluginManager(new ServiceManager());
347+
$this->setExpectedException(InvalidServiceException::class);
348+
$pluginManager->configure(['services' => [
349+
stdClass::class => new stdClass(),
350+
]]);
351+
}
336352
}

0 commit comments

Comments
 (0)