From d38a600dd989a7f96407388bb476f503beac967c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20B=C3=B6sing?= <2189546+boesing@users.noreply.github.com> Date: Tue, 2 May 2023 19:28:54 +0200 Subject: [PATCH] qa: adapt changes to `CommonPluginManagerTrait` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com> --- src/Test/CommonPluginManagerTrait.php | 50 ++++++++----------- test/ExamplePluginManagerTest.php | 28 +++++++---- .../InvokableObjectPluginManager.php | 23 ++------- 3 files changed, 43 insertions(+), 58 deletions(-) diff --git a/src/Test/CommonPluginManagerTrait.php b/src/Test/CommonPluginManagerTrait.php index b93677f6..6c83183b 100644 --- a/src/Test/CommonPluginManagerTrait.php +++ b/src/Test/CommonPluginManagerTrait.php @@ -5,34 +5,36 @@ namespace Laminas\ServiceManager\Test; use Laminas\ServiceManager\AbstractPluginManager; +use Laminas\ServiceManager\AbstractSingleInstancePluginManager; use Laminas\ServiceManager\Exception\InvalidServiceException; +use Laminas\ServiceManager\ServiceManager; use ReflectionProperty; use function method_exists; /** - * Trait for testing plugin managers for v2-v3 compatibility + * Trait for testing plugin managers for compatibility * * To use this trait: * * implement the `getPluginManager()` method to return your plugin manager - * * implement the `getV2InvalidPluginException()` method to return the class `validatePlugin()` throws under v2 + * @psalm-import-type ServiceManagerConfiguration from ServiceManager */ trait CommonPluginManagerTrait { - public function testInstanceOfMatches() + public function testInstanceOfMatches(): void { $manager = $this->getPluginManager(); $reflection = new ReflectionProperty($manager, 'instanceOf'); $this->assertEquals($this->getInstanceOf(), $reflection->getValue($manager), 'instanceOf does not match'); } - public function testRegisteringInvalidElementRaisesException() + public function testRegisteringInvalidElementRaisesException(): void { $this->expectException($this->getServiceNotFoundException()); $this->getPluginManager()->setService('test', $this); } - public function testLoadingInvalidElementRaisesException() + public function testLoadingInvalidElementRaisesException(): void { $manager = $this->getPluginManager(); $manager->setInvokableClass('test', static::class); @@ -42,55 +44,45 @@ public function testLoadingInvalidElementRaisesException() /** * @dataProvider aliasProvider - * @param string $alias - * @param string $expected */ - public function testPluginAliasesResolve($alias, $expected) + public function testPluginAliasesResolve(string $alias, string $expected): void { $this->assertInstanceOf($expected, $this->getPluginManager()->get($alias), "Alias '$alias' does not resolve'"); } /** - * @return array + * @return list */ - public function aliasProvider() + public function aliasProvider(): array { $manager = $this->getPluginManager(); - $reflection = new ReflectionProperty($manager, 'aliases'); + $pluginContainer = (new ReflectionProperty(AbstractPluginManager::class, 'plugins')) + ->getValue($manager); + + $reflection = new ReflectionProperty($pluginContainer, 'aliases'); $data = []; - foreach ($reflection->getValue($manager) as $alias => $expected) { + foreach ($reflection->getValue($pluginContainer) as $alias => $expected) { $data[] = [$alias, $expected]; } + return $data; } protected function getServiceNotFoundException(): string { - $manager = $this->getPluginManager(); - if (method_exists($manager, 'configure')) { - return InvalidServiceException::class; - } - return $this->getV2InvalidPluginException(); + return InvalidServiceException::class; } /** * Returns the plugin manager to test - * - * @return AbstractPluginManager - */ - abstract protected function getPluginManager(); - - /** - * Returns the FQCN of the exception thrown under v2 by `validatePlugin()` - * - * @return mixed + * @param ServiceManagerConfiguration $config */ - abstract protected function getV2InvalidPluginException(); + abstract protected function getPluginManager(array $config = []): AbstractSingleInstancePluginManager; /** * Returns the value the instanceOf property has been set to * - * @return string + * @return class-string */ - abstract protected function getInstanceOf(); + abstract protected function getInstanceOf(): string; } diff --git a/test/ExamplePluginManagerTest.php b/test/ExamplePluginManagerTest.php index 60d64d2d..7f4d028e 100644 --- a/test/ExamplePluginManagerTest.php +++ b/test/ExamplePluginManagerTest.php @@ -4,13 +4,14 @@ namespace LaminasTest\ServiceManager; -use Laminas\ServiceManager\AbstractPluginManager; +use Laminas\ServiceManager\AbstractSingleInstancePluginManager; +use Laminas\ServiceManager\Factory\InvokableFactory; use Laminas\ServiceManager\ServiceManager; use Laminas\ServiceManager\Test\CommonPluginManagerTrait; use LaminasTest\ServiceManager\TestAsset\InvokableObject; use LaminasTest\ServiceManager\TestAsset\InvokableObjectPluginManager; use PHPUnit\Framework\TestCase; -use RuntimeException; +use function array_replace_recursive; /** * Example test of using CommonPluginManagerTrait @@ -21,17 +22,22 @@ final class ExamplePluginManagerTest extends TestCase { use CommonPluginManagerTrait; - /** - * @param ServiceManagerConfiguration $config - */ - protected function getPluginManager(array $config = []): AbstractPluginManager + protected function getPluginManager(array $config = []): AbstractSingleInstancePluginManager { - return new InvokableObjectPluginManager(new ServiceManager()); - } + $config = array_replace_recursive($config, [ + 'aliases' => [ + 'foo' => InvokableObject::class, + // v2 normalized FQCNs + 'laminastestservicemanagertestassetinvokableobject' => InvokableObject::class, + ], + 'factories' => [ + InvokableObject::class => InvokableFactory::class, + // Legacy (v2) due to alias resolution + 'laminastestservicemanagertestassetinvokableobject' => InvokableFactory::class, + ], + ]); - protected function getV2InvalidPluginException(): string - { - return RuntimeException::class; + return new InvokableObjectPluginManager(new ServiceManager(), $config); } protected function getInstanceOf(): string diff --git a/test/TestAsset/InvokableObjectPluginManager.php b/test/TestAsset/InvokableObjectPluginManager.php index 46f221cf..9f4ea8ed 100644 --- a/test/TestAsset/InvokableObjectPluginManager.php +++ b/test/TestAsset/InvokableObjectPluginManager.php @@ -6,35 +6,22 @@ use Laminas\ServiceManager\AbstractPluginManager; use Laminas\ServiceManager\AbstractSingleInstancePluginManager; -use Laminas\ServiceManager\Factory\InvokableFactory; +use Laminas\ServiceManager\ServiceManager; use Psr\Container\ContainerInterface; /** + * @psalm-import-type ServiceManagerConfiguration from ServiceManager * @template-extends AbstractPluginManager */ final class InvokableObjectPluginManager extends AbstractSingleInstancePluginManager { - /** @var array */ - protected array $aliases = [ - 'foo' => InvokableObject::class, - - // v2 normalized FQCNs - 'laminastestservicemanagertestassetinvokableobject' => InvokableObject::class, - ]; - - /** @var array */ - protected array $factories = [ - InvokableObject::class => InvokableFactory::class, - // Legacy (v2) due to alias resolution - 'laminastestservicemanagertestassetinvokableobject' => InvokableFactory::class, - ]; - protected string $instanceOf = InvokableObject::class; protected bool $sharedByDefault = false; - public function __construct(ContainerInterface $creationContext) + /** @param ServiceManagerConfiguration $config */ + public function __construct(ContainerInterface $creationContext, array $config = []) { - parent::__construct($creationContext, ['aliases' => $this->aliases, 'factories' => $this->factories]); + parent::__construct($creationContext, $config); } }