From 31f6ccf6dc26be1b060afcec036f90c8d7f86a1c Mon Sep 17 00:00:00 2001 From: Yukihiro Arisawa Date: Mon, 29 Apr 2024 22:28:32 +0900 Subject: [PATCH] feat: Random medaka generate --- config/psysh/config.php | 6 +++- src/Domain/Logic/NicknameGenerator.php | 27 +++++++++++++++++ src/Domain/Logic/RandomMedakaGenerator.php | 28 ++++++++++++++++++ src/Domain/Model/Fish/FishInterface.php | 7 +++++ src/UseCase/AddMedakaUseCase.php | 9 +++--- tests/Domain/Logic/NicknameGeneratorTest.php | 25 ++++++++++++++++ .../Logic/RandomMedakaGeneratorTest.php | 29 +++++++++++++++++++ tests/UseCase/AddMedakaUseCaseTest.php | 12 +++++++- 8 files changed, 136 insertions(+), 7 deletions(-) create mode 100644 src/Domain/Logic/NicknameGenerator.php create mode 100644 src/Domain/Logic/RandomMedakaGenerator.php create mode 100644 tests/Domain/Logic/NicknameGeneratorTest.php create mode 100644 tests/Domain/Logic/RandomMedakaGeneratorTest.php diff --git a/config/psysh/config.php b/config/psysh/config.php index 572e91a..3b0cf57 100644 --- a/config/psysh/config.php +++ b/config/psysh/config.php @@ -2,6 +2,8 @@ declare(strict_types=1); +use Iamyukihiro\Aquarium\Domain\Logic\NicknameGenerator; +use Iamyukihiro\Aquarium\Domain\Logic\RandomMedakaGenerator; use Iamyukihiro\Aquarium\Domain\Model\Tank\TankManager; use Iamyukihiro\Aquarium\Service\PsySH\AddMedakaCommand; use Iamyukihiro\Aquarium\Service\PsySH\InitTankCommand; @@ -10,8 +12,10 @@ $tankPATH = dirname(__FILE__).'/../../.memory/tank.memory'; +$nicknameGenerator = new NicknameGenerator(); +$randomMedakaGenerator = new RandomMedakaGenerator($nicknameGenerator); $tankManager = new TankManager($tankPATH); -$addMedakaUseCase = new AddMedakaUseCase($tankManager); +$addMedakaUseCase = new AddMedakaUseCase($tankManager, $randomMedakaGenerator); return [ 'commands' => [ diff --git a/src/Domain/Logic/NicknameGenerator.php b/src/Domain/Logic/NicknameGenerator.php new file mode 100644 index 0000000..3134bff --- /dev/null +++ b/src/Domain/Logic/NicknameGenerator.php @@ -0,0 +1,27 @@ +fishNicknames); + + return $this->fishNicknames[$randomIndex]; + } +} diff --git a/src/Domain/Logic/RandomMedakaGenerator.php b/src/Domain/Logic/RandomMedakaGenerator.php new file mode 100644 index 0000000..63cc648 --- /dev/null +++ b/src/Domain/Logic/RandomMedakaGenerator.php @@ -0,0 +1,28 @@ +nicknameGenerator->generate() . 'メダカ', + new Variety('みゆき'), + 'Swim' + ); + } +} diff --git a/src/Domain/Model/Fish/FishInterface.php b/src/Domain/Model/Fish/FishInterface.php index d077e3f..b087531 100644 --- a/src/Domain/Model/Fish/FishInterface.php +++ b/src/Domain/Model/Fish/FishInterface.php @@ -4,6 +4,13 @@ namespace Iamyukihiro\Aquarium\Domain\Model\Fish; +use Iamyukihiro\Aquarium\Domain\VO\Variety; + interface FishInterface { + public function getNickName(): string; + + public function getVariety(): Variety; + + public function getAct(): string; } diff --git a/src/UseCase/AddMedakaUseCase.php b/src/UseCase/AddMedakaUseCase.php index af36678..2b5b7d9 100644 --- a/src/UseCase/AddMedakaUseCase.php +++ b/src/UseCase/AddMedakaUseCase.php @@ -4,22 +4,21 @@ namespace Iamyukihiro\Aquarium\UseCase; -use Iamyukihiro\Aquarium\Domain\Model\Fish\Medaka; +use Iamyukihiro\Aquarium\Domain\Logic\RandomMedakaGenerator; use Iamyukihiro\Aquarium\Domain\Model\Tank\TankManager; -use Iamyukihiro\Aquarium\Domain\VO\Variety; class AddMedakaUseCase { public function __construct( - private TankManager $tankManager + private TankManager $tankManager, + private RandomMedakaGenerator $randomMedakaGenerator, ) { } public function add(): void { $tank = $this->tankManager->load(); - $tank->addFish(new Medaka('メダカ', new Variety('みゆき'), 'Swim')); + $tank->addFish($this->randomMedakaGenerator->generate()); $this->tankManager->save($tank); - clearstatcache(); } } diff --git a/tests/Domain/Logic/NicknameGeneratorTest.php b/tests/Domain/Logic/NicknameGeneratorTest.php new file mode 100644 index 0000000..81d8df9 --- /dev/null +++ b/tests/Domain/Logic/NicknameGeneratorTest.php @@ -0,0 +1,25 @@ +getSUT(); + $actual = $SUT->generate(); + + $this->assertIsString($actual); + } + + public function getSUT(): NicknameGenerator + { + return new NicknameGenerator(); + } +} diff --git a/tests/Domain/Logic/RandomMedakaGeneratorTest.php b/tests/Domain/Logic/RandomMedakaGeneratorTest.php new file mode 100644 index 0000000..ff2762d --- /dev/null +++ b/tests/Domain/Logic/RandomMedakaGeneratorTest.php @@ -0,0 +1,29 @@ +getSUT(); + $actual = $SUT->generate(); + + $this->assertInstanceOf(Medaka::class, $actual); + $this->assertIsString($actual->getNickName()); + } + + public function getSUT(): RandomMedakaGenerator + { + return new RandomMedakaGenerator( + new NicknameGenerator() + ); + } +} diff --git a/tests/UseCase/AddMedakaUseCaseTest.php b/tests/UseCase/AddMedakaUseCaseTest.php index 7804ab5..1e84643 100644 --- a/tests/UseCase/AddMedakaUseCaseTest.php +++ b/tests/UseCase/AddMedakaUseCaseTest.php @@ -6,8 +6,11 @@ namespace Iamyukihiro\Aquarium\UseCase; +use Iamyukihiro\Aquarium\Domain\Logic\RandomMedakaGenerator; +use Iamyukihiro\Aquarium\Domain\Model\Fish\Medaka; use Iamyukihiro\Aquarium\Domain\Model\Tank\Tank; use Iamyukihiro\Aquarium\Domain\Model\Tank\TankManager; +use Iamyukihiro\Aquarium\Domain\VO\Variety; use PHPUnit\Framework\TestCase; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; @@ -18,20 +21,26 @@ class AddMedakaUseCaseTest extends TestCase use ProphecyTrait; private ObjectProphecy $tankManagerP; + private ObjectProphecy $randomMedakaGeneratorP; protected function setUp(): void { $this->tankManagerP = $this->prophesize(TankManager::class); + $this->randomMedakaGeneratorP = $this->prophesize(RandomMedakaGenerator::class); } public function test(): void { + $medaka = new Medaka('テストメダカ', new Variety('楊貴妃'), 'Sleeping'); + $this->randomMedakaGeneratorP->generate()->willReturn($medaka)->shouldBeCalled(); + $tank = new Tank(); $this->tankManagerP->load()->willReturn($tank)->shouldBeCalled(); $this->tankManagerP->save( Argument::that( function (Tank $tank) { $this->assertCount(1, $tank->getFishList()); + $this->assertSame('テストメダカ', $tank->getFishList()[0]->getNickName()); return $tank; } @@ -45,7 +54,8 @@ function (Tank $tank) { public function getSUT(): AddMedakaUseCase { return new AddMedakaUseCase( - $this->tankManagerP->reveal() + $this->tankManagerP->reveal(), + $this->randomMedakaGeneratorP->reveal(), ); } }