Skip to content

Commit

Permalink
[#209] rename from add strategy to set strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
pheaturebot committed May 24, 2021
1 parent 17820b8 commit fb63324
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 38 deletions.
12 changes: 6 additions & 6 deletions src/PatchFeature.php
Expand Up @@ -4,7 +4,7 @@

namespace Pheature\Crud\Psr7\Toggle;

use Pheature\Crud\Toggle\Handler\AddStrategy;
use Pheature\Crud\Toggle\Handler\SetStrategy;
use Pheature\Crud\Toggle\Handler\DisableFeature;
use Pheature\Crud\Toggle\Handler\EnableFeature;
use Pheature\Crud\Toggle\Handler\RemoveStrategy;
Expand All @@ -15,20 +15,20 @@

final class PatchFeature implements RequestHandlerInterface
{
private AddStrategy $addStrategy;
private SetStrategy $setStrategy;
private RemoveStrategy $removeStrategy;
private EnableFeature $enableFeature;
private DisableFeature $disableFeature;
private ResponseFactoryInterface $responseFactory;

public function __construct(
AddStrategy $addStrategy,
SetStrategy $setStrategy,
RemoveStrategy $removeStrategy,
EnableFeature $enableFeature,
DisableFeature $disableFeature,
ResponseFactoryInterface $responseFactory
) {
$this->addStrategy = $addStrategy;
$this->setStrategy = $setStrategy;
$this->removeStrategy = $removeStrategy;
$this->enableFeature = $enableFeature;
$this->disableFeature = $disableFeature;
Expand Down Expand Up @@ -60,8 +60,8 @@ private function handleRequest(string $featureId, ServerRequestInterface $reques
if ($patchRequest->isDisableFeatureAction()) {
$this->disableFeature->handle($patchRequest->disableFeatureCommand());
}
if ($patchRequest->isAddStrategyAction()) {
$this->addStrategy->handle($patchRequest->addStrategyCommand());
if ($patchRequest->isSetStrategyAction()) {
$this->setStrategy->handle($patchRequest->setStrategyCommand());
}
if ($patchRequest->isRemoveStrategyAction()) {
$this->removeStrategy->handle($patchRequest->removeStrategyCommand());
Expand Down
28 changes: 20 additions & 8 deletions src/PatchRequest.php
Expand Up @@ -4,7 +4,7 @@

namespace Pheature\Crud\Psr7\Toggle;

use Pheature\Crud\Toggle\Command\AddStrategy;
use Pheature\Crud\Toggle\Command\SetStrategy;
use Pheature\Crud\Toggle\Command\DisableFeature;
use Pheature\Crud\Toggle\Command\EnableFeature;
use Pheature\Crud\Toggle\Command\RemoveStrategy;
Expand All @@ -15,7 +15,7 @@ final class PatchRequest
{
private const ACTION_ENABLE_FEATURE = 'enable_feature';
private const ACTION_DISABLE_FEATURE = 'disable_feature';
private const ACTION_ADD_STRATEGY = 'add_strategy';
private const ACTION_SET_STRATEGY = 'set_strategy';
private const ACTION_REMOVE_STRATEGY = 'remove_strategy';

private string $featureId;
Expand All @@ -39,18 +39,30 @@ public function __construct(string $featureId, ServerRequestInterface $request)
$this->requestData = $value;
}

public function addStrategyCommand(): AddStrategy
public function setStrategyCommand(): SetStrategy
{
Assert::notNull($this->requestData);
Assert::keyExists($this->requestData, 'strategy_id');
Assert::keyExists($this->requestData, 'strategy_type');
Assert::string($this->requestData['strategy_id']);
Assert::string($this->requestData['strategy_type']);

return AddStrategy::withIdAndType(
/** @var array<array<string, mixed>>|null $segments */
$segments = $this->requestData['segments'] ?? [];
Assert::isArray($segments);
foreach ($segments as $segment) {
Assert::keyExists($segment, 'segment_id');
Assert::keyExists($segment, 'segment_type');
Assert::keyExists($segment, 'criteria');
Assert::string($segment['segment_id']);
Assert::string($segment['segment_type']);
Assert::isArray($segment['criteria']);
}

return SetStrategy::withIdTypeAndSegments(
$this->featureId,
$this->requestData['strategy_id'],
$this->requestData['strategy_type']
$this->requestData['strategy_type'],
$segments
);
}

Expand All @@ -76,9 +88,9 @@ public function disableFeatureCommand(): DisableFeature
return DisableFeature::withId($this->featureId);
}

public function isAddStrategyAction(): bool
public function isSetStrategyAction(): bool
{
return self::ACTION_ADD_STRATEGY === $this->action;
return self::ACTION_SET_STRATEGY === $this->action;
}

public function isRemoveStrategyAction(): bool
Expand Down
111 changes: 87 additions & 24 deletions test/PatchFeatureTest.php
Expand Up @@ -8,7 +8,7 @@
use Pheature\Core\Toggle\Write\FeatureId;
use Pheature\Core\Toggle\Write\FeatureRepository;
use Pheature\Crud\Psr7\Toggle\PatchFeature;
use Pheature\Crud\Toggle\Handler\AddStrategy;
use Pheature\Crud\Toggle\Handler\SetStrategy;
use Pheature\Crud\Toggle\Handler\DisableFeature;
use Pheature\Crud\Toggle\Handler\EnableFeature;
use Pheature\Crud\Toggle\Handler\RemoveStrategy;
Expand All @@ -24,7 +24,7 @@ final class PatchFeatureTest extends TestCase
private FeatureRepository $repository;
/** @var MockObject|ResponseFactoryInterface */
private ResponseFactoryInterface $responseFactory;
private AddStrategy $addStrategy;
private SetStrategy $addStrategy;
private RemoveStrategy $removeStrategy;
private EnableFeature $enableFeature;
private DisableFeature $disableFeature;
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testItShouldHandleAddStrategyRequestAndReturnProcessedResponse()
$request->expects($this->once())
->method('getParsedBody')
->willReturn([
'action' => 'add_strategy',
'action' => 'set_strategy',
'value' => [
'strategy_id' => 'some_strategy_id',
'strategy_type' => 'identity_matching_strategy',
Expand Down Expand Up @@ -222,35 +222,98 @@ public function getInvalidRequestData(): array
'array type action present in request body' => [
['action' => []]
],
'invalid value for add_strategy action' => [
['action' => 'add_strategy', 'value' => []]
'invalid value for set_strategy action' => [
['action' => 'set_strategy', 'value' => []]
],
'null value for add_strategy action' => [
['action' => 'add_strategy', 'value' => null]
'null value for set_strategy action' => [
['action' => 'set_strategy', 'value' => null]
],
'string value for add_strategy action' => [
['action' => 'add_strategy', 'value' => 'hello world!!']
'string value for set_strategy action' => [
['action' => 'set_strategy', 'value' => 'hello world!!']
],
'null strategy_id value for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => null, 'strategy_type' => 'some_type']]
'null strategy_id value for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => null, 'strategy_type' => 'some_type']]
],
'invalid strategy_id value for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => [], 'strategy_type' => 'some_type']]
'invalid strategy_id value for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => [], 'strategy_type' => 'some_type']]
],
'not strategy_id for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_type' => 'some_type']]
'not strategy_id for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_type' => 'some_type']]
],
'not strategy_type for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => 'some_id']]
'not strategy_type for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => 'some_id']]
],
'null strategy_type value for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => null]]
'null strategy_type value for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => null]]
],
'invalid strategy_type value for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => []]]
'invalid strategy_type value for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => []]]
],
'object strategy_type value for add_strategy action' => [
['action' => 'add_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => new \StdClass()]]
'object strategy_type value for set_strategy action' => [
['action' => 'set_strategy', 'value' => ['strategy_id' => 'some_id', 'strategy_type' => new \StdClass()]]
],
'not segment_id for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_type' => 'some_segment_type',
'criteria' => []
]]
]]
],
'invalid segment_id for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_id' => null,
'segment_type' => 'some_segment_type',
'criteria' => [],
]]
]]
],
'not segment_type for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_id' => 'some_segment',
'criteria' => [],
]]
]]
],
'invalid segment_type for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_id' => 'some_segment',
'segment_type' => null,
'criteria' => [],
]]
]]
],
'not criteria for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_id' => 'some_segment',
'segment_type' => 'some_type',
]]
]]
],
'invalid criteria for set_strategy_action with segments' => [
['action' => 'set_strategy', 'value' => [
'strategy_id' => 'some_id',
'strategy_type' => 'some_strategy',
'segments' => [[
'segment_id' => 'some_segment',
'segment_type' => 'some_type',
'criteria' => 'invalid criteria',
]]
]]
],
'invalid value for remove_strategy action' => [
['action' => 'remove_strategy', 'value' => []]
Expand All @@ -271,7 +334,7 @@ protected function setUp(): void
{
$this->repository = $this->createMock(FeatureRepository::class);
$this->responseFactory = $this->createMock(ResponseFactoryInterface::class);
$this->addStrategy = new AddStrategy($this->repository);
$this->addStrategy = new SetStrategy($this->repository);
$this->removeStrategy = new RemoveStrategy($this->repository);
$this->enableFeature = new EnableFeature($this->repository);
$this->disableFeature = new DisableFeature($this->repository);
Expand Down

0 comments on commit fb63324

Please sign in to comment.