|
7 | 7 | use Symfony\Component\Workflow\Definition;
|
8 | 8 | use Symfony\Component\Workflow\Event\Event;
|
9 | 9 | use Symfony\Component\Workflow\Event\GuardEvent;
|
| 10 | +use Symfony\Component\Workflow\Exception\NotEnabledTransitionException; |
10 | 11 | use Symfony\Component\Workflow\Marking;
|
11 | 12 | use Symfony\Component\Workflow\MarkingStore\MarkingStoreInterface;
|
12 | 13 | use Symfony\Component\Workflow\MarkingStore\MultipleStateMarkingStore;
|
13 | 14 | use Symfony\Component\Workflow\Transition;
|
| 15 | +use Symfony\Component\Workflow\TransitionBlocker; |
14 | 16 | use Symfony\Component\Workflow\Workflow;
|
15 | 17 |
|
16 | 18 | class WorkflowTest extends TestCase
|
@@ -162,35 +164,114 @@ public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions()
|
162 | 164 | $this->assertSame(array('workflow_name.guard.t3'), $dispatchedEvents);
|
163 | 165 | }
|
164 | 166 |
|
| 167 | + public function testCanWithSameNameTransition() |
| 168 | + { |
| 169 | + $definition = $this->createWorkflowWithSameNameTransition(); |
| 170 | + $workflow = new Workflow($definition, new MultipleStateMarkingStore()); |
| 171 | + |
| 172 | + $subject = new \stdClass(); |
| 173 | + $subject->marking = null; |
| 174 | + $this->assertTrue($workflow->can($subject, 'a_to_bc')); |
| 175 | + $this->assertFalse($workflow->can($subject, 'b_to_c')); |
| 176 | + $this->assertFalse($workflow->can($subject, 'to_a')); |
| 177 | + |
| 178 | + $subject->marking = array('b' => 1); |
| 179 | + $this->assertFalse($workflow->can($subject, 'a_to_bc')); |
| 180 | + $this->assertTrue($workflow->can($subject, 'b_to_c')); |
| 181 | + $this->assertTrue($workflow->can($subject, 'to_a')); |
| 182 | + } |
| 183 | + |
165 | 184 | /**
|
166 |
| - * @expectedException \Symfony\Component\Workflow\Exception\LogicException |
167 |
| - * @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed". |
| 185 | + * @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException |
| 186 | + * @expectedExceptionMessage Transition "404 Not Found" is not defined for workflow "unnamed". |
168 | 187 | */
|
169 |
| - public function testApplyWithImpossibleTransition() |
| 188 | + public function testBuildTransitionBlockerListReturnsUndefinedTransition() |
| 189 | + { |
| 190 | + $definition = $this->createSimpleWorkflowDefinition(); |
| 191 | + $subject = new \stdClass(); |
| 192 | + $subject->marking = null; |
| 193 | + $workflow = new Workflow($definition); |
| 194 | + |
| 195 | + $workflow->buildTransitionBlockerList($subject, '404 Not Found'); |
| 196 | + } |
| 197 | + |
| 198 | + public function testBuildTransitionBlockerListReturnsReasonsProvidedByMarking() |
170 | 199 | {
|
171 | 200 | $definition = $this->createComplexWorkflowDefinition();
|
172 | 201 | $subject = new \stdClass();
|
173 | 202 | $subject->marking = null;
|
174 | 203 | $workflow = new Workflow($definition, new MultipleStateMarkingStore());
|
175 | 204 |
|
176 |
| - $workflow->apply($subject, 't2'); |
| 205 | + $transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't2'); |
| 206 | + $this->assertCount(1, $transitionBlockerList); |
| 207 | + $blockers = iterator_to_array($transitionBlockerList); |
| 208 | + $this->assertSame('The marking does not enable the transition.', $blockers[0]->getMessage()); |
| 209 | + $this->assertSame('19beefc8-6b1e-4716-9d07-a39bd6d16e34', $blockers[0]->getCode()); |
177 | 210 | }
|
178 | 211 |
|
179 |
| - public function testCanWithSameNameTransition() |
| 212 | + public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards() |
180 | 213 | {
|
181 |
| - $definition = $this->createWorkflowWithSameNameTransition(); |
| 214 | + $definition = $this->createSimpleWorkflowDefinition(); |
| 215 | + $subject = new \stdClass(); |
| 216 | + $subject->marking = null; |
| 217 | + $dispatcher = new EventDispatcher(); |
| 218 | + $workflow = new Workflow($definition, new MultipleStateMarkingStore(), $dispatcher); |
| 219 | + |
| 220 | + $dispatcher->addListener('workflow.guard', function (GuardEvent $event) { |
| 221 | + $event->addTransitionBlocker(new TransitionBlocker('Transition blocker 1', 'blocker_1')); |
| 222 | + $event->addTransitionBlocker(new TransitionBlocker('Transition blocker 2', 'blocker_2')); |
| 223 | + }); |
| 224 | + $dispatcher->addListener('workflow.guard', function (GuardEvent $event) { |
| 225 | + $event->addTransitionBlocker(new TransitionBlocker('Transition blocker 3', 'blocker_3')); |
| 226 | + }); |
| 227 | + $dispatcher->addListener('workflow.guard', function (GuardEvent $event) { |
| 228 | + $event->setBlocked(true); |
| 229 | + }); |
| 230 | + |
| 231 | + $transitionBlockerList = $workflow->buildTransitionBlockerList($subject, 't1'); |
| 232 | + $this->assertCount(4, $transitionBlockerList); |
| 233 | + $blockers = iterator_to_array($transitionBlockerList); |
| 234 | + $this->assertSame('Transition blocker 1', $blockers[0]->getMessage()); |
| 235 | + $this->assertSame('blocker_1', $blockers[0]->getCode()); |
| 236 | + $this->assertSame('Transition blocker 2', $blockers[1]->getMessage()); |
| 237 | + $this->assertSame('blocker_2', $blockers[1]->getCode()); |
| 238 | + $this->assertSame('Transition blocker 3', $blockers[2]->getMessage()); |
| 239 | + $this->assertSame('blocker_3', $blockers[2]->getCode()); |
| 240 | + $this->assertSame('Unknown reason.', $blockers[3]->getMessage()); |
| 241 | + $this->assertSame('e8b5bbb9-5913-4b98-bfa6-65dbd228a82a', $blockers[3]->getCode()); |
| 242 | + } |
| 243 | + |
| 244 | + /** |
| 245 | + * @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException |
| 246 | + * @expectedExceptionMessage Transition "404 Not Found" is not defined for workflow "unnamed". |
| 247 | + */ |
| 248 | + public function testApplyWithNotExisingTransition() |
| 249 | + { |
| 250 | + $definition = $this->createComplexWorkflowDefinition(); |
| 251 | + $subject = new \stdClass(); |
| 252 | + $subject->marking = null; |
182 | 253 | $workflow = new Workflow($definition, new MultipleStateMarkingStore());
|
183 | 254 |
|
| 255 | + $workflow->apply($subject, '404 Not Found'); |
| 256 | + } |
| 257 | + |
| 258 | + public function testApplyWithNotEnabledTransition() |
| 259 | + { |
| 260 | + $definition = $this->createComplexWorkflowDefinition(); |
184 | 261 | $subject = new \stdClass();
|
185 | 262 | $subject->marking = null;
|
186 |
| - $this->assertTrue($workflow->can($subject, 'a_to_bc')); |
187 |
| - $this->assertFalse($workflow->can($subject, 'b_to_c')); |
188 |
| - $this->assertFalse($workflow->can($subject, 'to_a')); |
| 263 | + $workflow = new Workflow($definition, new MultipleStateMarkingStore()); |
189 | 264 |
|
190 |
| - $subject->marking = array('b' => 1); |
191 |
| - $this->assertFalse($workflow->can($subject, 'a_to_bc')); |
192 |
| - $this->assertTrue($workflow->can($subject, 'b_to_c')); |
193 |
| - $this->assertTrue($workflow->can($subject, 'to_a')); |
| 265 | + try { |
| 266 | + $workflow->apply($subject, 't2'); |
| 267 | + |
| 268 | + $this->fail('Should throw an exception'); |
| 269 | + } catch (NotEnabledTransitionException $e) { |
| 270 | + $this->assertSame('Transition "t2" is not enabled for workflow "unnamed".', $e->getMessage()); |
| 271 | + $this->assertCount(1, $e->getTransitionBlockerList()); |
| 272 | + $list = iterator_to_array($e->getTransitionBlockerList()); |
| 273 | + $this->assertSame('The marking does not enable the transition.', $list[0]->getMessage()); |
| 274 | + } |
194 | 275 | }
|
195 | 276 |
|
196 | 277 | public function testApply()
|
|
0 commit comments