Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^8.3",
"gember/dependency-contracts": "^0.1",
"gember/dependency-contracts": "^0.2",
"symfony/messenger": "^7.1"
},
"require-dev": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/SymfonyCommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Gember\MessageBusSymfony;

use Gember\DependencyContracts\Util\Messaging\MessageBus\CommandBus;
use Gember\DependencyContracts\Util\Messaging\MessageBus\HandlingMessageFailedException;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Override;

final readonly class SymfonyCommandBus implements CommandBus
{
public function __construct(
private MessageBusInterface $commandBus,
) {}

#[Override]
public function handle(object $command): void
{
try {
$this->commandBus->dispatch($command);
} catch (ExceptionInterface $exception) {
throw HandlingMessageFailedException::withException($exception);
}
}
}
47 changes: 47 additions & 0 deletions tests/SymfonyCommandBusTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Gember\MessageBusSymfony\Test;

use Gember\DependencyContracts\Util\Messaging\MessageBus\HandlingMessageFailedException;
use Gember\MessageBusSymfony\SymfonyCommandBus;
use Gember\MessageBusSymfony\Test\TestDoubles\TestCommand;
use Gember\MessageBusSymfony\Test\TestDoubles\TestCommandThrowingException;
use Gember\MessageBusSymfony\Test\TestDoubles\TestSymfonyCommandBus;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

/**
* @internal
*/
final class SymfonyCommandBusTest extends TestCase
{
private SymfonyCommandBus $commandBus;
private TestSymfonyCommandBus $symfonyCommandBus;

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

$this->commandBus = new SymfonyCommandBus(
$this->symfonyCommandBus = new TestSymfonyCommandBus(),
);
}

#[Test]
public function itShouldHandleCommand(): void
{
$this->commandBus->handle(new TestCommand());

self::assertTrue($this->symfonyCommandBus->isCalled);
}

#[Test]
public function itShouldThrowExceptionWhenHandlingCommandFailed(): void
{
self::expectException(HandlingMessageFailedException::class);

$this->commandBus->handle(new TestCommandThrowingException());
}
}
7 changes: 7 additions & 0 deletions tests/TestDoubles/TestCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Gember\MessageBusSymfony\Test\TestDoubles;

final readonly class TestCommand {}
7 changes: 7 additions & 0 deletions tests/TestDoubles/TestCommandThrowingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

namespace Gember\MessageBusSymfony\Test\TestDoubles;

final readonly class TestCommandThrowingException {}
26 changes: 26 additions & 0 deletions tests/TestDoubles/TestSymfonyCommandBus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Gember\MessageBusSymfony\Test\TestDoubles;

use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Exception;

final class TestSymfonyCommandBus implements MessageBusInterface
{
public bool $isCalled;

public function dispatch(object $message, array $stamps = []): Envelope
{
$this->isCalled = true;

if ($message instanceof TestCommandThrowingException) {
throw new class extends Exception implements ExceptionInterface {};
}

return new Envelope($message);
}
}