Skip to content

Commit

Permalink
[WIP] #14 Repo/Game, tests: Allow to set board values only if the sta…
Browse files Browse the repository at this point in the history
…tus is 'draft'
  • Loading branch information
janis-rullis committed Oct 18, 2020
1 parent 03c741a commit 2edde3e
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 49 deletions.
2 changes: 1 addition & 1 deletion symfony5/src/Interfaces/IGameRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

interface IGameRepo
{
public function setBoardDimensions(int $width, int $height): Game;
public function setBoardDimensions(Game $item, int $width, int $height): Game;

public function setRules(Game $item, int $moveCntToWin): Game;

Expand Down
3 changes: 1 addition & 2 deletions symfony5/src/Repository/GameRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function __construct(EntityManagerInterface $em)
* #12 Set game board dimensions but don't store it yet.
* Validations happen in those Entity methods.
*/
public function setBoardDimensions(int $width, int $height): Game
public function setBoardDimensions(Game $item, int $width, int $height): Game
{
$item->setWidth($width);
$item->setHeight($height);
Expand All @@ -40,7 +40,6 @@ public function setBoardDimensions(int $width, int $height): Game
*/
public function setRules(Game $item, int $moveCntToWin): Game
{
// #15 Collect game first.
$item->setMoveCntToWin($moveCntToWin);

return $item;
Expand Down
4 changes: 2 additions & 2 deletions symfony5/src/Service/GameCreatorService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function setBoardDimensions(?array $request): Game
throw new \App\Exception\GameValidatorException([Game::HEIGHT => Game::ERROR_HEIGHT_WIDTH_INVALID], Game::ERROR_HEIGHT_WIDTH_INVALID_CODE);
}
try {
$game = $this->orderRepo->insertDraftIfNotExist();
$game = $this->gameRepo->insertDraftIfNotExist();

return $this->gameRepo->setBoardDimensions($game, $request[Game::WIDTH], $request[Game::HEIGHT]);
} catch (\Error $ex) {
Expand All @@ -57,7 +57,7 @@ public function setRules(?array $request): Game
throw new \App\Exception\GameValidatorException([Game::MOVE_CNT_TO_WIN => Game::ERROR_MOVE_CNT_TO_WIN_INVALID], Game::ERROR_MOVE_CNT_TO_WIN_INVALID_CODE);
}
try {
$game = $this->orderRepo->mustFindCurrentDraft();
$game = $this->gameRepo->mustFindCurrentDraft();

return $this->gameRepo->setRules($game, $request[Game::MOVE_CNT_TO_WIN]);
} catch (\Error $ex) {
Expand Down
31 changes: 11 additions & 20 deletions symfony5/tests/Rules/MovesToWinUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function setUp(): void
public function testValid()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MAX_HEIGHT_WIDTH);
$game->setMoveCntToWin(Game::MAX_HEIGHT_WIDTH);
Expand All @@ -31,7 +31,7 @@ public function testValid()
public function testValid2()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MAX_HEIGHT_WIDTH);
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
Expand Down Expand Up @@ -61,30 +61,22 @@ public function testHeightNotSet()
$this->expectErrorMessage('Typed property '.Game::class.'::$height must not be accessed before initialization');
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}

public function testStatusNotSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}


public function testInvalidStatusSet()

public function testInvalidStatusSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setStatus(Game::ONGOING);
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
$game->setStatus(Game::ONGOING);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}


public function testWidthNotSet()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$this->expectException('\Error');
$this->expectErrorMessage('Typed property '.Game::class.'::$width must not be accessed before initialization');
Expand All @@ -94,7 +86,7 @@ public function testWidthNotSet()
public function testTooSmall()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
$this->expectException(GameValidatorException::class);
Expand All @@ -105,12 +97,11 @@ public function testTooSmall()
public function testTooBig()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MAX_HEIGHT_WIDTH);
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_MOVE_CNT_TO_WIN_INVALID_CODE, Game::ERROR_MOVE_CNT_TO_WIN_INVALID);
$game->setMoveCntToWin(Game::MAX_HEIGHT_WIDTH + 1);
}

}
20 changes: 10 additions & 10 deletions symfony5/tests/WxH/GridHeightUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ public function testValidHeight()
{
$width = 3;
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight($width);
$this->assertEquals($width, $game->getHeight());
}

