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

declare(strict_types=1);

namespace Chess\Application\Command\StartGame;

final class StartGameCommand
{
}
27 changes: 27 additions & 0 deletions src/Chess/Application/Command/StartGame/StartGameHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Chess\Application\Command\StartGame;

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

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

public function __invoke(StartGameCommand $command): GameId
{
$game = Game::start();
$this->repository->save($game);

return $game->id();
}
}
15 changes: 15 additions & 0 deletions src/Chess/Domain/Repository/GameRepositoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Chess\Domain\Repository;

use Chess\Domain\Model\Game;
use Chess\Domain\Value\GameId;

interface GameRepositoryInterface
{
public function save(Game $game): void;

public function findById(GameId $id): ?Game;
}
39 changes: 39 additions & 0 deletions tests/Chess/Application/Command/StartGame/StartGameHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Chess\Tests\Application\Command\StartGame;

use Chess\Application\Command\StartGame\StartGameCommand;
use Chess\Application\Command\StartGame\StartGameHandler;
use Chess\Domain\Model\Game;
use Chess\Domain\Repository\GameRepositoryInterface;
use Chess\Domain\Value\GameId;
use PHPUnit\Framework\TestCase;

final class StartGameHandlerTest extends TestCase
{
public function testItCreatesAndSavesAGame(): void
{
$repository = new class implements GameRepositoryInterface {
public ?Game $savedGame = null;

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

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

$handler = new StartGameHandler($repository);
$gameId = $handler(new StartGameCommand());

self::assertInstanceOf(GameId::class, $gameId);
self::assertNotNull($repository->savedGame);
self::assertSame($gameId->toString(), $repository->savedGame->id()->toString());
}
}
Loading