Skip to content

Commit

Permalink
Merge branch 'release/v4.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Invis1ble committed Jun 1, 2024
2 parents 6bb5d6a + 82ed061 commit e762316
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 191 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "invis1ble/messenger-bundle",
"version": "3.1.0",
"version": "4.0.0",
"type": "symfony-bundle",
"description": "Symfony Bundle for the Bus and Message Interfaces and Implementations.",
"authors": [
Expand Down
6 changes: 1 addition & 5 deletions config/packages/messenger.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@
https://symfony.com/schema/dic/symfony/symfony-1.0.xsd">

<framework:config>
<framework:messenger default-bus="messenger.bus.command">
<framework:routing message-class="Invis1ble\Messenger\Event\EventInterface">
<framework:sender service="async"/>
</framework:routing>

<framework:messenger default-bus="messenger.bus.event.async">
<framework:transport name="async" dsn="%env(MESSENGER_TRANSPORT_DSN)%">
<framework:retry-strategy max-retries="3" delay="1000" multiplier="2" max-delay="0"/>
</framework:transport>
Expand Down
28 changes: 0 additions & 28 deletions src/DependencyInjection/Invis1bleMessengerExtension.php

This file was deleted.

10 changes: 2 additions & 8 deletions src/DependencyInjection/RegisterMessageHandlersPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ public function process(ContainerBuilder $container): void
QueryHandlerInterface::class => 'messenger.bus.query',
EventHandlerInterface::class => 'messenger.bus.event.async',
] as $handler => $bus) {
$definition = $container->registerForAutoconfiguration($handler);

if ($definition->hasTag($messageHandlerTag)) {
return;
}

$definition
->setPublic(true)
$container->registerForAutoconfiguration($handler)
->clearTag($messageHandlerTag)
->addTag($messageHandlerTag, ['bus' => $bus])
;
}
Expand Down
20 changes: 18 additions & 2 deletions src/Invis1bleMessengerBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@
use Invis1ble\MessengerBundle\DependencyInjection\RegisterMessageHandlersPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class Invis1bleMessengerBundle extends Bundle
class Invis1bleMessengerBundle extends AbstractBundle
{
public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new RegisterMessageHandlersPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 99999);
}

public function loadExtension(
array $config,
ContainerConfigurator $container,
ContainerBuilder $builder,
): void {
$container->import('../config/services.xml');
}

public function prependExtension(
ContainerConfigurator $container,
ContainerBuilder $builder,
): void {
$container->import('../config/packages/messenger.xml');
}
}
75 changes: 0 additions & 75 deletions tests/DependencyInjection/Invis1bleMessengerExtensionTest.php

This file was deleted.

91 changes: 28 additions & 63 deletions tests/DependencyInjection/RegisterMessageHandlersPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,87 +4,52 @@

namespace Invis1ble\MessengerBundle\Tests\DependencyInjection;

use Invis1ble\Messenger\Command\CommandHandlerInterface;
use Invis1ble\Messenger\Event\EventHandlerInterface;
use Invis1ble\Messenger\Query\QueryHandlerInterface;
use Invis1ble\MessengerBundle\DependencyInjection\RegisterMessageHandlersPass;
use Invis1ble\MessengerBundle\Tests\MessageHandler\TestCommandHandler;
use Invis1ble\MessengerBundle\Tests\MessageHandler\TestEventHandler;
use Invis1ble\MessengerBundle\Tests\MessageHandler\TestQueryHandler;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractCompilerPassTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

