Add no duplicate edges rule for workflow validation

[MAILPOET-4629]
This commit is contained in:
Jan Jakes
2022-09-14 15:11:59 +02:00
committed by David Remer
parent 11a711e42c
commit 325418e826
2 changed files with 59 additions and 0 deletions

View File

@@ -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 {
}
}

View File

@@ -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
}
}