77use Symfony \Component \Workflow \Definition ;
88use Symfony \Component \Workflow \Event \Event ;
99use Symfony \Component \Workflow \Event \GuardEvent ;
10- use Symfony \Component \Workflow \Exception \BlockedTransitionException ;
10+ use Symfony \Component \Workflow \Exception \NotEnabledTransitionException ;
11+ use Symfony \Component \Workflow \Exception \UndefinedTransitionException ;
1112use Symfony \Component \Workflow \Marking ;
1213use Symfony \Component \Workflow \MarkingStore \MarkingStoreInterface ;
1314use Symfony \Component \Workflow \MarkingStore \MultipleStateMarkingStore ;
@@ -164,20 +165,6 @@ public function testCanDoesNotTriggerGuardEventsForNotEnabledTransitions()
164165 $ this ->assertSame (array ('workflow_name.guard.t3 ' ), $ dispatchedEvents );
165166 }
166167
167- /**
168- * @expectedException \Symfony\Component\Workflow\Exception\LogicException
169- * @expectedExceptionMessage Unable to apply transition "t2" for workflow "unnamed".
170- */
171- public function testApplyWithImpossibleTransition ()
172- {
173- $ definition = $ this ->createComplexWorkflowDefinition ();
174- $ subject = new \stdClass ();
175- $ subject ->marking = null ;
176- $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore ());
177-
178- $ workflow ->apply ($ subject , 't2 ' );
179- }
180-
181168 public function testCanWithSameNameTransition ()
182169 {
183170 $ definition = $ this ->createWorkflowWithSameNameTransition ();
@@ -195,6 +182,103 @@ public function testCanWithSameNameTransition()
195182 $ this ->assertTrue ($ workflow ->can ($ subject , 'to_a ' ));
196183 }
197184
185+ public function testBuildTransitionBlockerListReturnsUndefinedTransition ()
186+ {
187+ $ definition = $ this ->createSimpleWorkflowDefinition ();
188+ $ subject = new \stdClass ();
189+ $ subject ->marking = null ;
190+ $ workflow = new Workflow ($ definition );
191+
192+ try {
193+ $ workflow ->buildTransitionBlockerList ($ subject , 'not defined ' );
194+
195+ $ this ->fail ('Workflow failed to throw undefined transition exception. ' );
196+ } catch (\Exception $ e ) {
197+ $ this ->assertInstanceOf (UndefinedTransitionException::class, $ e );
198+ }
199+
200+ $ this ->assertSame ('The transition "not defined" is not defined for the workflow "unnamed". ' , $ e ->getMessage ());
201+ }
202+
203+ public function testBuildTransitionBlockerListReturnsReasonsProvidedByMarking ()
204+ {
205+ $ definition = $ this ->createComplexWorkflowDefinition ();
206+ $ subject = new \stdClass ();
207+ $ subject ->marking = null ;
208+ $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore ());
209+
210+ $ transitionBlockerList = $ workflow ->buildTransitionBlockerList ($ subject , 't2 ' );
211+ $ this ->assertCount (1 , $ transitionBlockerList );
212+ $ blockers = iterator_to_array ($ transitionBlockerList );
213+ $ this ->assertSame ('The marking does not enable the transition. ' , $ blockers [0 ]->getMessage ());
214+ $ this ->assertSame ('19beefc8-6b1e-4716-9d07-a39bd6d16e34 ' , $ blockers [0 ]->getCode ());
215+ }
216+
217+ public function testBuildTransitionBlockerListReturnsReasonsProvidedInGuards ()
218+ {
219+ $ definition = $ this ->createSimpleWorkflowDefinition ();
220+ $ subject = new \stdClass ();
221+ $ subject ->marking = null ;
222+ $ dispatcher = new EventDispatcher ();
223+ $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore (), $ dispatcher );
224+
225+ $ dispatcher ->addListener ('workflow.guard ' , function (GuardEvent $ event ) {
226+ $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 1 ' , 'blocker_1 ' ));
227+ $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 2 ' , 'blocker_2 ' ));
228+ });
229+ $ dispatcher ->addListener ('workflow.guard ' , function (GuardEvent $ event ) {
230+ $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 3 ' , 'blocker_3 ' ));
231+ });
232+ $ dispatcher ->addListener ('workflow.guard ' , function (GuardEvent $ event ) {
233+ $ event ->setBlocked (true );
234+ });
235+
236+ $ transitionBlockerList = $ workflow ->buildTransitionBlockerList ($ subject , 't1 ' );
237+ $ this ->assertCount (4 , $ transitionBlockerList );
238+ $ blockers = iterator_to_array ($ transitionBlockerList );
239+ $ this ->assertSame ('Transition blocker 1 ' , $ blockers [0 ]->getMessage ());
240+ $ this ->assertSame ('blocker_1 ' , $ blockers [0 ]->getCode ());
241+ $ this ->assertSame ('Transition blocker 2 ' , $ blockers [1 ]->getMessage ());
242+ $ this ->assertSame ('blocker_2 ' , $ blockers [1 ]->getCode ());
243+ $ this ->assertSame ('Transition blocker 3 ' , $ blockers [2 ]->getMessage ());
244+ $ this ->assertSame ('blocker_3 ' , $ blockers [2 ]->getCode ());
245+ $ this ->assertSame ('Unknown reason. ' , $ blockers [3 ]->getMessage ());
246+ $ this ->assertSame ('e8b5bbb9-5913-4b98-bfa6-65dbd228a82a ' , $ blockers [3 ]->getCode ());
247+ }
248+
249+ /**
250+ * @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException
251+ * @expectedExceptionMessage The transition "404 Not Found" is not defined for the workflow "unnamed".
252+ */
253+ public function testApplyWithNotExisingTransition ()
254+ {
255+ $ definition = $ this ->createComplexWorkflowDefinition ();
256+ $ subject = new \stdClass ();
257+ $ subject ->marking = null ;
258+ $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore ());
259+
260+ $ workflow ->apply ($ subject , '404 Not Found ' );
261+ }
262+
263+ public function testApplyWithNotEnabledTransition ()
264+ {
265+ $ definition = $ this ->createComplexWorkflowDefinition ();
266+ $ subject = new \stdClass ();
267+ $ subject ->marking = null ;
268+ $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore ());
269+
270+ try {
271+ $ workflow ->apply ($ subject , 't2 ' );
272+
273+ $ this ->fail ('Should throw an exception ' );
274+ } catch (NotEnabledTransitionException $ e ) {
275+ $ this ->assertSame ('The transition "t2" is not enabled for the workflow "unnamed". ' , $ e ->getMessage ());
276+ $ this ->assertCount (1 , $ e ->getTransitionBlockerList ());
277+ $ list = iterator_to_array ($ e ->getTransitionBlockerList ());
278+ $ this ->assertSame ('The marking does not enable the transition. ' , $ list [0 ]->getMessage ());
279+ }
280+ }
281+
198282 public function testApply ()
199283 {
200284 $ definition = $ this ->createComplexWorkflowDefinition ();
@@ -413,116 +497,6 @@ public function testGetEnabledTransitionsWithSameNameTransition()
413497 $ this ->assertSame ('to_a ' , $ transitions [1 ]->getName ());
414498 $ this ->assertSame ('to_a ' , $ transitions [2 ]->getName ());
415499 }
416-
417- public function testWhyCannotReturnsReasonsProvidedInGuards ()
418- {
419- $ definition = $ this ->createSimpleWorkflowDefinition ();
420- $ subject = new \stdClass ();
421- $ subject ->marking = null ;
422- $ dispatcher = new EventDispatcher ();
423- $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore (), $ dispatcher );
424-
425- $ guardsAddingTransitionBlockers = array (
426- function (GuardEvent $ event ) {
427- $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 1 ' , 'blocker_1 ' ));
428- $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 2 ' , 'blocker_2 ' ));
429- },
430- function (GuardEvent $ event ) {
431- $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 3 ' , 'blocker_3 ' ));
432- },
433- );
434-
435- foreach ($ guardsAddingTransitionBlockers as $ guard ) {
436- $ dispatcher ->addListener ('workflow.guard ' , $ guard );
437- }
438-
439- $ transitionBlockerList = $ workflow ->buildTransitionBlockerList ($ subject , 't1 ' );
440-
441- $ this ->assertCount (3 , $ transitionBlockerList );
442-
443- $ assertTransitionBlockerPresentByCodeFn = function (string $ code ) use ($ transitionBlockerList ) {
444- $ this ->assertNotNull (
445- $ transitionBlockerList ->findByCode ($ code ),
446- sprintf ('Workflow did not produce transition blocker with code "%s" ' , $ code )
447- );
448- };
449-
450- $ assertTransitionBlockerPresentByCodeFn ('blocker_1 ' );
451- $ assertTransitionBlockerPresentByCodeFn ('blocker_2 ' );
452- $ assertTransitionBlockerPresentByCodeFn ('blocker_3 ' );
453- }
454-
455- public function testWhyCannotReturnsTransitionNotDefinedReason ()
456- {
457- $ definition = $ this ->createSimpleWorkflowDefinition ();
458- $ subject = new \stdClass ();
459- $ subject ->marking = null ;
460- $ workflow = new Workflow ($ definition );
461-
462- $ transitionBlockerList = $ workflow ->buildTransitionBlockerList ($ subject , 'undefined_transition_name ' );
463-
464- $ this ->assertCount (1 , $ transitionBlockerList );
465- $ this ->assertEquals (
466- TransitionBlocker::REASON_TRANSITION_NOT_DEFINED ,
467- $ transitionBlockerList ->get (0 )->getCode ()
468- );
469- }
470-
471- public function testWhyCannotReturnsTransitionNotApplicableReason ()
472- {
473- $ definition = $ this ->createSimpleWorkflowDefinition ();
474- $ subject = new \stdClass ();
475- $ subject ->marking = null ;
476- $ workflow = new Workflow ($ definition );
477-
478- $ transitionBlockerList = $ workflow ->buildTransitionBlockerList ($ subject , 't2 ' );
479-
480- $ this ->assertCount (1 , $ transitionBlockerList );
481- $ this ->assertEquals (
482- TransitionBlocker::REASON_TRANSITION_NOT_APPLICABLE ,
483- $ transitionBlockerList ->get (0 )->getCode ()
484- );
485- }
486-
487- public function testApplyConveysTheTransitionBlockers ()
488- {
489- $ definition = $ this ->createSimpleWorkflowDefinition ();
490- $ subject = new \stdClass ();
491- $ subject ->marking = null ;
492- $ dispatcher = new EventDispatcher ();
493- $ workflow = new Workflow ($ definition , new MultipleStateMarkingStore (), $ dispatcher );
494-
495- $ dispatcher ->addListener ('workflow.guard ' , function (GuardEvent $ event ) {
496- $ event ->addTransitionBlocker (new TransitionBlocker ('Transition blocker 3 ' , 'blocker_1 ' ));
497- });
498-
499- try {
500- $ workflow ->apply ($ subject , 't1 ' );
501- } catch (BlockedTransitionException $ exception ) {
502- $ this ->assertNotNull (
503- $ exception ->getTransitionBlockerList ()->findByCode ('blocker_1 ' ),
504- 'Workflow failed to convey it could not transition subject because of expected blocker '
505- );
506-
507- return ;
508- }
509-
510- $ this ->fail ('Workflow failed to prevent a transition from happening ' );
511- }
512-
513- /**
514- * @expectedException \Symfony\Component\Workflow\Exception\UndefinedTransitionException
515- * @expectedExceptionMessage Transition "undefined_transition" is not defined in workflow "unnamed".
516- */
517- public function testApplyWithUndefinedTransition ()
518- {
519- $ definition = $ this ->createSimpleWorkflowDefinition ();
520- $ subject = new \stdClass ();
521- $ subject ->marking = null ;
522- $ workflow = new Workflow ($ definition );
523-
524- $ workflow ->apply ($ subject , 'undefined_transition ' );
525- }
526500}
527501
528502class EventDispatcherMock implements \Symfony \Component \EventDispatcher \EventDispatcherInterface
0 commit comments