class RegisterMessageHandlersPassTest extends TestCase
class RegisterMessageHandlersPassTest extends AbstractCompilerPassTestCase
{
#[DataProvider('provideHandlerAndCorrespondingMessageBus')]
public function testProcess(
string $handlerClassName,
#[DataProvider('provideHandlerAndCorrespondingBus')]
public function testMessageHandlerTagged(
string $handlerFqn,
string $bus,
string $tag = 'messenger.message_handler',
): void {
$container = new ContainerBuilder();
$definition = new Definition();
$definition->setAutoconfigured(true);
$this->setDefinition($handlerFqn, $definition);

$this->process($container);
$this->compile();

$definitions = $container->getAutoconfiguredInstanceof();

$this->assertArrayHasKey(
$handlerClassName,
$definitions,
sprintf('Interface "%s" is not autoconfigured.', $handlerClassName),
);
$this->assertTrue(
$definitions[$handlerClassName]->hasTag($tag),
sprintf('Interface "%s" must be tagged as "%s".', $handlerClassName, $tag),
);

$tag = $definitions[$handlerClassName]->getTag($tag)[0];

$this->assertArrayHasKey(
'bus',
$tag,
sprintf('Interface "%s" tag must have attribute "bus".', $handlerClassName),
);
$this->assertSame(
$bus,
$tag['bus'],
sprintf('Command handler definition tag attribute "bus" must be "%s".', $bus),
$this->assertContainerBuilderHasServiceDefinitionWithTag(
$handlerFqn,
'messenger.message_handler',
['bus' => $bus],
);
}

#[DataProvider('provideHandlerAndCorrespondingMessageBus')]
public function testAddMessageHandlerTagOnlyOnce(
string $handlerClassName,
string $bus,
): void {
$container = new ContainerBuilder();

$container->registerForAutoconfiguration($handlerClassName)
->setPublic(true)
->addTag('messenger.message_handler', ['bus' => $bus])
;

$this->process($container);

$definitions = $container->getAutoconfiguredInstanceof();
$tag = $definitions[$handlerClassName]->getTag('messenger.message_handler');

$this->assertSame([['bus' => $bus]], $tag);
}

/**
* @return \Generator<array<string, string>>
*/
public static function provideHandlerAndCorrespondingMessageBus(): \Generator
public static function provideHandlerAndCorrespondingBus(): iterable
{
yield from [
[CommandHandlerInterface::class, 'messenger.bus.command'],
[QueryHandlerInterface::class, 'messenger.bus.query'],
[EventHandlerInterface::class, 'messenger.bus.event.async'],
];
yield [TestCommandHandler::class, 'messenger.bus.command'];
yield [TestQueryHandler::class, 'messenger.bus.query'];
yield [TestEventHandler::class, 'messenger.bus.event.async'];
}

protected function process(ContainerBuilder $container): void
protected function registerCompilerPass(ContainerBuilder $container): void
{
(new RegisterMessageHandlersPass())
->process($container);
$container->addCompilerPass(
new RegisterMessageHandlersPass(),
PassConfig::TYPE_BEFORE_OPTIMIZATION,
99999,
);
}
}
70 changes: 61 additions & 9 deletions tests/Invis1bleMessengerBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,27 @@

namespace Invis1ble\MessengerBundle\Tests;

use Invis1ble\Messenger\Command\CommandBus;
use Invis1ble\Messenger\Command\CommandBusInterface;
use Invis1ble\Messenger\Event\EventBus;
use Invis1ble\Messenger\Event\EventBusInterface;
use Invis1ble\Messenger\Query\QueryBus;
use Invis1ble\Messenger\Query\QueryBusInterface;
use Invis1ble\MessengerBundle\DependencyInjection\RegisterMessageHandlersPass;
use Invis1ble\MessengerBundle\Invis1bleMessengerBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;
use PHPUnit\Framework\Attributes\DataProvider;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;

class Invis1bleMessengerBundleTest extends TestCase
class Invis1bleMessengerBundleTest extends AbstractExtensionTestCase
{
public function testBuild(): void
public function testContainerHasRegisterMessageHandlersPass(): void
{
$bundle = new Invis1bleMessengerBundle();
$container = new ContainerBuilder();
$bundle = $this->createBundle();
$bundle->build($this->container);

$bundle->build($container);

$passes = $container->getCompilerPassConfig()
$passes = $this->container->getCompilerPassConfig()
->getPasses();

$found = false;
Expand All @@ -36,4 +42,50 @@ public function testBuild(): void
sprintf('%s is not added to the container.', RegisterMessageHandlersPass::class),
);
}

#[DataProvider('provideBus')]
public function testContainerContainsBus(string $serviceFqn, string $aliasFqn, string $busName): void
{
$this->load();
$this->compile();

$this->assertContainerBuilderHasServiceDefinitionWithArgument(
serviceId: $serviceFqn,
argumentIndex: 0,
expectedValue: new Reference($busName),
);

$this->assertContainerBuilderHasService($serviceFqn, $serviceFqn);
$this->assertContainerBuilderHasAlias($aliasFqn, $serviceFqn);
}

/**
* @return \Generator<array<string, string>>
*/
public static function provideBus(): iterable
{
yield [CommandBus::class, CommandBusInterface::class, 'messenger.bus.command'];
yield [QueryBus::class, QueryBusInterface::class, 'messenger.bus.query'];
yield [EventBus::class, EventBusInterface::class, 'messenger.bus.event.async'];
}

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

$this->setParameter('kernel.environment', 'test');
$this->setParameter('kernel.build_dir', __DIR__);
}

protected function getContainerExtensions(): array
{
return [
$this->createBundle()->getContainerExtension(),
];
}

private function createBundle(): AbstractBundle
{
return new Invis1bleMessengerBundle();
}
}
16 changes: 16 additions & 0 deletions tests/MessageHandler/TestCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Invis1ble\MessengerBundle\Tests\MessageHandler;

use Invis1ble\Messenger\Command\CommandHandlerInterface;
use Invis1ble\Messenger\Command\CommandInterface;

class TestCommandHandler implements CommandHandlerInterface
{
public function __invoke(CommandInterface $command): void
{
// do nothing
}
}
Loading

0 comments on commit e762316

Please sign in to comment.