Skip to content

Commit 9191a7c

Browse files
committed
Implement SubscriberInterface for listeners
1 parent 9b8e072 commit 9191a7c

4 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/Listener/OutputListener.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,24 @@
1616
use FiveLab\Component\Amqp\Consumer\ConsumerStoppedReason;
1717
use FiveLab\Component\Amqp\Event\ConsumerStoppedEvent;
1818
use FiveLab\Component\Amqp\Event\ReceiveMessageEvent;
19+
use Symfony\Component\Console\ConsoleEvents;
1920
use Symfony\Component\Console\Event\ConsoleCommandEvent;
2021
use Symfony\Component\Console\Output\OutputInterface;
22+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2123

22-
class OutputListener
24+
class OutputListener implements EventSubscriberInterface
2325
{
2426
private ?OutputInterface $output = null;
2527

28+
public static function getSubscribedEvents(): array
29+
{
30+
return [
31+
ConsoleEvents::COMMAND => ['onConsoleCommand', 0],
32+
ConsumerStoppedEvent::class => ['onConsumerStopped', 0],
33+
ReceiveMessageEvent::class => ['onReceiveMessage', 0],
34+
];
35+
}
36+
2637
public function onConsoleCommand(ConsoleCommandEvent $event): void
2738
{
2839
$this->output = $event->getOutput();

src/Listener/StopAfterNExecutesListener.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ public function __construct(private readonly EventDispatcherInterface $eventDisp
2727
{
2828
}
2929

30+
public static function getSubscribedEvents(): array
31+
{
32+
return [
33+
ProcessedMessageEvent::class => ['onProcessedMessage', -1024],
34+
];
35+
}
36+
3037
public function onProcessedMessage(ProcessedMessageEvent $event): void
3138
{
3239
$this->executesCounter++;
@@ -39,11 +46,4 @@ public function onProcessedMessage(ProcessedMessageEvent $event): void
3946
$event->consumer->stop();
4047
}
4148
}
42-
43-
public static function getSubscribedEvents(): array
44-
{
45-
return [
46-
ProcessedMessageEvent::class => ['onProcessedMessage', 0],
47-
];
48-
}
4949
}

tests/Unit/Listener/OutputListenerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ protected function setUp(): void
4040
$this->listener = new OutputListener();
4141
}
4242

43+
#[Test]
44+
public function shouldSuccessGetListeners(): void
45+
{
46+
self::assertEquals([
47+
'console.command' => ['onConsoleCommand', 0],
48+
ConsumerStoppedEvent::class => ['onConsumerStopped', 0],
49+
ReceiveMessageEvent::class => ['onReceiveMessage', 0],
50+
], OutputListener::getSubscribedEvents());
51+
}
52+
4353
#[Test]
4454
#[TestWith([ConsumerStoppedReason::Timeout, '<comment>Receive consumer timeout exceed error.</comment>'])]
4555
#[TestWith([ConsumerStoppedReason::StopConsuming, '<comment>Stop consuming.</comment>'])]

tests/Unit/Listener/StopAfterNExecutesListenerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@
2323

2424
class StopAfterNExecutesListenerTest extends TestCase
2525
{
26+
#[Test]
27+
public function shouldSuccessGetListeners(): void
28+
{
29+
self::assertEquals([
30+
ProcessedMessageEvent::class => ['onProcessedMessage', -1024],
31+
], StopAfterNExecutesListener::getSubscribedEvents());
32+
}
33+
2634
#[Test]
2735
public function shouldSuccessStopAfterReachedLimit(): void
2836
{

0 commit comments

Comments
 (0)