|
3 | 3 | namespace Symfony\Component\Workflow\Tests;
|
4 | 4 |
|
5 | 5 | use PHPUnit\Framework\TestCase;
|
| 6 | +use Symfony\Component\EventDispatcher\EventDispatcher; |
| 7 | +use Symfony\Component\Workflow\Event\GuardEvent; |
6 | 8 | use Symfony\Component\Workflow\StateMachine;
|
| 9 | +use Symfony\Component\Workflow\TransitionBlocker; |
7 | 10 |
|
8 | 11 | class StateMachineTest extends TestCase
|
9 | 12 | {
|
@@ -38,4 +41,70 @@ public function testCanWithMultipleTransition()
|
38 | 41 | $this->assertTrue($net->can($subject, 't2'));
|
39 | 42 | $this->assertTrue($net->can($subject, 't3'));
|
40 | 43 | }
|
| 44 | + |
| 45 | + public function testBuildTransitionBlockerList() |
| 46 | + { |
| 47 | + $definition = $this->createComplexStateMachineDefinition(); |
| 48 | + |
| 49 | + $net = new StateMachine($definition); |
| 50 | + $subject = new \stdClass(); |
| 51 | + |
| 52 | + $subject->marking = 'a'; |
| 53 | + $this->assertTrue($net->buildTransitionBlockerList($subject, 't1')->isEmpty()); |
| 54 | + $subject->marking = 'd'; |
| 55 | + $this->assertTrue($net->buildTransitionBlockerList($subject, 't1')->isEmpty()); |
| 56 | + |
| 57 | + $subject->marking = 'b'; |
| 58 | + $this->assertFalse($net->buildTransitionBlockerList($subject, 't1')->isEmpty()); |
| 59 | + } |
| 60 | + |
| 61 | + public function testBuildTransitionBlockerListWithMultipleTransitions() |
| 62 | + { |
| 63 | + $definition = $this->createComplexStateMachineDefinition(); |
| 64 | + |
| 65 | + $net = new StateMachine($definition); |
| 66 | + $subject = new \stdClass(); |
| 67 | + |
| 68 | + $subject->marking = 'b'; |
| 69 | + $this->assertTrue($net->buildTransitionBlockerList($subject, 't2')->isEmpty()); |
| 70 | + $this->assertTrue($net->buildTransitionBlockerList($subject, 't3')->isEmpty()); |
| 71 | + } |
| 72 | + |
| 73 | + public function testBuildTransitionBlockerListReturnsExpectedReasonOnBranchMerge() |
| 74 | + { |
| 75 | + $definition = $this->createComplexStateMachineDefinition(); |
| 76 | + |
| 77 | + $dispatcher = new EventDispatcher(); |
| 78 | + $net = new StateMachine($definition, null, $dispatcher); |
| 79 | + |
| 80 | + $dispatcher->addListener('workflow.guard', function (GuardEvent $event) { |
| 81 | + $event->addTransitionBlocker(new TransitionBlocker(\sprintf('Transition blocker of place %s', $event->getTransition()->getFroms()[0]), 'blocker')); |
| 82 | + }); |
| 83 | + |
| 84 | + $subject = new \stdClass(); |
| 85 | + |
| 86 | + // There may be multiple transitions with the same name. Make sure that transitions |
| 87 | + // that are not enabled by the marking are evaluated. |
| 88 | + // see https://github.com/symfony/symfony/issues/28432 |
| 89 | + |
| 90 | + // Test if when you are in place "a"trying transition "t1" then returned |
| 91 | + // blocker list contains guard blocker instead blockedByMarking |
| 92 | + $subject->marking = 'a'; |
| 93 | + $transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1'); |
| 94 | + $this->assertCount(1, $transitionBlockerList); |
| 95 | + $blockers = iterator_to_array($transitionBlockerList); |
| 96 | + |
| 97 | + $this->assertSame('Transition blocker of place a', $blockers[0]->getMessage()); |
| 98 | + $this->assertSame('blocker', $blockers[0]->getCode()); |
| 99 | + |
| 100 | + // Test if when you are in place "d" trying transition "t1" then |
| 101 | + // returned blocker list contains guard blocker instead blockedByMarking |
| 102 | + $subject->marking = 'd'; |
| 103 | + $transitionBlockerList = $net->buildTransitionBlockerList($subject, 't1'); |
| 104 | + $this->assertCount(1, $transitionBlockerList); |
| 105 | + $blockers = iterator_to_array($transitionBlockerList); |
| 106 | + |
| 107 | + $this->assertSame('Transition blocker of place d', $blockers[0]->getMessage()); |
| 108 | + $this->assertSame('blocker', $blockers[0]->getCode()); |
| 109 | + } |
41 | 110 | }
|
0 commit comments