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
15 changes: 15 additions & 0 deletions src/Chess/Application/Command/PlayMove/PlayMoveCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Command\PlayMove;

final class PlayMoveCommand
{
public function __construct(
public readonly string $gameId,
public readonly string $from,
public readonly string $to,
) {
}
}
33 changes: 33 additions & 0 deletions src/Chess/Application/Command/PlayMove/PlayMoveHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Command\PlayMove;

use Chess\Domain\Repository\GameRepositoryInterface;
use Chess\Domain\Service\MoveValidator;
use Chess\Domain\Value\GameId;
use Chess\Domain\Value\Move;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'command.bus')]
final class PlayMoveHandler
{
public function __construct(
private readonly GameRepositoryInterface $repository,
private readonly MoveValidator $validator,
) {
}

public function __invoke(PlayMoveCommand $command): void
{
$game = $this->repository->findById(GameId::fromString($command->gameId));

if (null === $game) {
throw new \DomainException(\sprintf('Game "%s" not found.', $command->gameId));
}

$game->play(Move::fromString($command->from, $command->to), $this->validator);
$this->repository->save($game);
}
}
65 changes: 65 additions & 0 deletions tests/Chess/Application/PlayMove/PlayMoveHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace Chess\Tests\Application\Command\PlayMove;

use Chess\Application\Command\PlayMove\PlayMoveCommand;
use Chess\Application\Command\PlayMove\PlayMoveHandler;
use Chess\Domain\Model\Game;
use Chess\Domain\Repository\GameRepositoryInterface;
use Chess\Domain\Service\MoveValidator;
use Chess\Domain\Value\Color;
use Chess\Domain\Value\GameId;
use PHPUnit\Framework\TestCase;

final class PlayMoveHandlerTest extends TestCase
{
public function testItPlaysAMoveOnAnExistingGame(): void
{
$game = Game::start();

$repository = new class($game) implements GameRepositoryInterface {
public ?Game $savedGame = null;

public function __construct(private readonly Game $game)
{
}

public function save(Game $game): void
{
$this->savedGame = $game;
}

public function findById(GameId $id): Game
{
return $this->game;
}
};

$handler = new PlayMoveHandler($repository, new MoveValidator());
$handler(new PlayMoveCommand($game->id()->toString(), 'e2', 'e4'));

self::assertNotNull($repository->savedGame);
self::assertSame(Color::Black, $repository->savedGame->currentTurn());
}

public function testItThrowsWhenGameNotFound(): void
{
$repository = new class implements GameRepositoryInterface {
public function save(Game $game): void
{
}

public function findById(GameId $id): ?Game
{
return null;
}
};

$this->expectException(\DomainException::class);

$handler = new PlayMoveHandler($repository, new MoveValidator());
$handler(new PlayMoveCommand('unknown-id', 'e2', 'e4'));
}
}
Loading