-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SDK-812: Added interactive workflow transition. (#196)
* SDK-832 refactor workflow listener * SDK-832 update composer dependency and revert some changes * SDK-832 review fixes * SDK-812: Interactive workflow resolver * SDK-812: Created test for resolver * SDK-723: updated composer * SDK-812: Fixed issue after merging * SDK-812: Created test, fixed code issues. * SDK-812: Updated doc block * SDK-812: Updated doc block * SDK-812: Fixed run inited workflows * SDK-812: Fixed message Co-authored-by: Sergey Romankov <[email protected]>
- Loading branch information
1 parent
a18c7dd
commit 346c3cb
Showing
8 changed files
with
247 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2019-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerSdk\Sdk\Extension\Exception; | ||
|
||
use LogicException; | ||
|
||
class UniqueValueException extends LogicException | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/Extension/Workflow/InteractionAnswerBasedTransitionResolver.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2019-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerSdk\Sdk\Extension\Workflow; | ||
|
||
use SprykerSdk\Sdk\Core\Application\Dto\ReceiverValue; | ||
use SprykerSdk\Sdk\Extension\Exception\UniqueValueException; | ||
use SprykerSdk\SdkContracts\Entity\ContextInterface; | ||
use SprykerSdk\SdkContracts\ValueReceiver\ValueReceiverInterface; | ||
use SprykerSdk\SdkContracts\Workflow\TransitionResolverInterface; | ||
|
||
class InteractionAnswerBasedTransitionResolver implements TransitionResolverInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const QUESTION = 'question'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public const CHOICES = 'choices'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public const RESOLVER_NAME = 'interactive'; | ||
|
||
/** | ||
* @var \SprykerSdk\SdkContracts\ValueReceiver\ValueReceiverInterface | ||
*/ | ||
protected ValueReceiverInterface $cliValueReceiver; | ||
|
||
/** | ||
* @param \SprykerSdk\SdkContracts\ValueReceiver\ValueReceiverInterface $cliValueReceiver | ||
*/ | ||
public function __construct(ValueReceiverInterface $cliValueReceiver) | ||
{ | ||
$this->cliValueReceiver = $cliValueReceiver; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getName(): string | ||
{ | ||
return static::RESOLVER_NAME; | ||
} | ||
|
||
/** | ||
* @param \SprykerSdk\SdkContracts\Entity\ContextInterface $context | ||
* @param array $settings | ||
* | ||
* @return string|null | ||
*/ | ||
public function resolveTransition(ContextInterface $context, array $settings): ?string | ||
{ | ||
$choiceValues = $this->getChoiceValues($settings); | ||
$flippedValues = array_flip($choiceValues); | ||
|
||
$answer = $this->cliValueReceiver->receiveValue(new ReceiverValue( | ||
$settings[static::QUESTION] ?? '', | ||
null, | ||
'string', | ||
array_values($choiceValues), | ||
)); | ||
|
||
return $flippedValues[$answer]; | ||
} | ||
|
||
/** | ||
* @param array $settings | ||
* | ||
* @throws \SprykerSdk\Sdk\Extension\Exception\UniqueValueException | ||
* | ||
* @return array<string, string> | ||
*/ | ||
protected function getChoiceValues(array $settings): array | ||
{ | ||
$choices = $settings[static::CHOICES] ?? []; | ||
$choiceValues = []; | ||
foreach ($choices as $transition => $choice) { | ||
$choiceValues[(string)$transition] = (string)$choice['description']; | ||
} | ||
|
||
$duplicates = $this->getDuplicateDescriptions($choiceValues); | ||
if ($duplicates) { | ||
throw new UniqueValueException(sprintf('Descriptions for choices must be unique: `%s`', implode('`,`', $duplicates))); | ||
} | ||
|
||
return $choiceValues; | ||
} | ||
|
||
/** | ||
* @param array<string, string> $choiceValues | ||
* | ||
* @return array<string> | ||
*/ | ||
protected function getDuplicateDescriptions(array $choiceValues): array | ||
{ | ||
$duplicates = array_diff_assoc($choiceValues, array_unique($choiceValues)); | ||
|
||
return array_unique($duplicates); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
tests/Sdk/Unit/Extension/Workflow/InteractionAnswerBasedTransitionResolverTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright © 2019-present Spryker Systems GmbH. All rights reserved. | ||
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. | ||
*/ | ||
|
||
namespace SprykerSdk\Sdk\Unit\Extension\Workflow; | ||
|
||
use Codeception\Test\Unit; | ||
use SprykerSdk\Sdk\Extension\Exception\UniqueValueException; | ||
use SprykerSdk\Sdk\Extension\Workflow\InteractionAnswerBasedTransitionResolver; | ||
use SprykerSdk\SdkContracts\Entity\ContextInterface; | ||
use SprykerSdk\SdkContracts\ValueReceiver\ValueReceiverInterface; | ||
|
||
/** | ||
* @group Sdk | ||
* @group Extension | ||
* @group Workflow | ||
* @group InteractionAnswerBasedTransitionResolverTest | ||
*/ | ||
class InteractionAnswerBasedTransitionResolverTest extends Unit | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function testResolveTransitionFailed(): void | ||
{ | ||
// Arrange | ||
$valueReceiver = $this->createMock(ValueReceiverInterface::class); | ||
$valueReceiver->expects($this->once()) | ||
->method('receiveValue') | ||
->willReturn('Choice two description.'); | ||
$context = $this->createMock(ContextInterface::class); | ||
|
||
$resolveTransition = new InteractionAnswerBasedTransitionResolver($valueReceiver); | ||
|
||
$settings = [ | ||
InteractionAnswerBasedTransitionResolver::QUESTION => 'Interactive question', | ||
InteractionAnswerBasedTransitionResolver::CHOICES => [ | ||
'choice one' => [ | ||
'description' => 'Choice one description.', | ||
], | ||
'choice two' => [ | ||
'description' => 'Choice two description.', | ||
], | ||
], | ||
]; | ||
|
||
// Act | ||
$transition = $resolveTransition->resolveTransition($context, $settings); | ||
|
||
// Assert | ||
$this->assertSame('choice two', $transition); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function testResolveTransitionWithEqualDescription(): void | ||
{ | ||
// Arrange | ||
$valueReceiver = $this->createMock(ValueReceiverInterface::class); | ||
$context = $this->createMock(ContextInterface::class); | ||
|
||
$resolveTransition = new InteractionAnswerBasedTransitionResolver($valueReceiver); | ||
|
||
$settings = [ | ||
InteractionAnswerBasedTransitionResolver::QUESTION => 'Interactive question', | ||
InteractionAnswerBasedTransitionResolver::CHOICES => [ | ||
'choice one' => [ | ||
'description' => 'Choice one description.', | ||
], | ||
'choice two' => [ | ||
'description' => 'Choice one description.', | ||
], | ||
], | ||
]; | ||
|
||
// Assert | ||
$this->expectException(UniqueValueException::class); | ||
|
||
// Act | ||
$resolveTransition->resolveTransition($context, $settings); | ||
} | ||
} |