diff --git a/symfony5/src/Controller/GameController.php b/symfony5/src/Controller/GameController.php index 07b341f..12ae4db 100644 --- a/symfony5/src/Controller/GameController.php +++ b/symfony5/src/Controller/GameController.php @@ -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) { diff --git a/symfony5/src/Entity/Game.php b/symfony5/src/Entity/Game.php index 7a180e2..493e8ce 100644 --- a/symfony5/src/Entity/Game.php +++ b/symfony5/src/Entity/Game.php @@ -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") @@ -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; } @@ -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; + } } diff --git a/symfony5/src/Interfaces/IGameRepo.php b/symfony5/src/Interfaces/IGameRepo.php index c244ecd..b5db6cb 100644 --- a/symfony5/src/Interfaces/IGameRepo.php +++ b/symfony5/src/Interfaces/IGameRepo.php @@ -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; diff --git a/symfony5/src/Repository/GameRepository.php b/symfony5/src/Repository/GameRepository.php index 4433774..6254793 100644 --- a/symfony5/src/Repository/GameRepository.php +++ b/symfony5/src/Repository/GameRepository.php @@ -29,6 +29,7 @@ public function setBoardDimensions(Game $item, int $width, int $height): Game { $item->setWidth($width); $item->setHeight($height); + $this->save(); return $item; } @@ -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)) { @@ -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. diff --git a/symfony5/tests/GameUnitTest.php b/symfony5/tests/GameUnitTest.php index ad3bbb6..152f00d 100644 --- a/symfony5/tests/GameUnitTest.php +++ b/symfony5/tests/GameUnitTest.php @@ -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()); } } diff --git a/symfony5/tests/Rules/MovesToWinUnitTest.php b/symfony5/tests/Rules/MovesToWinUnitTest.php index 8732b72..daf985b 100644 --- a/symfony5/tests/Rules/MovesToWinUnitTest.php +++ b/symfony5/tests/Rules/MovesToWinUnitTest.php @@ -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(); @@ -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(); diff --git a/symfony5/tests/WxH/GridUnitTest.php b/symfony5/tests/WxH/GridUnitTest.php index 289bc5d..11afb7e 100644 --- a/symfony5/tests/WxH/GridUnitTest.php +++ b/symfony5/tests/WxH/GridUnitTest.php @@ -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(); - } }