Skip to content

Commit

Permalink
feat: Random medaka generate
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukihiro Arisawa committed Apr 29, 2024
1 parent 79b720e commit f91b27b
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 7 deletions.
Binary file modified .memory/tank.memory
Binary file not shown.
6 changes: 5 additions & 1 deletion config/psysh/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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' => [
Expand Down
26 changes: 26 additions & 0 deletions src/Domain/Logic/NicknameGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\Domain\Logic;

class NicknameGenerator
{
private array $fishNicknames = [
'ジョン',
'たけし',
'マイク',
'マイケル',
'ジョニー',
'ボブ',
'ありさわ',
'ゆきひろ',
];

public function generate(): string
{
$randomIndex = array_rand($this->fishNicknames);

return $this->fishNicknames[$randomIndex];
}
}
28 changes: 28 additions & 0 deletions src/Domain/Logic/RandomMedakaGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\Domain\Logic;

use Iamyukihiro\Aquarium\Domain\Model\Fish\Medaka;
use Iamyukihiro\Aquarium\Domain\VO\Variety;

/**
* The psychopathic class.
*/
class RandomMedakaGenerator
{
public function __construct(
private NicknameGenerator $nicknameGenerator
) {
}

public function generate(): Medaka
{
return new Medaka(
$this->nicknameGenerator->generate() . 'メダカ',
new Variety('みゆき'),
'Swim'
);
}
}
7 changes: 7 additions & 0 deletions src/Domain/Model/Fish/FishInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
9 changes: 4 additions & 5 deletions src/UseCase/AddMedakaUseCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
25 changes: 25 additions & 0 deletions tests/Domain/Logic/NicknameGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/** @noinspection NonAsciiCharacters */

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\Domain\Logic;

use PHPUnit\Framework\TestCase;

class NicknameGeneratorTest extends TestCase
{
public function test_ニックネームが生成されること(): void
{
$SUT = $this->getSUT();
$actual = $SUT->generate();

$this->assertIsString($actual);
}

public function getSUT(): NicknameGenerator
{
return new NicknameGenerator();
}
}
29 changes: 29 additions & 0 deletions tests/Domain/Logic/RandomMedakaGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/** @noinspection NonAsciiCharacters */

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\Domain\Logic;

use Iamyukihiro\Aquarium\Domain\Model\Fish\Medaka;
use PHPUnit\Framework\TestCase;

class RandomMedakaGeneratorTest extends TestCase
{
public function test_メダカが生成されること(): void
{
$SUT = $this->getSUT();
$actual = $SUT->generate();

$this->assertInstanceOf(Medaka::class, $actual);
$this->assertIsString($actual->getNickName());
}

public function getSUT(): RandomMedakaGenerator
{
return new RandomMedakaGenerator(
new NicknameGenerator()
);
}
}
12 changes: 11 additions & 1 deletion tests/UseCase/AddMedakaUseCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -45,7 +54,8 @@ function (Tank $tank) {
public function getSUT(): AddMedakaUseCase
{
return new AddMedakaUseCase(
$this->tankManagerP->reveal()
$this->tankManagerP->reveal(),
$this->randomMedakaGeneratorP->reveal(),
);
}
}

0 comments on commit f91b27b

Please sign in to comment.