-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Deal with shitty php enum and related tools * Refactor RestStartGame
- Loading branch information
Showing
12 changed files
with
196 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace QVGDS\Game\Domain; | ||
|
||
use QVGDS\Session\Domain\SessionId; | ||
|
||
final readonly class GameStart | ||
{ | ||
|
||
public function __construct(public GameId $gameId, public SessionId $sessionId, public string $player) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
qvgds-back/tests/App/Http/Controllers/DTO/RestGameTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\DTO; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use QVGDS\Tests\Game\GameFixtures; | ||
use QVGDS\Tests\Json\JsonHelper; | ||
|
||
class RestGameTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldSerializeToJson(): void | ||
{ | ||
$json = JsonHelper::writeAsJson(RestGame::from(GameFixtures::newGame())); | ||
|
||
self::assertEquals($this->json(), $json); | ||
} | ||
|
||
private function json(): string | ||
{ | ||
return | ||
'{"id":"7cb03186-5430-45c2-9bdb-d5993a4ad209",' | ||
. '"player":"Toto",' | ||
. '"step":1,' | ||
. '"shitcoins":0,' | ||
. '"status":"IN_PROGRESS",' | ||
. '"jokers":[' | ||
. '{"type":"FIFTY_FIFTY","status":"AVAILABLE"},' | ||
. '{"type":"CALL_A_FRIEND","status":"AVAILABLE"},' | ||
. '{"type":"AUDIENCE_HELP","status":"AVAILABLE"}]' | ||
. '}'; | ||
} | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
qvgds-back/tests/App/Http/Controllers/DTO/RestStartGameTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
use App\Http\Controllers\DTO\RestStartGame; | ||
use PHPUnit\Framework\TestCase; | ||
use QVGDS\Game\Domain\GameId; | ||
use QVGDS\Session\Domain\SessionId; | ||
use QVGDS\Tests\Json\JsonHelper; | ||
|
||
class RestStartGameTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function shouldDeserializeFromJson(): void | ||
{ | ||
/** @var RestStartGame $restStartGame */ | ||
$restStartGame = JsonHelper::readFromJson($this->json(), RestStartGame::class); | ||
|
||
$gameStart = $restStartGame->toDomain(); | ||
|
||
self::assertEquals($gameStart->player, "toto"); | ||
self::assertEquals($gameStart->sessionId, SessionId::from("b72ff86d-20fc-47f6-bbce-cd931a1a925d")); | ||
self::assertInstanceOf(GameId::class, $gameStart->gameId); | ||
} | ||
|
||
private function json(): string | ||
{ | ||
return <<<'JSON' | ||
{ | ||
"player": "toto", | ||
"session": "b72ff86d-20fc-47f6-bbce-cd931a1a925d" | ||
} | ||
JSON; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace QVGDS\Tests\Json; | ||
|
||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer; | ||
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
use Symfony\Component\Serializer\SerializerInterface; | ||
|
||
final class JsonHelper | ||
{ | ||
public static function writeAsJson(object $from): string | ||
{ | ||
return self::serializer()->serialize($from, "json"); | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* @param class-string<T> $className | ||
* @return T | ||
*/ | ||
public static function readFromJson(string $payload, string $className): object | ||
{ | ||
return self::serializer()->deserialize($payload, $className, "json"); | ||
} | ||
|
||
private static function serializer(): SerializerInterface | ||
{ | ||
$encoders = [new JsonEncoder()]; | ||
$normalizers = [new GetSetMethodNormalizer(), new PropertyNormalizer()]; | ||
|
||
return new Serializer($normalizers, $encoders); | ||
} | ||
} |