Skip to content

Commit

Permalink
add: AddMedakaUseCase
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukihiro Arisawa committed Apr 29, 2024
1 parent 4eecb89 commit 2db8204
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 59 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
/.php-version
/.phpcs-cache
/.phpunit.cache/
.DS_Store
Binary file modified .memory/tank_test.memory
Binary file not shown.
6 changes: 4 additions & 2 deletions config/psysh/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,21 @@
use Iamyukihiro\Aquarium\Service\PsySH\AddMedakaCommand;
use Iamyukihiro\Aquarium\Service\PsySH\InitTankCommand;
use Iamyukihiro\Aquarium\Service\PsySH\ViewTankCommand;
use Iamyukihiro\Aquarium\UseCase\AddMedakaUseCase;

$tankPATH = dirname(__FILE__).'/../../.memory/tank.memory';

$tankManager = new TankManager($tankPATH);
$addMedakaUseCase = new AddMedakaUseCase($tankManager);

return [
'commands' => [
new InitTankCommand($tankManager),
new ViewTankCommand($tankManager),
new AddMedakaCommand($tankManager)
new AddMedakaCommand($addMedakaUseCase)
],
'defaultIncludes' => [
__DIR__ . '/../../vendor/autoload.php',
],
'startupMessage' => sprintf('<info>PHP Aquarium > ( ‘o’ ∋ )))<</info>'),
'startupMessage' => '<info>PHP Aquarium > ( ‘o’ ∋ )))<</info>',
];
11 changes: 3 additions & 8 deletions src/Service/PsySH/AddMedakaCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

namespace Iamyukihiro\Aquarium\Service\PsySH;

use Iamyukihiro\Aquarium\Domain\Model\Fish\Medaka;
use Iamyukihiro\Aquarium\Domain\Model\Tank\TankManager;
use Iamyukihiro\Aquarium\Domain\VO\Variety;
use Iamyukihiro\Aquarium\UseCase\AddMedakaUseCase;
use Psy\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class AddMedakaCommand extends Command
{
public function __construct(
private TankManager $tankManager
private AddMedakaUseCase $addMedakaUseCase
) {
parent::__construct();
}
Expand All @@ -34,10 +32,7 @@ protected function configure(): void
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$tank = $this->tankManager->load();
$tank->addFish(new Medaka('メダカ', new Variety('みゆき'), 'Swim'));
$this->tankManager->save($tank);
clearstatcache();
$this->addMedakaUseCase->add();

$output->writeln('水槽にメダカを追加しました');

Expand Down
25 changes: 25 additions & 0 deletions src/UseCase/AddMedakaUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\UseCase;

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

class AddMedakaUseCase
{
public function __construct(
private TankManager $tankManager
) {
}

public function add(): void
{
$tank = $this->tankManager->load();
$tank->addFish(new Medaka('メダカ', new Variety('みゆき'), 'Swim'));
$this->tankManager->save($tank);
clearstatcache();
}
}
Binary file modified tests/Domain/Model/Tank/TankManagerTest.php
Binary file not shown.
27 changes: 0 additions & 27 deletions tests/Service/PsySH/AddMedakaCommandTest.php

This file was deleted.

22 changes: 0 additions & 22 deletions tests/Service/PsySH/ViewTankCommandTest.php

This file was deleted.

51 changes: 51 additions & 0 deletions tests/UseCase/AddMedakaUseCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/** @noinspection NonAsciiCharacters */

declare(strict_types=1);

namespace Iamyukihiro\Aquarium\UseCase;

use Iamyukihiro\Aquarium\Domain\Model\Tank\Tank;
use Iamyukihiro\Aquarium\Domain\Model\Tank\TankManager;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;

class AddMedakaUseCaseTest extends TestCase
{
use ProphecyTrait;

private ObjectProphecy $tankManagerP;

protected function setUp(): void
{
$this->tankManagerP = $this->prophesize(TankManager::class);
}

public function test(): void
{
$tank = new Tank();
$this->tankManagerP->load()->willReturn($tank)->shouldBeCalled();
$this->tankManagerP->save(
Argument::that(
function (Tank $tank) {
$this->assertCount(1, $tank->getFishList());

return $tank;
}
)
)->shouldBeCalled();

$SUT = $this->getSUT();
$SUT->add();
}

public function getSUT(): AddMedakaUseCase
{
return new AddMedakaUseCase(
$this->tankManagerP->reveal()
);
}
}

0 comments on commit 2db8204

Please sign in to comment.