19
19
use Symfony \Component \DependencyInjection \ParameterBag \ContainerBag ;
20
20
use Symfony \Component \DependencyInjection \ParameterBag \FrozenParameterBag ;
21
21
use Symfony \Component \Form \Form ;
22
+ use Symfony \Component \Form \FormBuilderInterface ;
22
23
use Symfony \Component \Form \FormConfigInterface ;
24
+ use Symfony \Component \Form \FormFactoryInterface ;
23
25
use Symfony \Component \HttpFoundation \BinaryFileResponse ;
26
+ use Symfony \Component \HttpFoundation \File \Exception \FileNotFoundException ;
24
27
use Symfony \Component \HttpFoundation \File \File ;
25
28
use Symfony \Component \HttpFoundation \JsonResponse ;
29
+ use Symfony \Component \HttpFoundation \RedirectResponse ;
26
30
use Symfony \Component \HttpFoundation \Request ;
27
31
use Symfony \Component \HttpFoundation \RequestStack ;
28
32
use Symfony \Component \HttpFoundation \Response ;
29
33
use Symfony \Component \HttpFoundation \ResponseHeaderBag ;
30
34
use Symfony \Component \HttpFoundation \Session \Flash \FlashBag ;
35
+ use Symfony \Component \HttpFoundation \Session \Session ;
36
+ use Symfony \Component \HttpFoundation \StreamedResponse ;
37
+ use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
38
+ use Symfony \Component \HttpKernel \HttpKernelInterface ;
31
39
use Symfony \Component \Security \Core \Authentication \Token \AnonymousToken ;
40
+ use
10000
Symfony \Component \Security \Core \Authentication \Token \Storage \TokenStorage ;
32
41
use Symfony \Component \Security \Core \Authentication \Token \UsernamePasswordToken ;
42
+ use Symfony \Component \Security \Core \Authorization \AuthorizationCheckerInterface ;
43
+ use Symfony \Component \Security \Core \Exception \AccessDeniedException ;
33
44
use Symfony \Component \Security \Core \User \User ;
45
+ use Symfony \Component \Security \Csrf \CsrfTokenManagerInterface ;
34
46
use Symfony \Component \Serializer \SerializerInterface ;
47
+ use Symfony \Component \Routing \RouterInterface ;
35
48
use Symfony \Component \WebLink \Link ;
49
+ use Twig \Environment ;
36
50
37
51
class AbstractControllerTest extends TestCase
38
52
{
@@ -103,7 +117,7 @@ public function testForward()
103
117
$ requestStack = new RequestStack ();
104
118
$ requestStack ->push ($ request );
105
119
106
- $ kernel = $ this ->getMockBuilder (\ Symfony \ Component \ HttpKernel \ HttpKernelInterface::class)-> getMock ( );
120
+ $ kernel = $ this ->createMock ( HttpKernelInterface::class);
107
121
$ kernel ->expects ($ this ->once ())->method ('handle ' )->willReturnCallback (function (Request $ request ) {
108
122
return new Response ($ request ->getRequestFormat ().'-- ' .$ request ->getLocale ());
109
123
});
@@ -161,7 +175,7 @@ public function testGetUserWithEmptyContainer()
161
175
162
176
private function getContainerWithTokenStorage ($ token = null ): Container
163
177
{
164
- $ tokenStorage = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authentication \ Token \ Storage \ TokenStorage::class)-> getMock ( );
178
+ $ tokenStorage = $ this ->createMock ( TokenStorage::class);
165
179
$ tokenStorage
166
180
->expects ($ this ->once ())
167
181
->method ('getToken ' )
@@ -187,7 +201,7 @@ public function testJsonWithSerializer()
187
201
{
188
202
$ container = new Container ();
189
203
190
- $ serializer = $ this ->getMockBuilder (SerializerInterface::class)-> getMock ( );
204
+ $ serializer = $ this ->createMock (SerializerInterface::class);
191
205
$ serializer
192
206
->expects ($ this ->once ())
193
207
->method ('serialize ' )
@@ -208,7 +222,7 @@ public function testJsonWithSerializerContextOverride()
208
222
{
209
223
$ container = new Container ();
210
224
211
- $ serializer = $ this ->getMockBuilder (SerializerInterface::class)-> getMock ( );
225
+ $ serializer = $ this ->createMock (SerializerInterface::class);
212
226
$ serializer
213
227
->expects ($ this ->once ())
214
228
->method ('serialize ' )
@@ -230,7 +244,7 @@ public function testJsonWithSerializerContextOverride()
230
244
public function testFile ()
231
245
{
232
246
$ container = new Container ();
233
- $ kernel = $ this ->getMockBuilder (\ Symfony \ Component \ HttpKernel \ HttpKernelInterface::class)-> getMock ( );
247
+ $ kernel = $ this ->createMock ( HttpKernelInterface::class);
234
248
$ container ->set ('http_kernel ' , $ kernel );
235
249
236
250
$ controller = $ this ->createController ();
@@ -331,7 +345,7 @@ public function testFileFromPathWithCustomizedFileName()
331
345
332
346
public function testFileWhichDoesNotExist ()
333
347
{
334
- $ this ->expectException (\ Symfony \ Component \ HttpFoundation \ File \ Exception \ FileNotFoundException::class);
348
+ $ this ->expectException (FileNotFoundException::class);
335
349
336
350
$ controller = $ this ->createController ();
337
351
@@ -340,7 +354,7 @@ public function testFileWhichDoesNotExist()
340
354
341
355
public function testIsGranted ()
342
356
{
343
- $ authorizationChecker = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authorization \ AuthorizationCheckerInterface::class)-> getMock ( );
357
+ $ authorizationChecker = $ this ->createMock ( AuthorizationCheckerInterface::class);
344
358
$ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (true );
345
359
346
360
$ container = new Container ();
@@ -354,9 +368,9 @@ public function testIsGranted()
354
368
355
369
public function testdenyAccessUnlessGranted ()
356
370
{
357
- $ this ->expectException (\ Symfony \ Component \ Security \ Core \ Exception \ AccessDeniedException::class);
371
+ $ this ->expectException (AccessDeniedException::class);
358
372
359
- $ authorizationChecker = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Core \ Authorization \ AuthorizationCheckerInterface::class)-> getMock ( );
373
+ $ authorizationChecker = $ this ->createMock ( AuthorizationCheckerInterface::class);
360
374
$ authorizationChecker ->expects ($ this ->once ())->method ('isGranted ' )->willReturn (false );
361
375
362
376
$ container = new Container ();
@@ -370,7 +384,7 @@ public function testdenyAccessUnlessGranted()
370
384
371
385
public function testRenderViewTwig ()
372
386
{
373
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)-> disableOriginalConstructor ()-> getMock ( );
387
+ $ twig = $ this ->createMock ( Environment::class);
374
388
$ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
375
389
376
390
$ container = new Container ();
@@ -384,7 +398,7 @@ public function testRenderViewTwig()
384
398
385
399
public function testRenderTwig ()
386
400
{
387
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)-> disableOriginalConstructor ()-> getMock ( );
401
+ $ twig = $ this ->createMock ( Environment::class);
388
402
$ twig ->expects ($ this ->once ())->method ('render ' )->willReturn ('bar ' );
389
403
390
404
$ container = new Container ();
@@ -398,20 +412,20 @@ public function testRenderTwig()
398
412
399
413
public function testStreamTwig ()
400
414
{
401
- $ twig = $ this ->getMockBuilder (\ Twig \ Environment::class)-> disableOriginalConstructor ()-> getMock ( );
415
+ $ twig = $ this ->createMock ( Environment::class);
402
416
403
417
$ container = new Container ();
404
418
$ container ->set ('twig ' , $ twig );
405
419
406
420
$ controller = $ this ->createController ();
407
421
$ controller ->setContainer ($ container );
408
422
409
- $ this ->assertInstanceOf (\ Symfony \ Component \ HttpFoundation \ StreamedResponse::class, $ controller ->stream ('foo ' ));
423
+ $ this ->assertInstanceOf (StreamedResponse::class, $ controller ->stream ('foo ' ));
410
424
}
411
425
412
426
public function testRedirectToRoute ()
413
427
{
414
- $ router = $ this ->getMockBuilder (\ Symfony \ Component \ Routing \ RouterInterface::class)-> getMock ( );
428
+ $ router = $ this ->createMock ( RouterInterface::class);
415
429
$ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
416
430
417
431
$ container = new Container ();
@@ -421,7 +435,7 @@ public function testRedirectToRoute()
421
435
$ controller ->setContainer ($ container );
422
436
$ response = $ controller ->redirectToRoute ('foo ' );
423
437
424
- $ this ->assertInstanceOf (\ Symfony \ Component \ HttpFoundation \ RedirectResponse::class, $ response );
438
+ $ this ->assertInstanceOf (RedirectResponse::class, $ response );
425
439
$ this ->assertSame ('/foo ' , $ response ->getTargetUrl ());
426
440
$ this ->assertSame (302 , $ response ->getStatusCode ());
427
441
}
@@ -432,7 +446,7 @@ public function testRedirectToRoute()
432
446
public function testAddFlash ()
433
447
{
434
448
$ flashBag = new FlashBag ();
435
- $ session = $ this ->getMockBuilder (\ Symfony \ Component \ HttpFoundation \ Session \Session ::class)-> getMock ( );
449
+ $ session = $ this ->createMock ( Session::class);
436
450
$ session ->expects ($ this ->once ())->method ('getFlashBag ' )->willReturn ($ flashBag );
437
451
438
452
$ container = new Container ();
@@ -449,12 +463,12 @@ public function testCreateAccessDeniedException()
449
463
{
450
464
$ controller = $ this ->createController ();
451
465
452
- $ this ->assertInstanceOf (\ Symfony \ Component \ Security \ Core \ Exception \ AccessDeniedException::class, $ controller ->createAccessDeniedException ());
466
+ $ this ->assertInstanceOf (AccessDeniedException::class, $ controller ->createAccessDeniedException ());
453
467
}
454
468
455
469
public function testIsCsrfTokenValid ()
456
470
{
457
- $ tokenManager = $ this ->getMockBuilder (\ Symfony \ Component \ Security \ Csrf \ CsrfTokenManagerInterface::class)-> getMock ( );
471
+ $ tokenManager = $ this ->createMock ( CsrfTokenManagerInterface::class);
458
472
$ tokenManager ->expects ($ this ->once ())->method ('isTokenValid ' )->willReturn (true );
459
473
460
474
$ container = new Container ();
@@ -468,7 +482,7 @@ public function testIsCsrfTokenValid()
468
482
469
483
public function testGenerateUrl ()
470
484
{
471
- $ router = $ this ->getMockBuilder (\ Symfony \ Component \ Routing \ RouterInterface::class)-> getMock ( );
485
+ $ router = $ this ->createMock ( RouterInterface::class);
472
486
$ router ->expects ($ this ->once ())->method ('generate ' )->willReturn ('/foo ' );
473
487
474
488
$ container = new Container ();
@@ -485,7 +499,7 @@ public function testRedirect()
485
499
$ controller = $ this ->createController ();
486
500
$ response = $ controller ->redirect ('https://dunglas.fr ' , 301 );
487
501
488
- $ this ->assertInstanceOf (\ Symfony \ Component \ HttpFoundation \ RedirectResponse::class, $ response );
502
+ $ this ->assertInstanceOf (RedirectResponse::class, $ response );
489
503
$ this ->assertSame ('https://dunglas.fr ' , $ response ->getTargetUrl ());
490
504
$ this ->assertSame (301 , $ response ->getStatusCode ());
491
505
}
@@ -494,14 +508,14 @@ public function testCreateNotFoundException()
494
508
{
495
509
$ controller = $ this ->createController ();
496
510
497
- $ this ->assertInstanceOf (\ Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException::class, $ controller ->createNotFoundException ());
511
+ $ this ->assertInstanceOf (NotFoundHttpException::class, $ controller ->createNotFoundException ());
498
512
}
499
513
500
514
public function testCreateForm ()
501
515
{
502
- $ form = new Form ($ this ->getMockBuilder (FormConfigInterface::class)-> getMock ( ));
516
+ $ form = new Form ($ this ->createMock (FormConfigInterface::class));
503
517
504
- $ formFactory = $ this ->getMockBuilder (\ Symfony \ Component \ Form \ FormFactoryInterface::class)-> getMock ( );
518
+ $ formFactory = $ this ->createMock ( FormFactoryInterface::class);
505
519
$ formFactory ->expects ($ this ->once ())->method ('create ' )->willReturn ($ form );
506
520
507
521
$ container = new Container ();
@@ -515,9 +529,9 @@ public function testCreateForm()
515
529
516
530
public function testCreateFormBuilder ()
517
531
{
518
- $ formBuilder = $ this ->getMockBuilder (\ Symfony \ Component \ Form \ FormBuilderInterface::class)-> getMock ( );
532
+ $ formBuilder = $ this ->createMock ( FormBuilderInterface::class);
519
533
520
- $ formFactory = $ this ->getMockBuilder (\ Symfony \ Component \ Form \ FormFactoryInterface::class)-> getMock ( );
534
+ $ formFactory = $ this ->createMock ( FormFactoryInterface::class);
521
535
$ formFactory ->expects ($ this ->once ())->method ('createBuilder ' )->willReturn ($ formBuilder );
522
536
523
537
$ container = new Container ();
0 commit comments