From 325418e826c203667a5e2075f0b75bcaa5940a01 Mon Sep 17 00:00:00 2001 From: Jan Jakes Date: Wed, 14 Sep 2022 15:11:59 +0200 Subject: [PATCH] Add no duplicate edges rule for workflow validation [MAILPOET-4629] --- .../WorkflowRules/NoDuplicateEdgesRule.php | 26 +++++++++++++++ .../WorkflowRules/NoDuplicateEdgesTest.php | 33 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesRule.php create mode 100644 mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesTest.php diff --git a/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesRule.php b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesRule.php new file mode 100644 index 0000000000..dc63195a7b --- /dev/null +++ b/mailpoet/lib/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesRule.php @@ -0,0 +1,26 @@ +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 { + } +} diff --git a/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesTest.php b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesTest.php new file mode 100644 index 0000000000..40fc44c657 --- /dev/null +++ b/mailpoet/tests/unit/Automation/Engine/Validation/WorkflowRules/NoDuplicateEdgesTest.php @@ -0,0 +1,33 @@ +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 + } +}