Skip to content

Commit b40931a

Browse files
More cleanups and fixes
1 parent 16b73e1 commit b40931a

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

Tests/Controller/AbstractControllerTest.php

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,34 @@
1919
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
2020
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
2121
use Symfony\Component\Form\Form;
22+
use Symfony\Component\Form\FormBuilderInterface;
2223
use Symfony\Component\Form\FormConfigInterface;
24+
use Symfony\Component\Form\FormFactoryInterface;
2325
use Symfony\Component\HttpFoundation\BinaryFileResponse;
26+
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
2427
use Symfony\Component\HttpFoundation\File\File;
2528
use Symfony\Component\HttpFoundation\JsonResponse;
29+
use Symfony\Component\HttpFoundation\RedirectResponse;
2630
use Symfony\Component\HttpFoundation\Request;
2731
use Symfony\Component\HttpFoundation\RequestStack;
2832
use Symfony\Component\HttpFoundation\Response;
2933
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
3034
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;
3139
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
40+
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
3241
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
42+
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
43+
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
3344
use Symfony\Component\Security\Core\User\User;
45+
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
3446
use Symfony\Component\Serializer\SerializerInterface;
47+
use Symfony\Component\Routing\RouterInterface;
3548
use Symfony\Component\WebLink\Link;
49+
use Twig\Environment;
3650

3751
class AbstractControllerTest extends TestCase
3852
{
@@ -103,7 +117,7 @@ public function testForward()
103117
$requestStack = new RequestStack();
104118
$requestStack->push($request);
105119

106-
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\HttpKernelInterface::class)->getMock();
120+
$kernel = $this->createMock(HttpKernelInterface::class);
107121
$kernel->expects($this->once())->method('handle')->willReturnCallback(function (Request $request) {
108122
return new Response($request->getRequestFormat().'--'.$request->getLocale());
109123
});
@@ -161,7 +175,7 @@ public function testGetUserWithEmptyContainer()
161175

