Add no join rule for workflow validation
[MAILPOET-4629]
This commit is contained in:
@ -0,0 +1,32 @@
|
|||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
|
||||||
|
|
||||||
|
use MailPoet\Automation\Engine\Data\Step;
|
||||||
|
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 NoJoinRule implements WorkflowNodeVisitor {
|
||||||
|
/** @var array<string, Step> */
|
||||||
|
private $visitedSteps = [];
|
||||||
|
|
||||||
|
public function initialize(Workflow $workflow): void {
|
||||||
|
$this->visitedSteps = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
|
||||||
|
$step = $node->getStep();
|
||||||
|
$this->visitedSteps[$step->getId()] = $step;
|
||||||
|
foreach ($step->getNextSteps() as $nextStep) {
|
||||||
|
$nextStepId = $nextStep->getId();
|
||||||
|
if (isset($this->visitedSteps[$nextStepId])) {
|
||||||
|
throw Exceptions::workflowStructureNotValid(__('Path join found in workflow graph', 'mailpoet'));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function complete(Workflow $workflow): void {
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,73 @@
|
|||||||
|
<?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 NoJoinRuleTest extends WorkflowRuleTest {
|
||||||
|
public function testItDetectsJoinedPath(): void {
|
||||||
|
$workflow = $this->createWorkflow([
|
||||||
|
'root' => ['a1', 'a2'],
|
||||||
|
'a1' => ['b'],
|
||||||
|
'a2' => ['b'],
|
||||||
|
'b' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$this->expectExceptionMessage('Invalid workflow structure: Path join found in workflow graph');
|
||||||
|
(new WorkflowWalker())->walk($workflow, [new NoJoinRule()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItDetectsLongJoinedPath(): void {
|
||||||
|
$workflow = $this->createWorkflow([
|
||||||
|
'root' => ['a1', 'a2'],
|
||||||
|
'a1' => ['b1'],
|
||||||
|
'a2' => ['b2'],
|
||||||
|
'b1' => ['c1'],
|
||||||
|
'b2' => ['c2'],
|
||||||
|
'c1' => ['d'],
|
||||||
|
'c2' => ['d'],
|
||||||
|
'd' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$this->expectExceptionMessage('Invalid workflow structure: Path join found in workflow graph');
|
||||||
|
(new WorkflowWalker())->walk($workflow, [new NoJoinRule()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItDetectsJoinedPathToSelf(): void {
|
||||||
|
$workflow = $this->createWorkflow([
|
||||||
|
'root' => ['root', 'root'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->expectException(UnexpectedValueException::class);
|
||||||
|
$this->expectExceptionMessage('Invalid workflow structure: Path join found in workflow graph');
|
||||||
|
(new WorkflowWalker())->walk($workflow, [new NoJoinRule()]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItPassesWithSimplePath(): void {
|
||||||
|
$workflow = $this->createWorkflow([
|
||||||
|
'root' => ['a'],
|
||||||
|
'a' => ['b'],
|
||||||
|
'b' => ['c'],
|
||||||
|
'c' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
(new WorkflowWalker())->walk($workflow, [new NoJoinRule()]);
|
||||||
|
// no exception thrown
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testItPassesWithPathSplit(): void {
|
||||||
|
$workflow = $this->createWorkflow([
|
||||||
|
'root' => ['a1', 'a1'],
|
||||||
|
'a1' => [],
|
||||||
|
'a2' => [],
|
||||||
|
]);
|
||||||
|
|
||||||
|
(new WorkflowWalker())->walk($workflow, [new NoJoinRule()]);
|
||||||
|
// no exception thrown
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user