11
11
use Symfony \Component \Validator \Validator \ValidatorInterface ;
12
12
use Symfony \Component \Workflow \Event \GuardEvent ;
13
13
use Symfony \Component \Workflow \EventListener \ExpressionLanguage ;
14
+ use Symfony \Component \Workflow \EventListener \GuardExpression ;
14
15
use Symfony \Component \Workflow \EventListener \GuardListener ;
15
16
use Symfony \Component \Workflow \Marking ;
16
17
use Symfony \Component \Workflow \Transition ;
@@ -21,12 +22,17 @@ class GuardListenerTest extends TestCase
21
22
private $ authenticationChecker ;
22
23
private $ validator ;
23
24
private $ listener ;
25
+ private $ configuration ;
24
26
25
27
protected function setUp ()
26
28
{
27
- $ configuration = array (
29
+ $ this -> configuration = array (
28
30
'test_is_granted ' => 'is_granted("something") ' ,
29
31
'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
+ ),
30
36
);
31
37
$ expressionLanguage = new ExpressionLanguage ();
32
38
$ token = $ this ->getMockBuilder (TokenInterface::class)->getMock ();
@@ -36,7 +42,7 @@ protected function setUp()
36
42
$ this ->authenticationChecker = $ this ->getMockBuilder (AuthorizationCheckerInterface::class)->getMock ();
37
43
$ trustResolver = $ this ->getMockBuilder (AuthenticationTrustResolverInterface::class)->getMock ();
38
44
$ 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 );
40
46
}
41
47
42
48
protected function tearDown ()
@@ -97,11 +103,38 @@ public function testWithValidatorSupportedEventAndAccept()
97
103
$ this ->assertFalse ($ event ->isBlocked ());
98
104
}
99
105
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 )
101
134
{
102
135
$ subject = new \stdClass ();
103
136
$ subject ->marking = new Marking ();
104
- $ transition = new Transition ('name ' , 'from ' , 'to ' );
137
+ $ transition = $ transition ?: new Transition ('name ' , 'from ' , 'to ' );
105
138
106
139
$ workflow = $ this ->getMockBuilder (WorkflowInterface::class)->getMock ();
107
140
0 commit comments