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
22 changes: 11 additions & 11 deletions src/Middleware/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
use PhpCmd\CmdBus\CommandHandlerResolverInterface;
use PhpCmd\CmdBus\CommandInterface;
use PhpCmd\CmdBus\MiddlewareInterface;
use Throwable;

class CommandHandlerMiddleware implements MiddlewareInterface
/** @internal */
final readonly class CommandHandlerMiddleware implements MiddlewareInterface
{
public function __construct(
private readonly CommandHandlerResolverInterface $resolver
private CommandHandlerResolverInterface $resolver
) {
}

Expand All @@ -27,14 +27,14 @@ public function process(
): mixed {
// Resolve the command handler for the given command
$cmdHandler = $this->resolver->resolve($command);
try {
// run the command and capture results
$result = $cmdHandler->handle($command);
// create a new CommandResult with the captured results
$command = new CommandResult($command, CommandStatus::Success, $result);
} catch (Throwable $th) {
$command = new CommandResult($command, CommandStatus::Failure, $th);
}

// create a new CommandResult with the captured results
$command = new CommandResult(
$command,
CommandStatus::Success,
$cmdHandler->handle($command)
);

return $handler->handle($command);
}
}
30 changes: 0 additions & 30 deletions test/unit/Middleware/CommandHandlerMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use RuntimeException;

#[CoversClass(CommandHandlerMiddleware::class)]
final class CommandHandlerMiddlewareTest extends TestCase
Expand Down Expand Up @@ -86,35 +85,6 @@ public function testProcessResolvesCommandHandlerAndCallsIt(): void
$this->assertEquals('final result', $result);
}

public function testProcessWrapsExceptionInFailureCommandResult(): void
{
$exception = new RuntimeException('Test exception');

$this->resolver->expects($this->once())
->method('resolve')
->with($this->command)
->willReturn($this->commandHandler);

$this->commandHandler->expects($this->once())
->method('handle')
->with($this->command)
->willThrowException($exception);

$this->handler->expects($this->once())
->method('handle')
->with($this->callback(function ($commandResult) use ($exception) {
return $commandResult instanceof CommandResult
&& $commandResult->getCommand() === $this->command
&& $commandResult->getStatus() === CommandStatus::Failure
&& $commandResult->getResult() === $exception;
}))
->willReturn('error handled');

$result = $this->middleware->process($this->command, $this->handler);

$this->assertEquals('error handled', $result);
}

public function testProcessCallsResolverWithCorrectCommand(): void
{
$this->resolver->expects($this->once())
Expand Down