public function testStatusNotSet()
public function testStatusNotSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
}
public function testInvalidStatusSet()

public function testInvalidStatusSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setStatus(Game::ONGOING);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$game->setStatus(Game::ONGOING);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
}

public function testNotInteger()
{
$game = new Game();
Expand All @@ -72,7 +72,7 @@ public function testNotInteger2()
public function testTooSmall()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);

$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_HEIGHT_WIDTH_INVALID_CODE, Game::ERROR_HEIGHT_WIDTH_INVALID);
Expand All @@ -82,7 +82,7 @@ public function testTooSmall()
public function testTooBig()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);

$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_HEIGHT_WIDTH_INVALID_CODE, Game::ERROR_HEIGHT_WIDTH_INVALID);
Expand Down
8 changes: 4 additions & 4 deletions symfony5/tests/WxH/GridUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function testValidMinMaxDimension()
$min = 3;
$max = 5;
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight($min);
$game->setWidth($max);
$this->assertEquals($min, $game->getMinDimension());
Expand All @@ -34,7 +34,7 @@ public function testValidMinMaxDimension2()
$min = 3;
$max = 5;
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setWidth($max);
$game->setHeight($min);
$this->assertEquals($min, $game->getMinDimension());
Expand All @@ -44,7 +44,7 @@ public function testValidMinMaxDimension2()
public function testHeightNotSet()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$this->expectException('\Error');
$this->expectErrorMessage('Typed property '.Game::class.'::$height must not be accessed before initialization');
$game->getMinDimension();
Expand All @@ -53,7 +53,7 @@ public function testHeightNotSet()
public function testWidthNotSet()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setHeight(Game::MIN_HEIGHT_WIDTH);
$this->expectException('\Error');
$this->expectErrorMessage('Typed property '.Game::class.'::$width must not be accessed before initialization');
Expand Down
1 change: 1 addition & 0 deletions symfony5/tests/WxH/GridWidthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function testWidthValid()
{
$data = [Game::WIDTH => Game::MAX_HEIGHT_WIDTH, Game::HEIGHT => Game::MIN_HEIGHT_WIDTH];
$this->client->request('POST', $this->uri, [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($data));
// print_r($this->client->getResponse());exit;
$this->assertEquals(Response::HTTP_OK, $this->client->getResponse()->getStatusCode());
$responseBody = json_decode($this->client->getResponse()->getContent(), true);
$this->assertEquals($data[Game::WIDTH], $responseBody[Game::WIDTH]);
Expand Down
20 changes: 10 additions & 10 deletions symfony5/tests/WxH/GridWidthUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,26 @@ public function testValidWidth()
{
$width = 3;
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);
$game->setWidth($width);
$this->assertEquals($width, $game->getWidth());
}
public function testStatusNotSet()

public function testStatusNotSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
}
public function testInvalidStatusSet()

public function testInvalidStatusSet()
{
$game = new Game();
$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_ONLY_FOR_DRAFT_CODE, Game::ERROR_ONLY_FOR_DRAFT);
$game->setStatus(Game::ONGOING);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
$game->setStatus(Game::ONGOING);
$game->setWidth(Game::MIN_HEIGHT_WIDTH);
}

public function testNotInteger()
Expand All @@ -72,7 +72,7 @@ public function testNotInteger2()
public function testTooSmall()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);

$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_HEIGHT_WIDTH_INVALID_CODE, Game::ERROR_HEIGHT_WIDTH_INVALID);
Expand All @@ -82,7 +82,7 @@ public function testTooSmall()
public function testTooBig()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$game->setStatus(Game::DRAFT);

$this->expectException(GameValidatorException::class);
$this->expectExceptionCode(Game::ERROR_HEIGHT_WIDTH_INVALID_CODE, Game::ERROR_HEIGHT_WIDTH_INVALID);
Expand Down

0 comments on commit 2edde3e

Please sign in to comment.