Skip to content

Commit

Permalink
#14 setBoardDimensions() - return the current draft game from the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
janis-rullis committed Oct 18, 2020
1 parent 95c2686 commit bce6ecb
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 69 deletions.
2 changes: 1 addition & 1 deletion symfony5/src/Controller/GameController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class GameController extends AbstractController
public function setBoardDimensions(Request $request, GameCreatorService $gameCreatorService): JsonResponse
{
try {
$resp = $gameCreatorService->setBoardDimensions(json_decode($request->getContent(), true));
$resp = $gameCreatorService->setBoardDimensions(json_decode($request->getContent(), true))->toArray();

return $this->json($resp, Response::HTTP_OK);
} catch (\Exception $e) {
Expand Down
50 changes: 44 additions & 6 deletions symfony5/src/Entity/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ class Game
const ERROR_CAN_NOT_FIND = '#14 Can not find the game.';
const ERROR_CAN_NOT_FIND_CODE = 106;
// #12 Field names.
const ID = 'id';
const STATUS = 'status';
const WIDTH = 'width';
const HEIGHT = 'height';
const HEIGHT_WIDTH = 'height_width';
const MOVE_CNT_TO_WIN = 'move_cnt_to_win';
/**

/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
Expand Down Expand Up @@ -78,11 +79,10 @@ class Game
/**
* @ORM\Column(type="integer", nullable=true)
* @SWG\Property(property="move_cnt_to_win", type="integer", example=3)
* @Groups({"CREATE", "PUB", "ID_ERROR"})
*/
private ?int $moveCntToWin;
public function getId(): ?int
private ?int $moveCntToWin = null;

public function getId(): ?int
{
return $this->id;
}
Expand Down Expand Up @@ -211,4 +211,42 @@ public function getMaxDimension(): int
{
return $this->getHeight() >= $this->getWidth() ? $this->getHeight() : $this->getWidth();
}

/**
* #15 Convert the Entity to array in unified manner.
* Will give same result in different endpoints.
*
* @param array $fields
*/
public function toArray(?array $fields = [], $relations = []): array
{
$return = [];
// #15 Contains most popular fields. Add a field is necessary.
$return = $this->toArrayFill($fields);

return $return;
}

/**
* #15 Fill order's fields.
*/
private function toArrayFill(?array $fields = []): array
{
$return = [];
$allFields = [
self::ID => $this->getId(), self::STATUS => $this->getStatus(),
self::WIDTH => $this->getWidth(), self::HEIGHT => $this->getHeight(),
self::MOVE_CNT_TO_WIN => $this->getMoveCntToWin(),
];

if (empty($fields)) {
return $allFields;
}

foreach ($fields as $field) {
$return[$field] = isset($allFields[$field]) ? $allFields[$field] : null;
}

return $return;
}
}
9 changes: 5 additions & 4 deletions symfony5/src/Interfaces/IGameRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ public function setBoardDimensions(Game $item, int $width, int $height): Game;
public function setRules(Game $item, int $moveCntToWin): Game;

public function insertDraftIfNotExist(): Game;

public function getCurrentDraft(): ?Game;
public function mustFindCurrentDraft(): ?Game;

public function mustFindCurrentDraft(): ?Game;

public function save();

// public function markAsCompleted(Order $order): Order;
//
// public function mustFindUsersOrder(int $userId, int $orderId): Order;
Expand Down
17 changes: 9 additions & 8 deletions symfony5/src/Repository/GameRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public function setBoardDimensions(Game $item, int $width, int $height): Game
{
$item->setWidth($width);
$item->setHeight($height);
$this->save();

return $item;
}
Expand All @@ -50,14 +51,14 @@ public function setRules(Game $item, int $moveCntToWin): Game
*/
public function insertDraftIfNotExist(): Game
{
$item = $this->getCurrentDraft();
$item = $this->getCurrentDraft();

// #14 Create if it doesn't exist yet.
if (empty($item)) {
$item = new Game();
$item->setStatus(Game::DRAFT);
$this->em->persist($item);
$this->em->flush();
$this->em->persist($item);
$this->em->flush();
}

if (empty($item)) {
Expand Down Expand Up @@ -88,11 +89,11 @@ public function mustFindCurrentDraft(): ?Game
/*
* #14 Shorthand to write to the database.
*/
public function save()
{
$this->em->flush();
$this->em->clear();
}
public function save()
{
$this->em->flush();
$this->em->clear();
}

/*
* #12 Mark the game as completed.
Expand Down
26 changes: 13 additions & 13 deletions symfony5/tests/GameUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@
class GameUnitTest extends KernelTestCase
{
private $c;
private $gameRepo ;
private $gameRepo;

protected function setUp(): void
{
$kernel = self::bootKernel();
$kernel = self::bootKernel();
$this->c = $kernel->getContainer();
$this->gameRepo = $this->c->get('test.'.IGameRepo::class);
$this->gameRepo = $this->c->get('test.'.IGameRepo::class);
}

public function testValid()
{
$this->assertNull($this->gameRepo->getCurrentDraft());
$item = $this->gameRepo->insertDraftIfNotExist();
$this->assertGreaterThan(1, $item->getId());
$this->assertEquals(Game::DRAFT, $item->getStatus());
$this->assertEquals(3, $item->getWidth());
$this->assertEquals(3, $item->getHeight());
$item2 = $this->gameRepo->getCurrentDraft();
$this->assertEquals($item2->getId(), $item->getId());
$this->assertNull($this->gameRepo->getCurrentDraft());

$item = $this->gameRepo->insertDraftIfNotExist();
$this->assertGreaterThan(1, $item->getId());
$this->assertEquals(Game::DRAFT, $item->getStatus());
$this->assertEquals(3, $item->getWidth());
$this->assertEquals(3, $item->getHeight());

$item2 = $this->gameRepo->getCurrentDraft();
$this->assertEquals($item2->getId(), $item->getId());
}
}
18 changes: 0 additions & 18 deletions symfony5/tests/Rules/MovesToWinUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,6 @@ public function testNotInteger2()
$game->setMoveCntToWin(3.9);
}

public function testHeightNotSet()
{
$game = new Game();
$this->expectException('\Error');
$this->expectErrorMessage('Typed property '.Game::class.'::$height must not be accessed before initialization');
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}

public function testInvalidStatusSet()
{
$game = new Game();
Expand All @@ -73,16 +65,6 @@ public function testInvalidStatusSet()
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}

public function testWidthNotSet()
{
$game = new Game();
$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');
$game->setMoveCntToWin(Game::MIN_HEIGHT_WIDTH);
}

public function testTooSmall()
{
$game = new Game();
Expand Down
19 changes: 0 additions & 19 deletions symfony5/tests/WxH/GridUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,4 @@ public function testValidMinMaxDimension2()
$this->assertEquals($min, $game->getMinDimension());
$this->assertEquals($max, $game->getMaxDimension());
}

public function testHeightNotSet()
{
$game = new Game();
$game->setStatus(Game::DRAFT);
$this->expectException('\Error');
$this->expectErrorMessage('Typed property '.Game::class.'::$height must not be accessed before initialization');
$game->getMinDimension();
}

public function testWidthNotSet()
{
$game = new Game();
$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');
$game->getMaxDimension();
}
}

0 comments on commit bce6ecb

Please sign in to comment.