Skip to content

Commit

Permalink
add remove strategy use case
Browse files Browse the repository at this point in the history
  • Loading branch information
kpicaza committed Apr 10, 2021
1 parent 6abd241 commit f6df119
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 107 deletions.
35 changes: 35 additions & 0 deletions src/Command/RemoveStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Pheature\Crud\Toggle\Command;

use Pheature\Core\Toggle\Write\FeatureId;
use Pheature\Core\Toggle\Write\StrategyId;

final class RemoveStrategy
{
private FeatureId $featureId;
private StrategyId $strategyId;

private function __construct(string $featureId, string $strategyId)
{
$this->featureId = FeatureId::fromString($featureId);
$this->strategyId = StrategyId::fromString($strategyId);
}

public static function withFeatureAndStrategyId(string $featureId, string $strategyId): self
{
return new self($featureId, $strategyId);
}

public function featureId(): FeatureId
{
return $this->featureId;
}

public function strategyId(): StrategyId
{
return $this->strategyId;
}
}
8 changes: 3 additions & 5 deletions src/Handler/AddStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,24 @@
namespace Pheature\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Core\Toggle\Write\StrategyFactory;
use Pheature\Core\Toggle\Write\Strategy;
use Pheature\Crud\Toggle\Command\AddStrategy as AddStrategyCommand;

final class AddStrategy
{
private FeatureRepository $featureRepository;
private StrategyFactory $strategyFactory;

public function __construct(FeatureRepository $featureRepository, StrategyFactory $strategyFactory)
public function __construct(FeatureRepository $featureRepository)
{
$this->featureRepository = $featureRepository;
$this->strategyFactory = $strategyFactory;
}

public function handle(AddStrategyCommand $command): void
{
$feature = $this->featureRepository->get($command->featureId());

$feature->addStrategy(
$this->strategyFactory->makeFromType($command->strategyId(), $command->strategyType())
new Strategy($command->strategyId(), $command->strategyType())
);

$this->featureRepository->save($feature);
Expand Down
2 changes: 1 addition & 1 deletion src/Handler/CreateFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

namespace Pheature\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\CreateFeature as CreateFeatureCommand;
use Pheature\Crud\Toggle\Model\Feature;

final class CreateFeature
{
Expand Down
27 changes: 27 additions & 0 deletions src/Handler/RemoveStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Pheature\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\RemoveStrategy as RemoveStrategyCommand;

final class RemoveStrategy
{
private FeatureRepository $featureRepository;

public function __construct(FeatureRepository $featureRepository)
{
$this->featureRepository = $featureRepository;
}

public function handle(RemoveStrategyCommand $command): void
{
$feature = $this->featureRepository->get($command->featureId());

$feature->removeStrategy($command->strategyId());

$this->featureRepository->save($feature);
}
}
65 changes: 0 additions & 65 deletions src/Model/Feature.php

This file was deleted.

14 changes: 2 additions & 12 deletions test/Handler/AddStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
namespace Pheature\Test\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Core\Toggle\Write\StrategyFactory;
use Pheature\Core\Toggle\Write\StrategyId;
use Pheature\Core\Toggle\Write\StrategyType;
use Pheature\Crud\Toggle\Command\AddStrategy as AddStrategyCommand;
use Pheature\Crud\Toggle\Handler\AddStrategy;
use Pheature\Crud\Toggle\Model\Feature;
use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use PHPUnit\Framework\TestCase;

Expand All @@ -32,15 +29,8 @@ public function testItShouldAddStrategyToAFeature(): void
$repository->expects($this->once())
->method('save')
->with($feature);
$strategyFactory = $this->createMock(StrategyFactory::class);
$strategyFactory->expects($this->once())
->method('makeFromType')
->with(
$this->isInstanceOf(StrategyId::class),
$this->isInstanceOf(StrategyType::class)
);

$handler = new AddStrategy($repository, $strategyFactory);
$handler = new AddStrategy($repository);
$handler->handle($command);

$strategies = $feature->strategies();
Expand Down
2 changes: 1 addition & 1 deletion test/Handler/CreateFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Pheature\Test\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\CreateFeature as CreateFeatureCommand;
use Pheature\Crud\Toggle\Handler\CreateFeature;
use Pheature\Crud\Toggle\Model\Feature;
use PHPUnit\Framework\TestCase;

final class CreateFeatureTest extends TestCase
Expand Down
2 changes: 1 addition & 1 deletion test/Handler/DisableFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\DisableFeature as DisableFeatureCommand;
use Pheature\Crud\Toggle\Handler\DisableFeature;
use Pheature\Crud\Toggle\Model\Feature;
use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use PHPUnit\Framework\TestCase;

Expand Down
2 changes: 1 addition & 1 deletion test/Handler/EnableFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Toggle\Command\EnableFeature as EnableFeatureCommand;
use Pheature\Crud\Toggle\Handler\EnableFeature;
use Pheature\Crud\Toggle\Model\Feature;
use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use PHPUnit\Framework\TestCase;

Expand Down
47 changes: 47 additions & 0 deletions test/Handler/RemoveStrategyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Pheature\Test\Crud\Toggle\Handler;

use Pheature\Core\Toggle\Write\Feature;
use Pheature\Core\Toggle\Write\FeatureId;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Core\Toggle\Write\Strategy;
use Pheature\Core\Toggle\Write\StrategyId;
use Pheature\Core\Toggle\Write\StrategyType;
use Pheature\Crud\Toggle\Command\RemoveStrategy as RemoveStrategyCommand;
use Pheature\Crud\Toggle\Handler\RemoveStrategy;
use PHPUnit\Framework\TestCase;

final class RemoveStrategyTest extends TestCase
{
private const FEATURE_ID = '252f6942-20ac-4b69-960a-d4246b1895c8';
private const STRATEGY_ID = 'ec721d39-4665-48b8-8e6f-0de3af19cbaf';
private const STRATEGY_TYPE = 'some_strategy';

public function testItShouldAddStrategyToAFeature(): void
{
$feature = Feature::withId(FeatureId::fromString(self::FEATURE_ID));
$feature->addStrategy(new Strategy(
StrategyId::fromString(self::STRATEGY_ID),
StrategyType::fromString(self::STRATEGY_TYPE)
));
$command = RemoveStrategyCommand::withFeatureAndStrategyId(self::FEATURE_ID, self::STRATEGY_ID);
$repository = $this->createMock(FeatureRepository::class);
$repository->expects($this->once())
->method('get')
->with($this->isInstanceOf(FeatureId::class))
->willReturn($feature);
$repository->expects($this->once())
->method('save')
->with($feature);

$this->assertCount(1, $feature->strategies());

$handler = new RemoveStrategy($repository);
$handler->handle($command);

$this->assertCount(0, $feature->strategies());
}
}
21 changes: 0 additions & 21 deletions test/Model/FeatureTest.php

This file was deleted.

0 comments on commit f6df119

Please sign in to comment.