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 ;
@@ -20,12 +21,17 @@ class GuardListenerTest extends TestCase
2021 private $ authenticationChecker ;
2122 private $ validator ;
2223 private $ listener ;
24+ private $ transition ;
2325
2426 protected function setUp ()
2527 {
2628 $ configuration = array (
2729 'test_is_granted ' => 'is_granted("something") ' ,
2830 'test_is_valid ' => 'is_valid(subject) ' ,
31+ 'test_expression ' => array (
32+ new GuardExpression ($ this ->getTransition (true ), '!is_valid(subject) ' ),
33+ new GuardExpression ($ this ->getTransition (true ), 'is_valid(subject) ' ),
34+ ),
2935 );
3036 $ expressionLanguage = new ExpressionLanguage ();
3137 $ token = $ this ->getMockBuilder (TokenInterface::class)->getMock ();
@@ -96,11 +102,38 @@ public function testWithValidatorSupportedEventAndAccept()
96102 $ this ->assertFalse ($ event ->isBlocked ());
97103 }
98104
99- private function createEvent ()
105+ public function testWithGuardExpressionWithNotSupportedTransition ()
106+ {
107+ $ event = $ this ->createEvent (true );
108+ $ this ->configureValidator (false , false );
109+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
110+
111+ $ this ->assertFalse ($ event ->isBlocked ());
112+ }
113+
114+ public function testWithGuardExpressionWithSupportedTransition ()
115+ {
116+ $ event = $ this ->createEvent ();
117+ $ this ->configureValidator (true , true );
118+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
119+
120+ $ this ->assertFalse ($ event ->isBlocked ());
121+ }
122+
123+ public function testGuardExpressionBlocks ()
124+ {
125+ $ event = $ this ->createEvent ();
126+ $ this ->configureValidator (true , false );
127+ $ this ->listener ->onTransition ($ event , 'test_expression ' );
128+
129+ $ this ->assertTrue ($ event ->isBlocked ());
130+ }
131+
132+ private function createEvent ($ newTransition = false )
100133 {
101134 $ subject = new \stdClass ();
102135 $ subject ->marking = new Marking ();
103- $ transition = new Transition ( ' name ' , ' from ' , ' to ' );
136+ $ transition = $ this -> getTransition ( $ newTransition );
104137
105138 return new GuardEvent ($ subject , $ subject ->marking , $ transition );
106139 }
@@ -140,4 +173,13 @@ private function configureValidator($isUsed, $valid = true)
140173 ->willReturn ($ valid ? array () : array ('a violation ' ))
141174 ;
142175 }
176+
177+ private function getTransition ($ new = false )
178+ {
179+ if ($ new || !$ this ->transition ) {
180+ $ this ->transition = new Transition ('name ' , 'from ' , 'to ' );
181+ }
182+
183+ return $ this ->transition ;
184+ }
143185}
0 commit comments