162176
private function getContainerWithTokenStorage($token = null): Container
163177
{
164-
$tokenStorage = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::class)->getMock();
178+
$tokenStorage = $this->createMock(TokenStorage::class);
165179
$tokenStorage
166180
->expects($this->once())
167181
->method('getToken')
@@ -187,7 +201,7 @@ public function testJsonWithSerializer()
187201
{
188202
$container = new Container();
189203

190-
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
204+
$serializer = $this->createMock(SerializerInterface::class);
191205
$serializer
192206
->expects($this->once())
193207
->method('serialize')
@@ -208,7 +222,7 @@ public function testJsonWithSerializerContextOverride()
208222
{
209223
$container = new Container();
210224

211-
$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
225+
$serializer = $this->createMock(SerializerInterface::class);
212226
$serializer
213227
->expects($this->once())
214228
->method('serialize')
@@ -230,7 +244,7 @@ public function testJsonWithSerializerContextOverride()
230244
public function testFile()
231245
{
232246
$container = new Container();
233-
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\HttpKernelInterface::class)->getMock();
247+
$kernel = $this->createMock(HttpKernelInterface::class);
234248
$container->set('http_kernel', $kernel);
235249

236250
$controller = $this->createController();
@@ -331,7 +345,7 @@ public function testFileFromPathWithCustomizedFileName()
331345

332346
public function testFileWhichDoesNotExist()
333347
{
334-
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException::class);
348+
$this->expectException(FileNotFoundException::class);
335349

336350
$controller = $this->createController();
337351

@@ -340,7 +354,7 @@ public function testFileWhichDoesNotExist()
340354

341355
public function testIsGranted()
342356
{
343-
$authorizationChecker = $this->getMockBuilder(\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface::class)->getMock();
357+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
344358
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(true);
345359

346360
$container = new Container();
@@ -354,9 +368,9 @@ public function testIsGranted()
354368

355369
public function testdenyAccessUnlessGranted()
356370
{
357-
$this->expectException(\Symfony\Component\Security\Core\Exception\AccessDeniedException::class);
371+
$this->expectException(AccessDeniedException::class);
358372

359-
$authorizationChecker = $this->getMockBuilder(\Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface::class)->getMock();
373+
$authorizationChecker = $this->createMock(AuthorizationCheckerInterface::class);
360374
$authorizationChecker->expects($this->once())->method('isGranted')->willReturn(false);
361375

362376
$container = new Container();
@@ -370,7 +384,7 @@ public function testdenyAccessUnlessGranted()
370384

371385
public function testRenderViewTwig()
372386
{
373-
$twig = $this->getMockBuilder(\Twig\Environment::class)->disableOriginalConstructor()->getMock();
387+
$twig = $this->createMock(Environment::class);
374388
$twig->expects($this->once())->method('render')->willReturn('bar');
375389

376390
$container = new Container();
@@ -384,7 +398,7 @@ public function testRenderViewTwig()
384398

385399
public function testRenderTwig()
386400
{
387-
$twig = $this->getMockBuilder(\Twig\Environment::class)->disableOriginalConstructor()->getMock();
401+
$twig = $this->createMock(Environment::class);
388402
$twig->expects($this->once())->method('render')->willReturn('bar');
389403

390404
$container = new Container();
@@ -398,20 +412,20 @@ public function testRenderTwig()
398412

399413
public function testStreamTwig()
400414
{
401-
$twig = $this->getMockBuilder(\Twig\Environment::class)->disableOriginalConstructor()->getMock();
415+
$twig = $this->createMock(Environment::class);
402416

403417
$container = new Container();
404418
$container->set('twig', $twig);
405419

406420
$controller = $this->createController();
407421
$controller->setContainer($container);
408422

409-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\StreamedResponse::class, $controller->stream('foo'));
423+
$this->assertInstanceOf(StreamedResponse::class, $controller->stream('foo'));
410424
}
411425

412426
public function testRedirectToRoute()
413427
{
414-
$router = $this->getMockBuilder(\Symfony\Component\Routing\RouterInterface::class)->getMock();
428+
$router = $this->createMock(RouterInterface::class);
415429
$router->expects($this->once())->method('generate')->willReturn('/foo');
416430

417431
$container = new Container();
@@ -421,7 +435,7 @@ public function testRedirectToRoute()
421435
$controller->setContainer($container);
422436
$response = $controller->redirectToRoute('foo');
423437

424-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $response);
438+
$this->assertInstanceOf(RedirectResponse::class, $response);
425439
$this->assertSame('/foo', $response->getTargetUrl());
426440
$this->assertSame(302, $response->getStatusCode());
427441
}
@@ -432,7 +446,7 @@ public function testRedirectToRoute()
432446
public function testAddFlash()
433447
{
434448
$flashBag = new FlashBag();
435-
$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\Session::class)->getMock();
449+
$session = $this->createMock(Session::class);
436450
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);
437451

438452
$container = new Container();
@@ -449,12 +463,12 @@ public function testCreateAccessDeniedException()
449463
{
450464
$controller = $this->createController();
451465

452-
$this->assertInstanceOf(\Symfony\Component\Security\Core\Exception\AccessDeniedException::class, $controller->createAccessDeniedException());
466+
$this->assertInstanceOf(AccessDeniedException::class, $controller->createAccessDeniedException());
453467
}
454468

455469
public function testIsCsrfTokenValid()
456470
{
457-
$tokenManager = $this->getMockBuilder(\Symfony\Component\Security\Csrf\CsrfTokenManagerInterface::class)->getMock();
471+
$tokenManager = $this->createMock(CsrfTokenManagerInterface::class);
458472
$tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true);
459473

460474
$container = new Container();
@@ -468,7 +482,7 @@ public function testIsCsrfTokenValid()
468482

469483
public function testGenerateUrl()
470484
{
471-
$router = $this->getMockBuilder(\Symfony\Component\Routing\RouterInterface::class)->getMock();
485+
$router = $this->createMock(RouterInterface::class);
472486
$router->expects($this->once())->method('generate')->willReturn('/foo');
473487

474488
$container = new Container();
@@ -485,7 +499,7 @@ public function testRedirect()
485499
$controller = $this->createController();
486500
$response = $controller->redirect('https://dunglas.fr', 301);
487501

488-
$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $response);
502+
$this->assertInstanceOf(RedirectResponse::class, $response);
489503
$this->assertSame('https://dunglas.fr', $response->getTargetUrl());
490504
$this->assertSame(301, $response->getStatusCode());
491505
}
@@ -494,14 +508,14 @@ public function testCreateNotFoundException()
494508
{
495509
$controller = $this->createController();
496510

497-
$this->assertInstanceOf(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class, $controller->createNotFoundException());
511+
$this->assertInstanceOf(NotFoundHttpException::class, $controller->createNotFoundException());
498512
}
499513

500514
public function testCreateForm()
501515
{
502-
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
516+
$form = new Form($this->createMock(FormConfigInterface::class));
503517

504-
$formFactory = $this->getMockBuilder(\Symfony\Component\Form\FormFactoryInterface::class)->getMock();
518+
$formFactory = $this->createMock(FormFactoryInterface::class);
505519
$formFactory->expects($this->once())->method('create')->willReturn($form);
506520

507521
$container = new Container();
@@ -515,9 +529,9 @@ public function testCreateForm()
515529

516530
public function testCreateFormBuilder()
517531
{
518-
$formBuilder = $this->getMockBuilder(\Symfony\Component\Form\FormBuilderInterface::class)->getMock();
532+
$formBuilder = $this->createMock(FormBuilderInterface::class);
519533

520-
$formFactory = $this->getMockBuilder(\Symfony\Component\Form\FormFactoryInterface::class)->getMock();
534+
$formFactory = $this->createMock(FormFactoryInterface::class);
521535
$formFactory->expects($this->once())->method('createBuilder')->willReturn($formBuilder);
522536

523537
$container = new Container();

0 commit comments

Comments
 (0)