Add no duplicate edges rule for workflow validation
[MAILPOET-4629]
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
|
||||
|
||||
use MailPoet\Automation\Engine\Data\Workflow;
|
||||
use MailPoet\Automation\Engine\Exceptions;
|
||||
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNode;
|
||||
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowNodeVisitor;
|
||||
|
||||
class NoDuplicateEdgesRule implements WorkflowNodeVisitor {
|
||||
public function initialize(Workflow $workflow): void {
|
||||
}
|
||||
|
||||
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
|
||||
$visitedNextStepIdsMap = [];
|
||||
foreach ($node->getStep()->getNextSteps() as $nextStep) {
|
||||
if (isset($visitedNextStepIdsMap[$nextStep->getId()])) {
|
||||
throw Exceptions::workflowStructureNotValid(__('Duplicate next step definition found', 'mailpoet'));
|
||||
}
|
||||
$visitedNextStepIdsMap[$nextStep->getId()] = true;
|
||||
}
|
||||
}
|
||||
|
||||
public function complete(Workflow $workflow): void {
|
||||
}
|
||||
}
|
@@ -0,0 +1,33 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
|
||||
|
||||
require_once __DIR__ . '/WorkflowRuleTest.php';
|
||||
|
||||
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker;
|
||||
|
||||
class NoDuplicateEdgesTest extends WorkflowRuleTest {
|
||||
public function testItDetectsDuplicateEdges(): void {
|
||||
$workflow = $this->createWorkflow([
|
||||
'root' => ['a', 'a'],
|
||||
'a' => [],
|
||||
]);
|
||||
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->expectExceptionMessage('Invalid workflow structure: Duplicate next step definition found');
|
||||
(new WorkflowWalker())->walk($workflow, [new NoDuplicateEdgesRule()]);
|
||||
}
|
||||
|
||||
public function testItPassesWithSingleEdges(): void {
|
||||
$workflow = $this->createWorkflow([
|
||||
'root' => ['a'],
|
||||
'a' => ['b'],
|
||||
'b' => ['c'],
|
||||
'c' => [],
|
||||
]);
|
||||
|
||||
(new WorkflowWalker())->walk($workflow, [new NoDuplicateEdgesRule()]);
|
||||
// no exception thrown
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user