1717use Symfony \Component \HttpFoundation \Request ;
1818use Symfony \Component \HttpFoundation \RequestStack ;
1919use Symfony \Component \HttpFoundation \Response ;
20+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBag ;
2021use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
2122use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
2223use Symfony \Component \Security \Core \User \User ;
@@ -204,6 +205,62 @@ public function testStreamTwig()
204205
205206 $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
206207 }
208+
209+ public function testRedirectToRoute ()
210+ {
211+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
212+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
213+
214+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
215+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
216+
217+ $ controller = new TestController ();
218+ $ controller ->setContainer ($ container );
219+ $ response = $ controller ->redirectToRoute ('foo ' );
220+
221+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
222+ $ this ->assertSame ('/foo ' , $ response ->getTargetUrl ());
223+ $ this ->assertSame (302 , $ response ->getStatusCode ());
224+ }
225+
226+ public function testAddFlash ()
227+ {
228+ $ flashBag = new FlashBag ();
229+ $ session = $ this ->getMock ('Symfony\Component\HttpFoundation\Session\Session ' );
230+ $ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ flashBag );
231+
232+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
233+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
234+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ session ));
235+
236+ $ controller = new TestController ();
237+ $ controller ->setContainer ($ container );
238+ $ controller ->addFlash ('foo ' , 'bar ' );
239+
240+ $ this ->assertSame (array ('bar ' ), $ flashBag ->get ('foo ' ));
241+ }
242+
243+ public function testCreateAccessDeniedException ()
244+ {
245+ $ controller = new TestController ();
246+
247+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' , $ controller ->createAccessDeniedException ());
248+ }
249+
250+ public function testIsCsrfTokenValid ()
251+ {
252+ $ tokenManager = $ this ->getMock ('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface ' );
253+ $ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
254+
255+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
256+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
257+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ tokenManager ));
258+
259+ $ controller = new TestController ();
260+ $ controller ->setContainer ($ container );
261+
262+ $ this ->assertTrue ($ controller ->isCsrfTokenValid ('foo ' , 'bar ' ));
263+ }
207264}
208265
209266class TestController extends Controller
@@ -227,4 +284,137 @@ public function denyAccessUnlessGranted($attributes, $object = null, $message =
227284 {
228285 parent ::denyAccessUnlessGranted ($ attributes , $ object , $ message );
229286 }
287+
288+ public function redirectToRoute ($ route , array $ parameters = array (), $ status = 302 )
289+ {
290+ return parent ::redirectToRoute ($ route , $ parameters , $ status );
291+ }
292+
293+ public function addFlash ($ type , $ message )
294+ {
295+ parent ::addFlash ($ type , $ message );
296+ }
297+
298+ public function isCsrfTokenValid ($ id , $ token )
299+ {
300+ return parent ::isCsrfTokenValid ($ id , $ token );
301+ }
302+
303+ public function testGenerateUrl ()
304+ {
305+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
306+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
307+
308+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
309+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
310+
311+ $ controller = new Controller ();
312+ $ controller ->setContainer ($ container );
313+
314+ $ this ->assertEquals ('/foo ' , $ controller ->generateUrl ('foo ' ));
315+ }
316+
317+ public function testRedirect ()
318+ {
319+ $ controller = new Controller ();
320+ $ response = $ controller ->redirect ('http://dunglas.fr ' , 301 );
321+
322+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
323+ $ this ->assertSame ('http://dunglas.fr ' , $ response ->getTargetUrl ());
324+ $ this ->assertSame (301 , $ response ->getStatusCode ());
325+ }
326+
327+ public function testRenderViewTemplating ()
328+ {
329+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
330+ $ templating ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
331+
332+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
333+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
334+
335+ $ controller = new Controller ();
336+ $ controller ->setContainer ($ container );
337+
338+ $ this ->assertEquals ('bar ' , $ controller ->renderView ('foo ' ));
339+ }
340+
341+ public function testRenderTemplating ()
342+ {
343+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
344+ $ templating ->expects ($ this ->once ())->method ('renderResponse ' )->willReturn (new Response ('bar ' ));
345+
346+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
347+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
348+
349+ $ controller = new Controller ();
350+ $ controller ->setContainer ($ container );
351+
352+ $ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
353+ }
354+
355+ public function testStreamTemplating ()
356+ {
357+ $ templating = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
358+
359+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
360+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
361+
362+ $ controller = new Controller ();
363+ $ controller ->setContainer ($ container );
364+
365+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
366+ }
367+
368+ public function testCreateNotFoundException ()
369+ {
370+ $ controller = new Controller ();
371+
372+ $ this ->assertInstanceOf ('Symfony\Component\HttpKernel\Exception\NotFoundHttpException ' , $ controller ->createNotFoundException ());
373+ }
374+
375+ public function testCreateForm ()
376+ {
377+ $ form = $ this ->getMock ('Symfony\Component\Form\FormInterface ' );
378+
379+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
380+ $ formFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ form );
381+
382+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
383+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
384+
385+ $ controller = new Controller ();
386+ $ controller ->setContainer ($ container );
387+
388+ $ this ->assertEquals ($ form , $ controller ->createForm ('foo ' ));
389+ }
390+
391+ public function testCreateFormBuilder ()
392+ {
393+ $ formBuilder = $ this ->getMock ('Symfony\Component\Form\FormBuilderInterface ' );
394+
395+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
396+ $ formFactory ->expects ($ this ->once ())->method ('createBuilder ' )->willReturn ($ formBuilder );
397+
398+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
399+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
400+
401+ $ controller = new Controller ();
402+ $ controller ->setContainer ($ container );
403+
404+ $ this ->assertEquals ($ formBuilder , $ controller ->createFormBuilder ('foo ' ));
405+ }
406+
407+ public function testGetDoctrine ()
408+ {
409+ $ doctrine = $ this ->getMock ('Doctrine\Common\Persistence\ManagerRegistry ' );
410+
411+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
412+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
413+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ doctrine ));
414+
415+ $ controller = new Controller ();
416+ $ controller ->setContainer ($ container );
417+
418+ $ this ->assertEquals ($ doctrine , $ controller ->getDoctrine ());
419+ }
230420}
0 commit comments