Skip to content

Commit c698b5d

Browse files
committed
Add SymfonyEventBus implementation
1 parent 7922012 commit c698b5d

File tree

5 files changed

+113
-0
lines changed

5 files changed

+113
-0
lines changed

src/SymfonyEventBus.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\MessageBusSymfony;
6+
7+
use Gember\EventSourcing\Util\Messaging\MessageBus\EventBus;
8+
use Gember\EventSourcing\Util\Messaging\MessageBus\HandlingMessageFailedException;
9+
use Symfony\Component\Messenger\Exception\ExceptionInterface;
10+
use Symfony\Component\Messenger\MessageBusInterface;
11+
12+
final readonly class SymfonyEventBus implements EventBus
13+
{
14+
public function __construct(
15+
private MessageBusInterface $eventBus,
16+
) {}
17+
18+
public function handle(object $event): void
19+
{
20+
try {
21+
$this->eventBus->dispatch($event);
22+
} catch (ExceptionInterface $exception) {
23+
throw HandlingMessageFailedException::withException($exception);
24+
}
25+
}
26+
}

tests/SymfonyEventBusTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\MessageBusSymfony\Test;
6+
7+
use Gember\EventSourcing\Util\Messaging\MessageBus\HandlingMessageFailedException;
8+
use Gember\MessageBusSymfony\SymfonyEventBus;
9+
use Gember\MessageBusSymfony\Test\TestDoubles\TestEvent;
10+
use Gember\MessageBusSymfony\Test\TestDoubles\TestEventThrowingException;
11+
use Gember\MessageBusSymfony\Test\TestDoubles\TestSymfonyEventBus;
12+
use PHPUnit\Framework\Attributes\Test;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @internal
17+
*/
18+
final class SymfonyEventBusTest extends TestCase
19+
{
20+
private SymfonyEventBus $eventBus;
21+
private TestSymfonyEventBus $symfonyEventBus;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$this->eventBus = new SymfonyEventBus(
28+
$this->symfonyEventBus = new TestSymfonyEventBus(),
29+
);
30+
}
31+
32+
#[Test]
33+
public function itShouldHandleEvent(): void
34+
{
35+
$this->eventBus->handle(new TestEvent());
36+
37+
self::assertTrue($this->symfonyEventBus->isCalled);
38+
}
39+
40+
#[Test]
41+
public function itShouldThrowExceptionWhenHandlingEventFailed(): void
42+
{
43+
self::expectException(HandlingMessageFailedException::class);
44+
45+
$this->eventBus->handle(new TestEventThrowingException());
46+
}
47+
}

tests/TestDoubles/TestEvent.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\MessageBusSymfony\Test\TestDoubles;
6+
7+
final readonly class TestEvent {}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\MessageBusSymfony\Test\TestDoubles;
6+
7+
final readonly class TestEventThrowingException {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Gember\MessageBusSymfony\Test\TestDoubles;
6+
7+
use Symfony\Component\Messenger\Envelope;
8+
use Symfony\Component\Messenger\Exception\ExceptionInterface;
9+
use Symfony\Component\Messenger\MessageBusInterface;
10+
use Exception;
11+
12+
final class TestSymfonyEventBus implements MessageBusInterface
13+
{
14+
public bool $isCalled;
15+
16+
public function dispatch(object $message, array $stamps = []): Envelope
17+
{
18+
$this->isCalled = true;
19+
20+
if ($message instanceof TestEventThrowingException) {
21+
throw new class extends Exception implements ExceptionInterface {};
22+
}
23+
24+
return new Envelope($message);
25+
}
26+
}

0 commit comments

Comments
 (0)