18
18
use Symfony \Component \HttpFoundation \Request ;
19
19
use Symfony \Component \HttpFoundation \RequestStack ;
20
20
use Symfony \Component \HttpFoundation \Response ;
21
+ use Symfony \Component \HttpFoundation \Session \Flash \FlashBag ;
21
22
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
22
23
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
23
24
use Symfony \Component \Security \Core \User \User ;
@@ -205,6 +206,142 @@ public function testJsonWithSerializerContextOverride()
205
206
$ response ->setEncodingOptions (JSON_FORCE_OBJECT );
206
207
$ this ->assertEquals ('{} ' , $ response ->getContent ());
207
208
}
209
+
210
+ public function testIsGranted ()
211
+ {
212
+ $ authorizationChecker = $ this ->getMock ('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' );
213
+ $ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (true );
214
+
215
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
216
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
217
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ authorizationChecker ));
218
+
219
+ $ controller = new TestController ();
220
+ $ controller ->setContainer ($ container );
221
+
222
+ $ this ->assertTrue ($ controller ->isGranted ('foo ' ));
223
+ }
224
+
225
+ /**
226
+ * @expectedException \Symfony\Component\Security\Core\Exception\AccessDeniedException
227
+ */
228
+ public function testdenyAccessUnlessGranted ()
229
+ {
230
+ $ authorizationChecker = $ this ->getMock ('Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface ' );
231
+ $ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (false );
232
+
233
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
234
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
235
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ authorizationChecker ));
236
+
237
+ $ controller = new TestController ();
238
+ $ controller ->setContainer ($ container );
239
+
240
+ $ controller ->denyAccessUnlessGranted ('foo ' );
241
+ }
242
+
243
+ public function testRenderViewTwig ()
244
+ {
245
+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
246
+ $ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
247
+
248
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
249
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
250
+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
251
+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
252
+
253
+ $ controller = new TestController ();
254
+ $ controller ->setContainer ($ container );
255
+
256
+ $ this ->assertEquals ('bar ' , $ controller ->renderView ('foo ' ));
257
+ }
258
+
259
+ public function testRenderTwig ()
260
+ {
261
+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
262
+ $ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
263
+
264
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
265
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
266
+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
267
+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
268
+
269
+ $ controller = new TestController ();
270
+ $ controller ->setContainer ($ container );
271
+
272
+ $ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
273
+ }
274
+
275
+ public function testStreamTwig ()
276
+ {
277
+ $ twig = $ this ->getMockBuilder ('\Twig_Environment ' )->disableOriginalConstructor ()->getMock ();
278
+
279
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
280
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (false ));
281
+ $ container ->expects ($ this ->at (1 ))->method ('has ' )->will ($ this ->returnValue (true ));
282
+ $ container ->expects ($ this ->at (2 ))->method ('get ' )->will ($ this ->returnValue ($ twig ));
283
+
284
+ $ controller = new TestController ();
285
+ $ controller ->setContainer ($ container );
286
+
287
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
288
+ }
289
+
290
+ public function testRedirectToRoute ()
291
+ {
292
+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
293
+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
294
+
295
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
296
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
297
+
298
+ $ controller = new TestController ();
299
+ $ controller ->setContainer ($ container );
300
+ $ response = $ controller ->redirectToRoute ('foo ' );
301
+
302
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
303
+ $ this ->assertSame ('/foo ' , $ response ->getTargetUrl ());
304
+ $ this ->assertSame (302 , $ response ->getStatusCode ());
305
+ }
306
+
307
+ public function testAddFlash ()
308
+ {
309
+ $ flashBag = new FlashBag ();
310
+ $ session = $ this ->getMock ('Symfony\Component\HttpFoundation\Session\Session ' );
311
+ $ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ flashBag );
312
+
313
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
314
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
315
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ session ));
316
+
317
+ $ controller = new TestController ();
318
+ $ controller ->setContainer ($ container );
319
+ $ controller ->addFlash ('foo ' , 'bar ' );
320
+
321
+ $ this ->assertSame (array ('bar ' ), $ flashBag ->get ('foo ' ));
322
+ }
323
+
324
+ public function testCreateAccessDeniedException ()
325
+ {
326
+ $ controller = new TestController ();
327
+
328
+ $ this ->assertInstanceOf ('Symfony\Component\Security\Core\Exception\AccessDeniedException ' , $ controller ->createAccessDeniedException ());
329
+ }
330
+
331
+ public function testIsCsrfTokenValid ()
332
+ {
333
+ $ tokenManager = $ this ->getMock ('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface ' );
334
+ $ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
335
+
336
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
337
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
338
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ tokenManager ));
339
+
340
+ $ controller = new TestController ();
341
+ $ controller ->setContainer ($ container );
342
+
343
+ $ this ->assertTrue ($ controller ->isCsrfTokenValid ('foo ' , 'bar ' ));
344
+ }
208
345
}
209
346
210
347
class TestController extends Controller
@@ -223,4 +360,147 @@ public function json($data, $status = 200, $headers = array(), $context = array(
223
360
{
224
361
return parent ::json ($ data , $ status , $ headers , $ context );
225
362
}
363
+
364
+ public function isGranted ($ attributes , $ object = null )
365
+ {
366
+ return parent ::isGranted ($ attributes , $ object );
367
+ }
368
+
369
+ public function denyAccessUnlessGranted ($ attributes , $ object = null , $ message = 'Access Denied. ' )
370
+ {
371
+ parent ::denyAccessUnlessGranted ($ attributes , $ object , $ message );
372
+ }
373
+
374
+ public function redirectToRoute ($ route , array $ parameters = array (), $ status = 302 )
375
+ {
376
+ return parent ::redirectToRoute ($ route , $ parameters , $ status );
377
+ }
378
+
379
+ public function addFlash ($ type , $ message )
380
+ {
381
+ parent ::addFlash ($ type , $ message );
382
+ }
383
+
384
+ public function isCsrfTokenValid ($ id , $ token )
385
+ {
386
+ return parent ::isCsrfTokenValid ($ id , $ token );
387
+ }
388
+
389
+ public function testGenerateUrl ()
390
+ {
391
+ $ router = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
392
+ $ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
393
+
394
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
395
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ router ));
396
+
397
+ $ controller = new Controller ();
398
+ $ controller ->setContainer ($ container );
399
+
400
+ $ this ->assertEquals ('/foo ' , $ controller ->generateUrl ('foo ' ));
401
+ }
402
+
403
+ public function testRedirect ()
404
+ {
405
+ $ controller = new Controller ();
406
+ $ response = $ controller ->redirect ('http://dunglas.fr ' , 301 );
407
+
408
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\RedirectResponse ' , $ response );
409
+ $ this ->assertSame ('http://dunglas.fr ' , $ response ->getTargetUrl ());
410
+ $ this ->assertSame (301 , $ response ->getStatusCode ());
411
+ }
412
+
413
+ public function testRenderViewTemplating ()
414
+ {
415
+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
416
+ $ templating ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
417
+
418
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
419
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
420
+
421
+ $ controller = new Controller ();
422
+ $ controller ->setContainer ($ container );
423
+
424
+ $ this ->assertEquals ('bar ' , $ controller ->renderView ('foo ' ));
425
+ }
426
+
427
+ public function testRenderTemplating ()
428
+ {
429
+ $ templating = $ this ->getMock ('Symfony\Bundle\FrameworkBundle\Templating\EngineInterface ' );
430
+ $ templating ->expects ($ this ->once ())->method ('renderResponse ' )->willReturn (new Response ('bar ' ));
431
+
432
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
433
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
434
+
435
+ $ controller = new Controller ();
436
+ $ controller ->setContainer ($ container );
437
+
438
+ $ this ->assertEquals ('bar ' , $ controller ->render ('foo ' )->getContent ());
439
+ }
440
+
441
+ public function testStreamTemplating ()
442
+ {
443
+ $ templating = $ this ->getMock ('Symfony\Component\Routing\RouterInterface ' );
444
+
445
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
446
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ templating ));
447
+
448
+ $ controller = new Controller ();
449
+ $ controller ->setContainer ($ container );
450
+
451
+ $ this ->assertInstanceOf ('Symfony\Component\HttpFoundation\StreamedResponse ' , $ controller ->stream ('foo ' ));
452
+ }
453
+
454
+ public function testCreateNotFoundException ()
455
+ {
456
+ $ controller = new Controller ();
457
+
458
+ $ this ->assertInstanceOf ('Symfony\Component\HttpKernel\Exception\NotFoundHttpException ' , $ controller ->createNotFoundException ());
459
+ }
460
+
461
+ public function testCreateForm ()
462
+ {
463
+ $ form = $ this ->getMock ('Symfony\Component\Form\FormInterface ' );
464
+
465
+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
466
+ $ formFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ form );
467
+
468
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
469
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
470
+
471
+ $ controller = new Controller ();
472
+ $ controller ->setContainer ($ container );
473
+
474
+ $ this ->assertEquals ($ form , $ controller ->createForm ('foo ' ));
475
+ }
476
+
477
+ public function testCreateFormBuilder ()
478
+ {
479
+ $ formBuilder = $ this ->getMock ('Symfony\Component\Form\FormBuilderInterface ' );
480
+
481
+ $ formFactory = $ this ->getMock ('Symfony\Component\Form\FormFactoryInterface ' );
482
+ $ formFactory ->expects ($ this ->once ())->method ('createBuilder ' )->willReturn ($ formBuilder );
483
+
484
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
485
+ $ container ->expects ($ this ->at (0 ))->method ('get ' )->will ($ this ->returnValue ($ formFactory ));
486
+
487
+ $ controller = new Controller ();
488
+ $ controller ->setContainer ($ container );
489
+
490
+ $ this ->assertEquals ($ formBuilder , $ controller ->createFormBuilder ('foo ' ));
491
+ }
492
+
493
+ public function testGetDoctrine ()
494
+ {
495
+ $ doctrine = $ this ->getMock ('Doctrine\Common\Persistence\ManagerRegistry ' );
496
+
497
+ $ container = $ this ->getMock ('Symfony\Component\DependencyInjection\ContainerInterface ' );
498
+ $ container ->expects ($ this ->at (0 ))->method ('has ' )->will ($ this ->returnValue (true ));
499
+ $ container ->expects ($ this ->at (1 ))->method ('get ' )->will ($ this ->returnValue ($ doctrine ));
500
+
501
+ $ controller = new Controller ();
502
+ $ controller ->setContainer ($ container );
503
+
504
+ $ this ->assertEquals ($ doctrine , $ controller ->getDoctrine ());
505
+ }
226
506
}
0 commit comments