Skip to content

Commit

Permalink
Test for registered event subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
JanMikes committed Jul 19, 2023
1 parent 264ff7b commit 796ccbf
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/DI/Pass/EventPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,17 @@ private function getSubscribers(): array
];

// Backward compatibility
$subscribers[] = new Statement(
class_exists(StopWorkerOnSignalsListener::class)
? StopWorkerOnSignalsListener::class
: StopWorkerOnSigtermSignalListener::class // @phpstan-ignore-line
);
if (class_exists(StopWorkerOnSignalsListener::class)) {

Check failure on line 101 in src/DI/Pass/EventPass.php

View workflow job for this annotation

GitHub Actions / Codesniffer / Codesniffer (8.1)

Use ternary operator.
$subscribers[] = new Statement(StopWorkerOnSignalsListener::class, [
null,
$this->prefix('@logger.logger'),
]);
} else {
// @phpstan-ignore-next-line
$subscribers[] = new Statement(StopWorkerOnSigtermSignalListener::class, [
$this->prefix('@logger.logger'),
]);
}

return $subscribers;
}
Expand Down
39 changes: 39 additions & 0 deletions tests/Cases/DI/MessengerExtension.event.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Tester\Assert;
use Tests\Toolkit\Container;
use Symfony\Component\Messenger\EventListener\StopWorkerOnSignalsListener;
use Symfony\Component\Messenger\EventListener\SendFailedMessageToFailureTransportListener;
use Symfony\Component\Messenger\EventListener\SendFailedMessageForRetryListener;
use Symfony\Component\Messenger\EventListener\DispatchPcntlSignalListener;

require_once __DIR__ . '/../../bootstrap.php';

Expand Down Expand Up @@ -40,3 +44,38 @@ Toolkit::test(static function () {
Assert::true($container->isCreated('events.dispatcher'));
Assert::false($container->isCreated('messenger.event.dispatcher'));
});

// Event listeners should be registered
Toolkit::test(static function () {
$container = Container::of()
->withDefaults()
->withCompiler(static function (Compiler $compiler): void {
$compiler->addExtension('events', new EventDispatcherExtension());
})
->build();

/** @var EventDispatcher $dispatcher */
$dispatcher = $container->getService('messenger.event.dispatcher');

$dispatcherListeners = $dispatcher->getListeners();
$listeners = [];

foreach ($dispatcherListeners as $listenersForEvent) {
if (is_array($listenersForEvent)) {
foreach ($listenersForEvent as $listenerForEvent) {
$listeners[] = $listenerForEvent[0]::class;
}
}
}

$expectedRegisteredListeners = [
DispatchPcntlSignalListener::class,
SendFailedMessageForRetryListener::class,
SendFailedMessageToFailureTransportListener::class,
StopWorkerOnSignalsListener::class,
];

foreach ($expectedRegisteredListeners as $expectedRegisteredListener) {
Assert::true(in_array($expectedRegisteredListener, $listeners, true));
}
});

0 comments on commit 796ccbf

Please sign in to comment.