17
17
use Symfony \Component \HttpFoundation \Request ;
18
18
use Symfony \Component \HttpFoundation \RequestStack ;
19
19
use Symfony \Component \HttpFoundation \Response ;
20
+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBag ;
20
21
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
21
22
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
22
23
use Symfony \Component \Security \Core \User \User ;
@@ -124,6 +125,62 @@ private function getContainerWithTokenStorage($token = null)
124
125
125
126
return $ container ;
126
127
}
128
+
129
+ public function testRedirectToRoute ()
130
+ {
131
+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
132
+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
133
+
134
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
135
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
136
+
137
+ $ controller = new TestController ();
138
+ $ controller ->setContainer ($ container );
139
+ $ response = $ controller ->redirectToRoute ('foo ' );
140
+
141
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
142
+ $ this ->assertSame ('/foo ' , $ response ->getTargetUrl ());
143
+ $ this ->assertSame (302 , $ response ->getStatusCode ());
144
+ }
145
+
146
+ public function testAddFlash ()
147
+ {
148
+ $ flashBag = new FlashBag ();
149
+ $ session = $ this ->getMock ('Symfony\Component\HttpFoundation\Session\Session ' );
150
+ $ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ flashBag );
151
+
152
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
153
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
154
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ session ));
155
+
156
+ $ controller = new TestController ();
157
+ $ controller ->setContainer ($ container );
158
+ $ controller ->addFlash ('foo ' , 'bar ' );
159
+
160
+ $ this ->assertSame (array ('bar ' ), $ flashBag ->get ('foo ' ));
161
+ }
162
+
163
+ public function testCreateAccessDeniedException ()
164
+ {
165
+ $ controller = new TestController ();
166
+
167
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' , $ controller ->createAccessDeniedException ());
168
+ }
169
+
170
+ public function testIsCsrfTokenValid ()
171
+ {
172
+ $ tokenManager = $ this ->getMock ('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface ' );
173
+ $ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
174
+
175
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
176
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
177
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ tokenManager ));
178
+
179
+ $ controller = new TestController ();
180
+ $ controller ->setContainer ($ container );
181
+
182
+ $ this ->assertTrue ($ controller ->isCsrfTokenValid ('foo ' , 'bar ' ));
183
+ }
127
184
}
128
185
129
186
class TestController extends Controller
@@ -137,4 +194,19 @@ public function getUser()
137
194
{
138
195
return parent ::getUser ();
139
196
}
197
+
198
+ public function redirectToRoute ($ route , array $ parameters = array (), $ status = 302 )
199
+ {
200
+ return parent ::redirectToRoute ($ route , $ parameters , $ status );
201
+ }
202
+
203
+ public function addFlash ($ type , $ message )
204
+ {
205
+ parent ::addFlash ($ type , $ message );
206
+ }
207
+
208
+ public function isCsrfTokenValid ($ id , $ token )
209
+ {
210
+ return parent ::isCsrfTokenValid ($ id , $ token );
211
+ }
140
212
}
0 commit comments