Add consisten steps map rule for workflow validation
[MAILPOET-4629]
This commit is contained in:
@ -0,0 +1,24 @@
|
||||
<?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 ConsistentStepMapRule implements WorkflowNodeVisitor {
|
||||
public function initialize(Workflow $workflow): void {
|
||||
foreach ($workflow->getSteps() as $id => $step) {
|
||||
if ($id !== $step->getId()) {
|
||||
throw Exceptions::workflowStructureNotValid(__('TODO', 'mailpoet'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function visitNode(Workflow $workflow, WorkflowNode $node): void {
|
||||
}
|
||||
|
||||
public function complete(Workflow $workflow): void {
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php declare(strict_types = 1);
|
||||
|
||||
namespace MailPoet\Automation\Engine\Validation\WorkflowRules;
|
||||
|
||||
require_once __DIR__ . '/WorkflowRuleTest.php';
|
||||
|
||||
use MailPoet\Automation\Engine\Data\Workflow;
|
||||
use MailPoet\Automation\Engine\Exceptions\UnexpectedValueException;
|
||||
use MailPoet\Automation\Engine\Validation\WorkflowGraph\WorkflowWalker;
|
||||
|
||||
class ConsistentStepMapRuleTest extends WorkflowRuleTest {
|
||||
public function testItDetectsWrongKeyValuePair(): void {
|
||||
$workflow = $this->make(Workflow::class, ['getSteps' => [
|
||||
'root' => $this->createStep('a'),
|
||||
]]);
|
||||
|
||||
$this->expectException(UnexpectedValueException::class);
|
||||
$this->expectExceptionMessage('Invalid workflow structure: TODO');
|
||||
(new WorkflowWalker())->walk($workflow, [new ConsistentStepMapRule()]);
|
||||
}
|
||||
|
||||
public function testItPassesWithCorrectKeyValuePair(): void {
|
||||
$workflow = $this->make(Workflow::class, ['getSteps' => [
|
||||
'root' => $this->createStep('root'),
|
||||
]]);
|
||||
|
||||
(new WorkflowWalker())->walk($workflow, [new ConsistentStepMapRule()]);
|
||||
// no exception thrown
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user