Add tests for InstanceManager and remove Decorator hook logic (avoid complex Dice logic)
This commit is contained in:
parent
527622df4a
commit
93af6f0564
14 changed files with 218 additions and 390 deletions
|
@ -21,7 +21,7 @@
|
|||
|
||||
namespace Friendica\Test\Util\Hooks\InstanceMocks;
|
||||
|
||||
class FakeInstance
|
||||
class FakeInstance implements IAmADecoratedInterface
|
||||
{
|
||||
protected $aText = null;
|
||||
protected $cBool = null;
|
||||
|
@ -39,6 +39,8 @@ class FakeInstance
|
|||
$this->aText = $aText;
|
||||
$this->cBool = $cBool;
|
||||
$this->bText = $bText;
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
public function getAText(): ?string
|
||||
|
|
|
@ -25,14 +25,14 @@ class FakeInstanceDecorator implements IAmADecoratedInterface
|
|||
{
|
||||
public static $countInstance = 0;
|
||||
|
||||
const PREFIX = 'prefix1';
|
||||
|
||||
/** @var IAmADecoratedInterface */
|
||||
protected $orig;
|
||||
protected $prefix = '';
|
||||
|
||||
public function __construct(IAmADecoratedInterface $orig, string $prefix = '')
|
||||
public function __construct(IAmADecoratedInterface $orig)
|
||||
{
|
||||
$this->orig = $orig;
|
||||
$this->prefix = $prefix;
|
||||
|
||||
self::$countInstance++;
|
||||
}
|
||||
|
@ -44,16 +44,16 @@ class FakeInstanceDecorator implements IAmADecoratedInterface
|
|||
|
||||
public function getAText(): ?string
|
||||
{
|
||||
return $this->prefix . $this->orig->getAText();
|
||||
return static::PREFIX . $this->orig->getAText();
|
||||
}
|
||||
|
||||
public function getBText(): ?string
|
||||
{
|
||||
return $this->prefix . $this->orig->getBText();
|
||||
return static::PREFIX . $this->orig->getBText();
|
||||
}
|
||||
|
||||
public function getCBool(): ?bool
|
||||
{
|
||||
return $this->prefix . $this->orig->getCBool();
|
||||
return static::PREFIX . $this->orig->getCBool();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ class AddonLoaderTest extends MockedTest
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -79,7 +79,7 @@ EOF;
|
|||
'addon/testaddon1/static/hooks.config.php' => $this->content,
|
||||
],
|
||||
'assertion' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -94,7 +94,7 @@ EOF;
|
|||
'addon/testaddon2/static/hooks.config.php' => $this->content,
|
||||
],
|
||||
'assertion' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => ['', ''],
|
||||
],
|
||||
|
@ -118,7 +118,7 @@ EOF;
|
|||
'addon/testaddon2/static/hooks.config.php' => $this->content,
|
||||
],
|
||||
'assertion' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
|
|
@ -22,25 +22,27 @@
|
|||
namespace Friendica\Test\src\Core\Hooks\Model;
|
||||
|
||||
use Dice\Dice;
|
||||
use Friendica\Core\Hooks\Exceptions\HookInstanceException;
|
||||
use Friendica\Core\Hooks\Exceptions\HookRegisterArgumentException;
|
||||
use Friendica\Core\Hooks\Model\DiceInstanceManager;
|
||||
use Friendica\Core\Hooks\Util\HookFileManager;
|
||||
use Friendica\Test\MockedTest;
|
||||
use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstance;
|
||||
use Friendica\Test\Util\Hooks\InstanceMocks\FakeInstanceDecorator;
|
||||
use Friendica\Test\Util\Hooks\InstanceMocks\IAmADecoratedInterface;
|
||||
use Mockery\MockInterface;
|
||||
|
||||
class InstanceManagerTest extends MockedTest
|
||||
{
|
||||
public function testEqualButNotSameInstance()
|
||||
/** @var HookFileManager|MockInterface */
|
||||
protected $hookFileManager;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
parent::setUp();
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class);
|
||||
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake');
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake');
|
||||
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
$this->hookFileManager = \Mockery::mock(HookFileManager::class);
|
||||
$this->hookFileManager->shouldReceive('setupHooks')->withAnyArgs();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
|
@ -50,6 +52,19 @@ class InstanceManagerTest extends MockedTest
|
|||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function testEqualButNotSameInstance()
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
|
||||
$getInstanceA = $instance->create(IAmADecoratedInterface::class, 'fake');
|
||||
$getInstanceB = $instance->create(IAmADecoratedInterface::class, 'fake');
|
||||
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
}
|
||||
|
||||
public function dataTests(): array
|
||||
{
|
||||
return [
|
||||
|
@ -79,9 +94,9 @@ class InstanceManagerTest extends MockedTest
|
|||
/**
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testInstanceWithConstructorAnonymArgs(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
public function testInstanceWithArgs(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
|
||||
|
||||
$args = [];
|
||||
|
||||
|
@ -95,45 +110,12 @@ class InstanceManagerTest extends MockedTest
|
|||
$args[] = $cBool;
|
||||
}
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class, $args);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
|
||||
/** @var IAmADecoratedInterface $getInstanceA */
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake');
|
||||
$getInstanceA = $instance->create(IAmADecoratedInterface::class, 'fake', $args);
|
||||
/** @var IAmADecoratedInterface $getInstanceB */
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake');
|
||||
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
self::assertEquals($aString, $getInstanceA->getAText());
|
||||
self::assertEquals($aString, $getInstanceB->getAText());
|
||||
self::assertEquals($bString, $getInstanceA->getBText());
|
||||
self::assertEquals($bString, $getInstanceB->getBText());
|
||||
self::assertEquals($cBool, $getInstanceA->getCBool());
|
||||
self::assertEquals($cBool, $getInstanceB->getCBool());
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testInstanceConstructorAndGetInstanceWithNamedArgs(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
|
||||
$args = [];
|
||||
|
||||
if (isset($aString)) {
|
||||
$args[] = $aString;
|
||||
}
|
||||
if (isset($cBool)) {
|
||||
$args[] = $cBool;
|
||||
}
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class, $args);
|
||||
|
||||
/** @var IAmADecoratedInterface $getInstanceA */
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake', [$bString]);
|
||||
/** @var IAmADecoratedInterface $getInstanceB */
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake', [$bString]);
|
||||
$getInstanceB = $instance->create(IAmADecoratedInterface::class, 'fake', $args);
|
||||
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
|
@ -150,24 +132,27 @@ class InstanceManagerTest extends MockedTest
|
|||
*/
|
||||
public function testInstanceWithTwoStrategies(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
|
||||
|
||||
$args = [];
|
||||
|
||||
if (isset($aString)) {
|
||||
$args[] = $aString;
|
||||
}
|
||||
if (isset($bString)) {
|
||||
$args[] = $bString;
|
||||
}
|
||||
if (isset($cBool)) {
|
||||
$args[] = $cBool;
|
||||
}
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class, $args);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake23', FakeInstance::class, $args);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake23');
|
||||
|
||||
/** @var IAmADecoratedInterface $getInstanceA */
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake', [$bString]);
|
||||
$getInstanceA = $instance->create(IAmADecoratedInterface::class, 'fake', $args);
|
||||
/** @var IAmADecoratedInterface $getInstanceB */
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake23', [$bString]);
|
||||
$getInstanceB = $instance->create(IAmADecoratedInterface::class, 'fake23', $args);
|
||||
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
|
@ -180,79 +165,74 @@ class InstanceManagerTest extends MockedTest
|
|||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataTests
|
||||
* Test the exception in case the interface was already registered
|
||||
*/
|
||||
public function testDecorator(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
public function testDoubleRegister()
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
self::expectException(HookRegisterArgumentException::class);
|
||||
self::expectExceptionMessage(sprintf('A class with the name %s is already set for the interface %s', 'fake', IAmADecoratedInterface::class));
|
||||
|
||||
$args = [];
|
||||
|
||||
if (isset($aString)) {
|
||||
$args[] = $aString;
|
||||
}
|
||||
if (isset($cBool)) {
|
||||
$args[] = $cBool;
|
||||
}
|
||||
|
||||
$prefix = 'prefix1';
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class, $args);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake23', FakeInstance::class, $args);
|
||||
$instance->registerDecorator(IAmADecoratedInterface::class, FakeInstanceDecorator::class, [$prefix]);
|
||||
|
||||
/** @var IAmADecoratedInterface $getInstanceA */
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake', [$bString]);
|
||||
/** @var IAmADecoratedInterface $getInstanceB */
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake23', [$bString]);
|
||||
|
||||
self::assertEquals(2, FakeInstanceDecorator::$countInstance);
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
self::assertEquals($prefix . $aString, $getInstanceA->getAText());
|
||||
self::assertEquals($prefix . $aString, $getInstanceB->getAText());
|
||||
self::assertEquals($prefix . $bString, $getInstanceA->getBText());
|
||||
self::assertEquals($prefix . $bString, $getInstanceB->getBText());
|
||||
self::assertEquals($prefix . $cBool, $getInstanceA->getCBool());
|
||||
self::assertEquals($prefix . $cBool, $getInstanceB->getCBool());
|
||||
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the exception in case the name of the instance isn't registered
|
||||
*/
|
||||
public function testWrongInstanceName()
|
||||
{
|
||||
self::expectException(HookInstanceException::class );
|
||||
self::expectExceptionMessage(sprintf('The class with the name %s isn\'t registered for the class or interface %s', 'fake', IAmADecoratedInterface::class));
|
||||
|
||||
$instance = new DiceInstanceManager(new Dice(), $this->hookFileManager);
|
||||
$instance->create(IAmADecoratedInterface::class, 'fake');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test in case there are already some rules
|
||||
*
|
||||
* @dataProvider dataTests
|
||||
*/
|
||||
public function testTwoDecoratorWithPrefix(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
public function testWithGivenRules(string $aString = null, bool $cBool = null, string $bString = null)
|
||||
{
|
||||
$instance = new DiceInstanceManager(new Dice());
|
||||
|
||||
$args = [];
|
||||
|
||||
if (isset($aString)) {
|
||||
$args[] = $aString;
|
||||
}
|
||||
if (isset($bString)) {
|
||||
$args[] = $bString;
|
||||
}
|
||||
|
||||
$dice = (new Dice())->addRules([
|
||||
FakeInstance::class => [
|
||||
'constructParams' => $args,
|
||||
],
|
||||
]);
|
||||
|
||||
$args = [];
|
||||
|
||||
if (isset($cBool)) {
|
||||
$args[] = $cBool;
|
||||
}
|
||||
|
||||
$prefix = 'prefix1';
|
||||
$instance = new DiceInstanceManager($dice, $this->hookFileManager);
|
||||
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake', FakeInstance::class, $args);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, 'fake23', FakeInstance::class, $args);
|
||||
$instance->registerDecorator(IAmADecoratedInterface::class, FakeInstanceDecorator::class, [$prefix]);
|
||||
$instance->registerDecorator(IAmADecoratedInterface::class, FakeInstanceDecorator::class);
|
||||
$instance->registerStrategy(IAmADecoratedInterface::class, FakeInstance::class, 'fake');
|
||||
|
||||
/** @var IAmADecoratedInterface $getInstanceA */
|
||||
$getInstanceA = $instance->createWithName(IAmADecoratedInterface::class, 'fake', [$bString]);
|
||||
$getInstanceA = $instance->create(IAmADecoratedInterface::class, 'fake', $args);
|
||||
/** @var IAmADecoratedInterface $getInstanceB */
|
||||
$getInstanceB = $instance->createWithName(IAmADecoratedInterface::class, 'fake23', [$bString]);
|
||||
$getInstanceB = $instance->create(IAmADecoratedInterface::class, 'fake', $args);
|
||||
|
||||
self::assertEquals(4, FakeInstanceDecorator::$countInstance);
|
||||
self::assertEquals($getInstanceA, $getInstanceB);
|
||||
self::assertNotSame($getInstanceA, $getInstanceB);
|
||||
self::assertEquals($prefix . $aString, $getInstanceA->getAText());
|
||||
self::assertEquals($prefix . $aString, $getInstanceB->getAText());
|
||||
self::assertEquals($prefix . $bString, $getInstanceA->getBText());
|
||||
self::assertEquals($prefix . $bString, $getInstanceB->getBText());
|
||||
self::assertEquals($prefix . $cBool, $getInstanceA->getCBool());
|
||||
self::assertEquals($prefix . $cBool, $getInstanceB->getCBool());
|
||||
self::assertEquals($aString, $getInstanceA->getAText());
|
||||
self::assertEquals($aString, $getInstanceB->getAText());
|
||||
self::assertEquals($bString, $getInstanceA->getBText());
|
||||
self::assertEquals($bString, $getInstanceB->getBText());
|
||||
self::assertEquals($cBool, $getInstanceA->getCBool());
|
||||
self::assertEquals($cBool, $getInstanceB->getCBool());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,12 +31,12 @@ class HookFileManagerTest extends MockedTest
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
],
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::DECORATOR => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::DECORATOR => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class,
|
||||
],
|
||||
|
@ -56,12 +56,12 @@ EOF,
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => '',
|
||||
],
|
||||
],
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::DECORATOR => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::DECORATOR => [
|
||||
\Psr\Log\LoggerInterface::class => \Psr\Log\NullLogger::class,
|
||||
],
|
||||
];
|
||||
|
@ -79,7 +79,7 @@ EOF,
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -87,7 +87,7 @@ return [
|
|||
];
|
||||
EOF,
|
||||
'addonsArray' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => ['null'],
|
||||
],
|
||||
|
@ -104,7 +104,7 @@ EOF,
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -112,7 +112,7 @@ return [
|
|||
];
|
||||
EOF,
|
||||
'addonsArray' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => 'null',
|
||||
],
|
||||
|
@ -130,7 +130,7 @@ EOF,
|
|||
<?php
|
||||
|
||||
return [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -138,7 +138,7 @@ return [
|
|||
];
|
||||
EOF,
|
||||
'addonsArray' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
@ -163,7 +163,7 @@ return [
|
|||
];
|
||||
EOF,
|
||||
'addonsArray' => [
|
||||
\Friendica\Core\Hooks\Capabilities\HookType::STRATEGY => [
|
||||
\Friendica\Core\Hooks\Capabilities\BehavioralHookType::STRATEGY => [
|
||||
\Psr\Log\LoggerInterface::class => [
|
||||
\Psr\Log\NullLogger::class => [''],
|
||||
],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue