Skip to content

Commit

Permalink
Fixed container versions
Browse files Browse the repository at this point in the history
  • Loading branch information
awd-studio authored Aug 10, 2023
2 parents 687cb8b + 65c6052 commit a63a3bb
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 46 deletions.
36 changes: 30 additions & 6 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,38 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in(__DIR__)
->exclude('tests');
if (!file_exists(__DIR__ . '/src')) {
exit(0);
}

$config = new PhpCsFixer\Config();
$finder = new PhpCsFixer\Finder();

$finder
->in(__DIR__ . '/src')
->append([
__DIR__ . '/public',
__DIR__ . '/.php-cs-fixer.php',
])
->exclude('tests');

$config
->setCacheFile(__DIR__ . '/build/cache/php-cs-fixer/.php-cs-fixer.cache')
->setRules(['@Symfony' => true])
->setFinder($finder);
->setFinder($finder)
->setRiskyAllowed(true)
->setRules([
'@PHP71Migration' => true,
'@PHPUnit75Migration:risky' => true,
'@Symfony' => true,
'@Symfony:risky' => true,
'protected_to_private' => false,
'native_constant_invocation' => ['strict' => false],
'nullable_type_declaration_for_default_null_value' => ['use_nullable_type_declaration' => false],
'no_superfluous_phpdoc_tags' => ['remove_inheritdoc' => true],
'modernize_strpos' => true,
'get_class_to_class_keyword' => true,
'final_class' => true,
'concat_space' => ['spacing' => 'one'],
'phpdoc_to_comment' => ['ignored_tags' => ['psalm-suppress']],
])
->setCacheFile(__DIR__ . '/tools/cache/php-cs-fixer/.php-cs-fixer.cache');

