Skip to content

Commit 6a3a369

Browse files
committed
Move print board to Infrastructure layer
1 parent 33d5edf commit 6a3a369

6 files changed

Lines changed: 116 additions & 95 deletions

File tree

bin/game_status.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use TicTacToe\Application\Service\Game\GameStatusRequest;
66
use TicTacToe\Application\Service\Game\GameStatusService;
77
use TicTacToe\Infrastructure\Persistence\File\Game\FileGameRepository;
8+
use TicTacToe\Infrastructure\UI\Console\Command\PrintGameBoard;
89

910

1011
try {
@@ -17,12 +18,14 @@
1718

1819
$gameStatusResponse = $service->execute(new GameStatusRequest($gameId));
1920

20-
echo "\n" . $gameStatusResponse->boardString();
21+
echo "\n" . PrintGameBoard::print($gameStatusResponse);
2122

2223
echo "\nGame " . ($gameStatusResponse->isFinished() ? 'finished' : 'playing');
2324
if ($gameStatusResponse->isFinished()) {
24-
if ($gameStatusResponse->winnerId() != null) {
25-
echo "\nWinner is " . $gameStatusResponse->winnerName() . ' - ' . $gameStatusResponse->winnerId();
25+
if (!empty($gameStatusResponse->winner())) {
26+
echo "\nWinner is " . $gameStatusResponse->winner()['name']
27+
. ' - '
28+
. $gameStatusResponse->winner()['id'];
2629
} else {
2730
echo "\nDraw";
2831
}

bin/user_movement.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use TicTacToe\Application\Service\Game\UserMovementInGameRequest;
66
use TicTacToe\Application\Service\Game\UserMovementInGameService;
77
use TicTacToe\Infrastructure\Persistence\File\Game\FileGameRepository;
8+
use TicTacToe\Infrastructure\UI\Console\Command\PrintGameBoard;
89

910

1011
try {
@@ -24,12 +25,14 @@
2425
));
2526

2627
echo "\nMovement done!";
27-
echo "\n" . $gameStatusResponse->boardString();
28+
echo "\n" . PrintGameBoard::print($gameStatusResponse);
2829

2930
if ($gameStatusResponse->isFinished()) {
3031
echo "\nGame finished!";
31-
if ($gameStatusResponse->winnerId() != null) {
32-
echo "\nWinner is " . $gameStatusResponse->winnerName() . ' - ' . $gameStatusResponse->winnerId();
32+
if (!empty($gameStatusResponse->winner())) {
33+
echo "\nWinner is " . $gameStatusResponse->winner()['name']
34+
. ' - '
35+
. $gameStatusResponse->winner()['id'];
3336
} else {
3437
echo "\nDraw";
3538
}

src/TicTacToe/Application/Service/Game/GameStatusResponse.php

Lines changed: 26 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,84 +10,67 @@
1010
class GameStatusResponse
1111
{
1212
private string $gameId;
13-
private ?string $winnerId;
14-
private ?string $winnerName;
1513
private bool $isFinished;
1614
private array $board;
17-
private string $boardString;
15+
private array $firstUser;
16+
private array $secondUser;
17+
private array $winner;
1818

1919
public static function createFromGame(Game $game): self
2020
{
2121
$self = new self();
2222
$self->gameId = $game->id();
23-
$self->winnerId = null;
24-
$self->winnerName = null;
23+
$self->winner = [];
2524
$self->board = $game->board();
26-
$self->setBoardToString($game);
25+
$self->firstUser = [
26+
'id' => $game->firstUser()->id(),
27+
'name' => $game->firstUser()->name()
28+
];
29+
$self->secondUser = [
30+
'id' => $game->secondUser()->id(),
31+
'name' => $game->secondUser()->name()
32+
];
2733

2834
if ($game->winner() instanceof User) {
29-
$self->winnerId = $game->winner()->id();
30-
$self->winnerName = $game->winner()->name();
35+
$self->winner = [
36+
'id' => $game->winner()->id(),
37+
'name' => $game->winner()->name()
38+
];
3139
}
3240

3341
$self->isFinished = $game->isFinished();
3442

3543
return $self;
3644
}
3745

38-
public function winnerId(): ?string
39-
{
40-
return $this->winnerId;
41-
}
42-
4346
public function isFinished(): bool
4447
{
4548
return $this->isFinished;
4649
}
4750

48-
public function winnerName(): ?string
49-
{
50-
return $this->winnerName;
51-
}
52-
5351
public function gameId(): string
5452
{
5553
return $this->gameId;
5654
}
5755

58-
private function setBoardToString(Game $game): void
56+
public function board(): array
5957
{
60-
$this->boardString = '';
61-
$tempBoard = [];
62-
$parseIdToSymbol = [
63-
$game->firstUser()->id() => 'x',
64-
$game->secondUser()->id() => 'o',
65-
];
66-
foreach ($game->board() as $field => $value) {
67-
if (!is_null($value)) {
68-
$tempBoard[$field] = $parseIdToSymbol[$value];
69-
} else {
70-
$tempBoard[$field] = ' ';
71-
}
72-
73-
}
74-
75-
$this->boardString .= $tempBoard[1] . ' | ' . $tempBoard[2] . ' | ' . $tempBoard[3] . "\n";
76-
$this->boardString .= $tempBoard[4] . ' | ' . $tempBoard[5] . ' | ' . $tempBoard[6] . "\n";
77-
$this->boardString .= $tempBoard[7] . ' | ' . $tempBoard[8] . ' | ' . $tempBoard[9] . "\n";
58+
return $this->board;
59+
}
7860

79-
$this->boardString .= "\n" . $game->firstUser()->name() . ': x';
80-
$this->boardString .= "\n" . $game->secondUser()->name() . ': o';
61+
public function winner(): array
62+
{
63+
return $this->winner;
8164
}
8265

83-
public function board(): array
66+
public function firstUser(): array
8467
{
85-
return $this->board;
68+
return $this->firstUser;
8669
}
8770

88-
public function boardString(): string
71+
public function secondUser(): array
8972
{
90-
return $this->boardString;
73+
return $this->secondUser;
9174
}
9275

9376
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
4+
namespace TicTacToe\Infrastructure\UI\Console\Command;
5+
6+
7+
use TicTacToe\Application\Service\Game\GameStatusResponse;
8+
9+
class PrintGameBoard
10+
{
11+
public static function print(GameStatusResponse $gameStatusResponse): string
12+
{
13+
$boardString = '';
14+
$tempBoard = [];
15+
$parseIdToSymbol = [
16+
$gameStatusResponse->firstUser()['id'] => 'x',
17+
$gameStatusResponse->secondUser()['id'] => 'o',
18+
];
19+
foreach ($gameStatusResponse->board() as $field => $value) {
20+
if (!is_null($value)) {
21+
$tempBoard[$field] = $parseIdToSymbol[$value];
22+
} else {
23+
$tempBoard[$field] = ' ';
24+
}
25+
}
26+
27+
$boardString .= $tempBoard[1] . ' | ' . $tempBoard[2] . ' | ' . $tempBoard[3] . "\n";
28+
$boardString .= $tempBoard[4] . ' | ' . $tempBoard[5] . ' | ' . $tempBoard[6] . "\n";
29+
$boardString .= $tempBoard[7] . ' | ' . $tempBoard[8] . ' | ' . $tempBoard[9] . "\n";
30+
31+
$boardString .= "\n" . $gameStatusResponse->firstUser()['name'] . ': x';
32+
$boardString .= "\n" . $gameStatusResponse->secondUser()['name'] . ': o';
33+
34+
return $boardString;
35+
}
36+
}

tests/Unit/TicTacToe/Application/Service/Game/GameStatusResponseTest.php

Lines changed: 8 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,63 +29,25 @@ private function gameFinished(): Game
2929
public function testWhenGameStatusResponseIsCreatedFromAGameObjectItHasAllDataParsed()
3030
{
3131
$game = Game::start(
32-
User::create('user1'),
33-
User::create('user2')
32+
$user1 = User::create('user1'),
33+
$user2 = User::create('user2')
3434
);
3535

3636
$gameStatusResponse = GameStatusResponse::createFromGame($game);
3737

38-
$this->assertNull($gameStatusResponse->winnerId());
39-
$this->assertNull($gameStatusResponse->winnerName());
38+
$this->assertEmpty($gameStatusResponse->winner());
39+
$this->assertEquals(['id' => $user1->id(), 'name' => $user1->name()], $gameStatusResponse->firstUser());
40+
$this->assertEquals(['id' => $user2->id(), 'name' => $user2->name()], $gameStatusResponse->secondUser());
4041
$this->assertFalse($gameStatusResponse->isFinished());
4142
}
4243

4344
public function testWhenGameStatusResponseIsCreatedFromAFinishedGameObjectItHasAllDataParsed()
4445
{
4546
$gameStatusResponse = GameStatusResponse::createFromGame($this->gameFinished());
4647

47-
$this->assertIsString($gameStatusResponse->winnerId());
48-
$this->assertIsString($gameStatusResponse->winnerName());
48+
$this->assertNotEmpty($gameStatusResponse->winner());
49+
$this->assertArrayHasKey('id', $gameStatusResponse->winner());
50+
$this->assertArrayHasKey('name', $gameStatusResponse->winner());
4951
$this->assertTrue($gameStatusResponse->isFinished());
5052
}
51-
52-
public function testBoardStringIsWellFormed()
53-
{
54-
$user1 = User::create('user1');
55-
$user2 = User::create('user2');
56-
$game = Game::start($user1, $user2);
57-
58-
$expectedBoardString =
59-
" | | \n"
60-
. " | | \n"
61-
. " | | \n"
62-
. "\n" . $user1->name() . ': x'
63-
. "\n" . $user2->name() . ': o'
64-
;
65-
$gameStatusResponse = GameStatusResponse::createFromGame($game);
66-
$this->assertEquals($expectedBoardString, $gameStatusResponse->boardString());
67-
68-
$expectedBoardString =
69-
"x | | \n"
70-
. " | | \n"
71-
. " | | \n"
72-
. "\n" . $user1->name() . ': x'
73-
. "\n" . $user2->name() . ': o'
74-
;
75-
$game->userMoves($user1->id(), 1);
76-
$gameStatusResponse = GameStatusResponse::createFromGame($game);
77-
$this->assertEquals($expectedBoardString, $gameStatusResponse->boardString());
78-
79-
$expectedBoardString =
80-
"x | | \n"
81-
. " | o | \n"
82-
. " | | \n"
83-
. "\n" . $user1->name() . ': x'
84-
. "\n" . $user2->name() . ': o'
85-
;
86-
$game->userMoves($user2->id(), 5);
87-
$gameStatusResponse = GameStatusResponse::createFromGame($game);
88-
$this->assertEquals($expectedBoardString, $gameStatusResponse->boardString());
89-
90-
}
9153
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
namespace Tests\Unit\TicTacToe\Infrastructure\Console\Command;
5+
6+
7+
use PHPUnit\Framework\TestCase;
8+
use TicTacToe\Application\Service\Game\GameStatusResponse;
9+
use TicTacToe\Domain\Game\Game;
10+
use TicTacToe\Domain\User\User;
11+
use TicTacToe\Infrastructure\UI\Console\Command\PrintGameBoard;
12+
13+
class PrintGameBoardTest extends TestCase
14+
{
15+
public function testTransformBoardFromGameStatusResponseToPrintableString()
16+
{
17+
$user1 = User::create('user1');
18+
$user2 = User::create('user2');
19+
$game = Game::start($user1, $user2);
20+
21+
$gameStatusResponse = GameStatusResponse::createFromGame($game);
22+
23+
$gameBoardPrintedString = PrintGameBoard::print($gameStatusResponse);
24+
$expectedBoardString =
25+
" | | \n"
26+
. " | | \n"
27+
. " | | \n"
28+
. "\n" . $user1->name() . ': x'
29+
. "\n" . $user2->name() . ': o'
30+
;
31+
32+
$this->assertEquals($expectedBoardString, $gameBoardPrintedString);
33+
}
34+
}

0 commit comments

Comments
 (0)