Skip to content

Commit 870c26e

Browse files
committed
Add CommandBus implementation
1 parent 0504b77 commit 870c26e

File tree

5 files changed

+115
-0
lines changed

5 files changed

+115
-0
lines changed

src/SymfonyCommandBus.php

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

tests/SymfonyCommandBusTest.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\DependencyContracts\Util\Messaging\MessageBus\HandlingMessageFailedException;
8+
use Gember\MessageBusSymfony\SymfonyCommandBus;
9+
use Gember\MessageBusSymfony\Test\TestDoubles\TestCommand;
10+
use Gember\MessageBusSymfony\Test\TestDoubles\TestCommandThrowingException;
11+
use Gember\MessageBusSymfony\Test\TestDoubles\TestSymfonyCommandBus;
12+
use PHPUnit\Framework\Attributes\Test;
13+
use PHPUnit\Framework\TestCase;
14+
15+
/**
16+
* @internal
17+
*/
18+
final class SymfonyCommandBusTest extends TestCase
19+
{
20+
private SymfonyCommandBus $commandBus;
21+
private TestSymfonyCommandBus $symfonyCommandBus;
22+
23+
protected function setUp(): void
24+
{
25+
parent::setUp();
26+
27+
$this->commandBus = new SymfonyCommandBus(
28+
$this->symfonyCommandBus = new TestSymfonyCommandBus(),
29+
);
30+
}
31+
32+
#[Test]
33+
public function itShouldHandleCommand(): void
34+
{
35+
$this->commandBus->handle(new TestCommand());
36+
37+
self::assertTrue($this->symfonyCommandBus->isCalled);
38+
}
39+
40+
#[Test]
41+
public function itShouldThrowExceptionWhenHandlingCommandFailed(): void
42+
{
43+
self::expectException(HandlingMessageFailedException::class);
44+
45+
$this->commandBus->handle(new TestCommandThrowingException());
46+
}
47+
}

tests/TestDoubles/TestCommand.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 TestCommand {}
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 TestCommandThrowingException {}
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 TestSymfonyCommandBus 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 TestCommandThrowingException) {
21+
throw new class extends Exception implements ExceptionInterface {};
22+
}
23+
24+
return new Envelope($message);
25+
}
26+
}

0 commit comments

Comments
 (0)