1111use Symfony \Component \Validator \Validator \ValidatorInterface ;
1212use Symfony \Component \Workflow \Event \GuardEvent ;
1313use Symfony \Component \Workflow \EventListener \ExpressionLanguage ;
14+ use Symfony \Component \Workflow \EventListener \GuardExpression ;
1415use Symfony \Component \Workflow \EventListener \GuardListener ;
1516use Symfony \Component \Workflow \Marking ;
1617use Symfony \Component \Workflow \Transition ;
@@ -21,12 +22,17 @@ class GuardListenerTest extends TestCase
2122 private $ authenticationChecker ;
2223 private $ validator ;
2324 private $ listener ;
25+ private $ configuration ;
2426
2527 protected function setUp ()
2628 {
27- $ configuration = array (
29+ $ this -> configuration = array (
2830 'test_is_granted ' => 'is_granted("something") ' ,
2931 'test_is_valid ' => 'is_valid(subject) ' ,
32+ 'test_expression ' => array (
33+ new GuardExpression (new Transition ('name ' , 'from ' , 'to ' ), '!is_valid(subject) ' ),
34+ new GuardExpression (new Transition ('name ' , 'from ' , 'to ' ), 'is_valid(subject) ' ),
35+ ),
3036 );
3137 $ expressionLanguage = new ExpressionLanguage ();
3238 $ token = $ this ->getMockBuilder (TokenInterface::class)->getMock ();
@@ -36,7 +42,7 @@ protected function setUp()
3642 $ this ->authenticationChecker = $ this ->getMockBuilder (AuthorizationCheckerInterface::class)->getMock ();
3743 $ trustResolver = $ this ->getMockBuilder (AuthenticationTrustResolverInterface::class)->getMock ();
3844 $ this ->validator = $ this ->getMockBuilder (ValidatorInterface::class)->getMock ();
39- $ this ->listener = new GuardListener ($ configuration , $ expressionLanguage , $ tokenStorage , $ this ->authenticationChecker , $ trustResolver , null , $ this ->validator );
45+ $ this ->listener = new GuardListener ($ this -> configuration , $ expressionLanguage , $ tokenStorage , $ this ->authenticationChecker , $ trustResolver , null , $ this ->validator );
4046 }
4147
4248 protected function tearDown ()
@@ -97,11 +103,38 @@ public function testWithValidatorSupportedEventAndAccept()
97103 $ this ->assertFalse ($ event ->isBlocked ());
98104 }
99105
100- private function createEvent ()
106+ public function testWithGuardExpressionWithNotSupportedTransition ()
107+ {
108+ $ event = $ this ->createEvent ();
109+ $ this ->configureValidator (false );
110+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
111+
112+ $ this ->assertFalse ($ event ->isBlocked ());
113+ }
114+
115+ public function testWithGuardExpressionWithSupportedTransition ()
116+ {
117+ $ event = $ this ->createEvent ($ this ->configuration ['test_expression ' ][1 ]->getTransition ());
118+ $ this ->configureValidator (true , true );
119+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
120+
121+ $ this ->assertFalse ($ event ->isBlocked ());
122+ }
123+
124+ public function testGuardExpressionBlocks ()
125+ {
126+ $ event = $ this ->createEvent ($ this ->configuration ['test_expression ' ][1 ]->getTransition ());
127+ $ this ->configureValidator (true , false );
128+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
129+
130+ $ this ->assertTrue ($ event ->isBlocked ());
131+ }
132+
133+ private function createEvent (Transition $ transition = null )
101134 {
102135 $ subject = new \stdClass ();
103136 $ subject ->marking = new Marking ();
104- $ transition = new Transition ('name ' , 'from ' , 'to ' );
137+ $ transition = $ transition ?: new Transition ('name ' , 'from ' , 'to ' );
105138
106139 $ workflow = $ this ->getMockBuilder (WorkflowInterface::class)->getMock ();
107140
0 commit comments