return $config;
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "library",
"require": {
"php": "^8.2",
"psr/container": "^1.0"
"psr/container": "^1.0|^2.0"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
Expand Down Expand Up @@ -43,7 +43,7 @@
"mkdir -p build/cache/phpunit",
"mkdir -p build/cache/psalm",
"mkdir -p build/report",
"cd tools/php-cs-fixer && composer install --prefer-dist --no-progress --no-interaction && cd -"
"composer install --working-dir=tools/php-cs-fixer --prefer-dist --no-progress --no-interaction"
],
"dev": "@setup-dev",
"csf": "tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php",
Expand Down
2 changes: 1 addition & 1 deletion src/Bus/Exception/NoHandlerDefined.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ final class NoHandlerDefined extends BusException
{
public function __construct(object $message)
{
parent::__construct(\sprintf('No handlers for a message "%s"', $message::class), 1, null);
parent::__construct(sprintf('No handlers for a message "%s"', $message::class), 1, null);
}
}
2 changes: 1 addition & 1 deletion src/Bus/Handler/AutoClassHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function autoRegister(string $handlerId, string $handlerMethod = '__invok
try {
$messageId = $this->reader->read($classReflection->getMethod($handlerMethod));
} catch (\ReflectionException $e) {
throw new InvalidHandler(\sprintf('A handler "%s" supposed to have a method "%s" to register automatically.', $handlerId, $handlerMethod));
throw new InvalidHandler(sprintf('A handler "%s" supposed to have a method "%s" to register automatically.', $handlerId, $handlerMethod));
}

$this->parent->register($messageId, $handlerId, $handlerMethod);
Expand Down
2 changes: 1 addition & 1 deletion src/Bus/Handler/ParentsAwareClassHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function has(string $messageId): bool

public function get(string $messageId): \Iterator
{
foreach (\array_merge([$messageId], $this->parse($messageId)) as $implementation) {
foreach (array_merge([$messageId], $this->parse($messageId)) as $implementation) {
foreach ($this->handlers->get($implementation) as $handler) {
yield $handler;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bus/Handler/PsrContainerClassHandlerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(
public function register(string $messageId, string $handlerClass, string $handlerMethod = '__invoke'): void
{
if (false === $this->serviceLocator->has($handlerClass)) {
throw new InvalidHandler(\sprintf('There is no a service such as "%s" to handle a "%s" message', $handlerClass, $messageId));
throw new InvalidHandler(sprintf('There is no a service such as "%s" to handle a "%s" message', $handlerClass, $messageId));
}

$this->containerHandlers[$messageId][$handlerClass] = $handlerMethod;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function get(string $messageId): \Iterator
foreach ($this->containerHandlers[$messageId] as $handlerId => $handlerMethod) {
/** @var object $handler */
$handler = $this->serviceLocator->get($handlerId);
assert(\method_exists($handler, $handlerMethod));
\assert(method_exists($handler, $handlerMethod));
yield $handler->$handlerMethod(...);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Bus/Reader/ReflectionMessageIdReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function read(\ReflectionFunctionAbstract $callback): string
}

$messageId = $messageType->getName();
if (false === \class_exists($messageId) && false === \interface_exists($messageId) && false === \enum_exists($messageId)) {
if (false === class_exists($messageId) && false === interface_exists($messageId) && false === enum_exists($messageId)) {
throw new ParsingException('A message ID must represent an existing class.');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Event/SimpleEventBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ final class SimpleEventBus extends SimpleBus implements EventBus
{
public function handle(object $event): void
{
\iterator_to_array($this->handleMessage($event));
iterator_to_array($this->handleMessage($event));
}
}
70 changes: 39 additions & 31 deletions tests/Unit/Bus/Handler/PsrContainerHandlersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,22 @@ final class PsrContainerHandlersTest extends BusTestCase
/** @var \AwdStudio\Bus\Handler\PsrContainerClassHandlerRegistry */
private $instance;

/** @var \Prophecy\Prophecy\ObjectProphecy|\Psr\Container\ContainerInterface */
private $containerProphecy;
/** @var \Psr\Container\ContainerInterface|\Prophecy\Prophecy\ObjectProphecy */
private $serviceLocator;

/** @var \AwdStudio\Bus\HandlerLocator|\Prophecy\Prophecy\ObjectProphecy */
private $handlerLocatorProphecy;
private $dynamicHandlers;

protected function setUp(): void
{
parent::setUp();

$this->containerProphecy = $this->prophesize(ContainerInterface::class);
$this->handlerLocatorProphecy = $this->prophesize(HandlerLocator::class);
$this->serviceLocator = $this->prophesize(ContainerInterface::class);
$this->dynamicHandlers = $this->prophesize(HandlerLocator::class);

$this->instance = new PsrContainerClassHandlerRegistry(
$this->containerProphecy->reveal(),
$this->handlerLocatorProphecy->reveal()
$this->serviceLocator->reveal(),
$this->dynamicHandlers->reveal()
);
}

Expand All @@ -86,15 +86,15 @@ public function testMustImplementAHandlers(): void
*/
public function testMustAllowToInstantiateWithoutExternalHandlers(): void
{
$this->assertNotNull(new PsrContainerClassHandlerRegistry($this->containerProphecy->reveal()));
$this->assertNotNull(new PsrContainerClassHandlerRegistry($this->serviceLocator->reveal()));
}

/**
* @covers ::register
*/
public function testMustCheckAServiceWhenTriesToRegisterIt(): void
{
$this->containerProphecy
$this->serviceLocator
->has(Argument::exact(FooHandler::class))
->willReturn(true)
->shouldBeCalledOnce();
Expand All @@ -107,7 +107,7 @@ public function testMustCheckAServiceWhenTriesToRegisterIt(): void
*/
public function testMustThrowAnExceptionIfAHandlerIsNotInTheServiceLocator(): void
{
$this->containerProphecy
$this->serviceLocator
->has(Argument::exact(FooHandler::class))
->willReturn(false);

Expand All @@ -123,7 +123,7 @@ public function testMustCallAnExternalHandlersToAddADynamicHandler(): void
{
$dynamicHandler = static function (): void { return; };

$this->handlerLocatorProphecy
$this->dynamicHandlers
->add(Argument::exact(\stdClass::class), Argument::exact($dynamicHandler))
->shouldBeCalledOnce();

Expand All @@ -135,7 +135,7 @@ public function testMustCallAnExternalHandlersToAddADynamicHandler(): void
*/
public function testMustCheckAnExternalHandlersWhenLooksForAHandler(): void
{
$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(false)
->shouldBeCalledOnce();
Expand All @@ -148,7 +148,7 @@ public function testMustCheckAnExternalHandlersWhenLooksForAHandler(): void
*/
public function testMustReturnTrueIfAHandlerIsInExternalHandlers(): void
{
$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(true);

Expand All @@ -160,11 +160,11 @@ public function testMustReturnTrueIfAHandlerIsInExternalHandlers(): void
*/
public function testMustReturnTrueIfAHandlerRegisteredInTheContainerHandlers(): void
{
$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(false);

$this->containerProphecy
$this->serviceLocator
->has(Argument::exact(FooHandler::class))
->willReturn(true);

Expand All @@ -178,7 +178,7 @@ public function testMustReturnTrueIfAHandlerRegisteredInTheContainerHandlers():
*/
public function testMustReturnFalseIfThereAreNoHandlersNorInDynamicHandlersNotInRegisteredOnes(): void
{
$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(false);

Expand All @@ -192,11 +192,11 @@ public function testMustYieldAHandlerFromDynamicHandlersIfItHasSome(): void
{
$dynamicHandler = static function (): void { return; };

$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(true);

$this->handlerLocatorProphecy
$this->dynamicHandlers
->get(Argument::exact(FooCallback::class))
->willYield([$dynamicHandler]);

Expand All @@ -210,16 +210,20 @@ public function testMustYieldAHandlerFromContainerIfItRegistered(): void
{
$dynamicHandler = static function (): void { return; };

$this->instance->register(FooCallback::class, FooHandler::class);

$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(false);

$this->containerProphecy
$this->serviceLocator
->has(Argument::exact(FooHandler::class))
->willReturn(true);

$this->serviceLocator
->get(Argument::exact(FooHandler::class))
->willReturn($dynamicHandler);

$this->instance->register(FooCallback::class, FooHandler::class);

$this->assertContains($dynamicHandler, $this->instance->get(FooCallback::class));
}

Expand All @@ -232,21 +236,25 @@ public function testMustYieldAllHandlersFromBothAndDynamicHandlersAndTheContaine
$dynamicHandler2 = static function (): void { return; };
$dynamicHandler3 = static function (): void { return; };

$this->instance->register(FooCallback::class, FooHandler1::class);
$this->instance->register(FooCallback::class, FooHandler2::class);

$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::exact(FooCallback::class))
->willReturn(true);

$this->handlerLocatorProphecy
$this->dynamicHandlers
->get(Argument::exact(FooCallback::class))
->willYield([$dynamicHandler3]);

$this->containerProphecy
$this->serviceLocator
->get(Argument::any())
->willReturn($dynamicHandler1, $dynamicHandler2);

$this->serviceLocator
->has(Argument::any())
->willReturn(true);

$this->instance->register(FooCallback::class, FooHandler1::class);
$this->instance->register(FooCallback::class, FooHandler2::class);

$result = [];
foreach ($this->instance->get(FooCallback::class) as $handler) {
$result[] = $handler;
Expand All @@ -262,15 +270,15 @@ public function testMustYieldAllHandlersFromBothAndDynamicHandlersAndTheContaine
*/
public function testMustReturnACallableArrayAsAnObjectAndRegisteredMethod(): void
{
$this->handlerLocatorProphecy
$this->dynamicHandlers
->has(Argument::any())
->willReturn(false);

$this->containerProphecy
$this->serviceLocator
->has(Argument::exact(FooHandler3::class))
->willReturn(true);

$this->containerProphecy
$this->serviceLocator
->get(Argument::exact(FooHandler3::class))
->willReturn(new FooHandler3());

Expand Down

0 comments on commit a63a3bb

Please sign in to comment.