Skip to content

Commit

Permalink
qa: adapt changes to CommonPluginManagerTrait
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilian Bösing <[email protected]>
  • Loading branch information
boesing committed May 2, 2023
1 parent 9f7d6d7 commit 8f14d75
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 63 deletions.
15 changes: 1 addition & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@5efddb42013538f9e4a757dc2a83b0acc41c1a2e">
<files psalm-version="dev-master@c059388274a641b0c577c11b845050891fbfbe2c">
<file src="src/AbstractFactory/ConfigAbstractFactory.php">
<InvalidStringClass>
<code>new $requestedName(...$arguments)</code>
Expand Down Expand Up @@ -110,19 +110,6 @@
<code><![CDATA[$this->getServiceNotFoundException()]]></code>
<code><![CDATA[$this->getServiceNotFoundException()]]></code>
</ArgumentTypeCoercion>
<MissingReturnType>
<code>testInstanceOfMatches</code>
<code>testLoadingInvalidElementRaisesException</code>
<code>testPluginAliasesResolve</code>
<code>testRegisteringInvalidElementRaisesException</code>
</MissingReturnType>
<MixedAssignment>
<code>$alias</code>
<code>$expected</code>
</MixedAssignment>
<MixedInferredReturnType>
<code>array</code>
</MixedInferredReturnType>
</file>
<file src="src/Tool/ConfigDumper.php">
<MixedArgument>
Expand Down
57 changes: 27 additions & 30 deletions src/Test/CommonPluginManagerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,38 @@
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;
use function assert;
use function is_string;

/**
* 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);
Expand All @@ -42,55 +46,48 @@ 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<array{string,string}>
*/
public function aliasProvider()
public function aliasProvider(): array
{
$manager = $this->getPluginManager();
$reflection = new ReflectionProperty($manager, 'aliases');
$manager = $this->getPluginManager();
$pluginContainerProperty = new ReflectionProperty(AbstractPluginManager::class, 'plugins');
$pluginContainer = $pluginContainerProperty->getValue($manager);
self::assertInstanceOf(ServiceManager::class, $pluginContainer);

$reflection = new ReflectionProperty($pluginContainer, 'aliases');
$data = [];
foreach ($reflection->getValue($manager) as $alias => $expected) {
foreach ($reflection->getValue($pluginContainer) as $alias => $expected) {
assert(is_string($alias) && is_string($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;
}
15 changes: 3 additions & 12 deletions test/ExamplePluginManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@

namespace LaminasTest\ServiceManager;

use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\AbstractSingleInstancePluginManager;
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;

/**
* Example test of using CommonPluginManagerTrait
Expand All @@ -21,17 +20,9 @@ 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());
}

protected function getV2InvalidPluginException(): string
{
return RuntimeException::class;
return new InvokableObjectPluginManager(new ServiceManager(), $config);
}

protected function getInstanceOf(): string
Expand Down
11 changes: 4 additions & 7 deletions test/TestAsset/InvokableObjectPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
use Laminas\ServiceManager\AbstractPluginManager;
use Laminas\ServiceManager\AbstractSingleInstancePluginManager;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Psr\Container\ContainerInterface;
use Laminas\ServiceManager\ServiceManager;

/**
* @psalm-import-type ServiceManagerConfiguration from ServiceManager
* @psalm-import-type FactoriesConfiguration from ServiceManager
* @template-extends AbstractPluginManager<InvokableObject>
*/
final class InvokableObjectPluginManager extends AbstractSingleInstancePluginManager
Expand All @@ -22,7 +24,7 @@ final class InvokableObjectPluginManager extends AbstractSingleInstancePluginMan
'laminastestservicemanagertestassetinvokableobject' => InvokableObject::class,
];

/** @var array<string,string> */
/** @var FactoriesConfiguration */
protected array $factories = [
InvokableObject::class => InvokableFactory::class,
// Legacy (v2) due to alias resolution
Expand All @@ -32,9 +34,4 @@ final class InvokableObjectPluginManager extends AbstractSingleInstancePluginMan
protected string $instanceOf = InvokableObject::class;

protected bool $sharedByDefault = false;

public function __construct(ContainerInterface $creationContext)
{
parent::__construct($creationContext, ['aliases' => $this->aliases, 'factories' => $this->factories]);
}
}

0 comments on commit 8f14d75

Please sign in to comment.