From 4f801381dcc1f873475b3d57817b4830c2274360 Mon Sep 17 00:00:00 2001 From: florentdevk Date: Wed, 1 Jul 2026 21:13:23 +0200 Subject: [PATCH] feat(application): add PlayMove command and handler --- .../Command/PlayMove/PlayMoveCommand.php | 15 +++++ .../Command/PlayMove/PlayMoveHandler.php | 33 ++++++++++ .../PlayMove/PlayMoveHandlerTest.php | 65 +++++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 src/Chess/Application/Command/PlayMove/PlayMoveCommand.php create mode 100644 src/Chess/Application/Command/PlayMove/PlayMoveHandler.php create mode 100644 tests/Chess/Application/PlayMove/PlayMoveHandlerTest.php diff --git a/src/Chess/Application/Command/PlayMove/PlayMoveCommand.php b/src/Chess/Application/Command/PlayMove/PlayMoveCommand.php new file mode 100644 index 0000000..8a00241 --- /dev/null +++ b/src/Chess/Application/Command/PlayMove/PlayMoveCommand.php @@ -0,0 +1,15 @@ +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); + } +} diff --git a/tests/Chess/Application/PlayMove/PlayMoveHandlerTest.php b/tests/Chess/Application/PlayMove/PlayMoveHandlerTest.php new file mode 100644 index 0000000..b745aa5 --- /dev/null +++ b/tests/Chess/Application/PlayMove/PlayMoveHandlerTest.php @@ -0,0 +1,65 @@ +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')); + } +}