Add no split rule for workflow validation

[MAILPOET-4629]
This commit is contained in:
Jan Jakes
2022-09-14 15:13:31 +02:00
committed by David Remer
parent 65927cc281
commit a3ea91adae
2 changed files with 79 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
<?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 NoSplitRule implements WorkflowNodeVisitor {
public function initialize(Workflow $workflow): void {
}
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
$step = $node->getStep();
if (count($step->getNextSteps()) > 1) {
throw Exceptions::workflowStructureNotValid(__('Path split found in workflow graph', 'mailpoet'));
}
}
public function complete(Workflow $workflow): void {
}
}

View File

@@ -0,0 +1,56 @@
<?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 NoSplitRuleTest extends WorkflowRuleTest {
public function testItDetectsSplitPath(): void {
$workflow = $this->createWorkflow([
'root' => ['a1', 'a2'],
'a1' => [],
'a2' => [],
]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid workflow structure: Path split found in workflow graph');
(new WorkflowWalker())->walk($workflow, [new NoSplitRule()]);
}
public function testItDetectsSplitPathWithSelfLoop(): void {
$workflow = $this->createWorkflow([
'root' => ['root', 'a'],
'a' => [],
]);
$this->expectException(UnexpectedValueException::class);
$this->expectExceptionMessage('Invalid workflow structure: Path split found in workflow graph');
(new WorkflowWalker())->walk($workflow, [new NoSplitRule()]);
}
public function testItPassesWithSimplePath(): void {
$workflow = $this->createWorkflow([
'root' => ['a'],
'a' => ['b'],
'b' => ['c'],
'c' => [],
]);
(new WorkflowWalker())->walk($workflow, [new NoSplitRule()]);
// no exception thrown
}
public function testItPassesWithJoinedPath(): void {
$workflow = $this->createWorkflow([
'root' => ['a'],
'a' => ['b'],
'b' => ['a'],
]);
(new WorkflowWalker())->walk($workflow, [new NoSplitRule()]);
// no exception thrown
}
}