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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ php bin/phpunit

- [x] Phase 0 — Project setup (Symfony skeleton, CI, PHPStan, CS Fixer)
- [x] Phase 1 — Domain modeling (Value Objects, Entities, Aggregate Root)
- [ ] Phase 2 — Application layer (CQRS, Symfony Messenger)
- [x] Phase 2 — Application layer (CQRS, Symfony Messenger)
- [ ] Phase 3 — Infrastructure (Doctrine, REST API)
- [ ] Phase 4 — PHPStan level 8, OpenAPI docs
19 changes: 19 additions & 0 deletions src/Chess/Application/Query/GetGameState/GameStateDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Query\GetGameState;

final class GameStateDTO
{
/**
* @param array<string, array{type: string, color: string}> $pieces
*/
public function __construct(
public readonly string $gameId,
public readonly string $currentTurn,
public readonly bool $isOver,
public readonly array $pieces,
) {
}
}
49 changes: 49 additions & 0 deletions src/Chess/Application/Query/GetGameState/GetGameStateHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Query\GetGameState;

use Chess\Domain\Repository\GameRepositoryInterface;
use Chess\Domain\Value\GameId;
use Chess\Domain\Value\Square;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler(bus: 'query.bus')]
final class GetGameStateHandler
{
public function __construct(
private readonly GameRepositoryInterface $repository,
) {
}

public function __invoke(GetGameStateQuery $query): GameStateDTO
{
$game = $this->repository->findById(GameId::fromString($query->gameId));

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

$pieces = [];
foreach (['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] as $file) {
foreach (range(1, 8) as $rank) {
$square = new Square($file, $rank);
$piece = $game->board()->pieceAt($square);
if (null !== $piece) {
$pieces[$square->toString()] = [
'type' => $piece->type()->name,
'color' => $piece->color()->name,
];
}
}
}

return new GameStateDTO(
gameId: $game->id()->toString(),
currentTurn: $game->currentTurn()->name,
isOver: $game->isOver(),
pieces: $pieces,
);
}
}
13 changes: 13 additions & 0 deletions src/Chess/Application/Query/GetGameState/GetGameStateQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Query\GetGameState;

final class GetGameStateQuery
{
public function __construct(
public readonly string $gameId,
) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

declare(strict_types=1);

namespace Chess\Tests\Application\Query\GetGameState;

use Chess\Application\Query\GetGameState\GameStateDTO;
use Chess\Application\Query\GetGameState\GetGameStateHandler;
use Chess\Application\Query\GetGameState\GetGameStateQuery;
use Chess\Domain\Model\Game;
use Chess\Domain\Repository\GameRepositoryInterface;
use Chess\Domain\Value\GameId;
use PHPUnit\Framework\TestCase;

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

$repository = new class($game) implements GameRepositoryInterface {
public function __construct(private readonly Game $game)
{
}

public function save(Game $game): void
{
}

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

$handler = new GetGameStateHandler($repository);
$dto = $handler(new GetGameStateQuery($game->id()->toString()));

self::assertInstanceOf(GameStateDTO::class, $dto);
self::assertSame($game->id()->toString(), $dto->gameId);
self::assertSame('White', $dto->currentTurn);
self::assertFalse($dto->isOver);
self::assertCount(32, $dto->pieces);
}

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 GetGameStateHandler($repository);
$handler(new GetGameStateQuery('unknown-id'));
}

public function testPiecesContainCorrectDataForWhiteKing(): void
{
$game = Game::start();

$repository = new class($game) implements GameRepositoryInterface {
public function __construct(private readonly Game $game)
{
}

public function save(Game $game): void
{
}

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

$handler = new GetGameStateHandler($repository);
$dto = $handler(new GetGameStateQuery($game->id()->toString()));

self::assertArrayHasKey('e1', $dto->pieces);
self::assertSame('King', $dto->pieces['e1']['type']);
self::assertSame('White', $dto->pieces['e1']['color']);
}
}
Loading