From a3ea91adae4ed683e802a7060bd0268eb16557c5 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Wed, 14 Sep 2022 15:13:31 +0200 Subject: [PATCH] Add no split rule for workflow validation [MAILPOET-4629] --- .../Validation/WorkflowRules/NoSplitRule.php | 23 ++++++++ .../WorkflowRules/NoSplitRuleTest.php | 56 +++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoSplitRule.php create mode 100644 mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoSplitRuleTest.php diff --git a/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoSplitRule.php b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoSplitRule.php new file mode 100644 index 0000000000..f137dfd7c1 --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoSplitRule.php @@ -0,0 +1,23 @@ +getStep(); + if (count($step->getNextSteps()) > 1) { + throw Exceptions::workflowStructureNotValid(__('Path split found in workflow graph', 'mailpoet')); + } + } + + public function complete(Workflow $workflow): void { + } +} diff --git a/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoSplitRuleTest.php b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoSplitRuleTest.php new file mode 100644 index 0000000000..a2abae42f7 --- /dev/null +++ b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoSplitRuleTest.php @@ -0,0 +1,56 @@ +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 + } +}