Skip to content

Commit

Permalink
Merge branch '5.1' into 5.2
Browse files Browse the repository at this point in the history
* 5.1:
  More cleanups and fixes
  • Loading branch information
nicolas-grekas committed Jan 27, 2021
2 parents 048006e + b40931a commit ff455b2
Showing 1 changed file with 39 additions and 25 deletions.
64 changes: 39 additions & 25 deletions Tests/Controller/AbstractControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,34 @@
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBag;
use Symfony\Component\DependencyInjection\ParameterBag\FrozenParameterBag;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormConfigInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
use Symfony\Component\Serializer\SerializerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\WebLink\Link;
use Twig\Environment;

class AbstractControllerTest extends TestCase
{
Expand Down Expand Up @@ -103,7 +117,7 @@ public function testForward()
$requestStack = new RequestStack();
$requestStack->push($request);

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

private function getContainerWithTokenStorage($token = null): Container
{
$tokenStorage = $this->getMockBuilder(\Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage::class)->getMock();
$tokenStorage = $this->createMock(TokenStorage::class);
$tokenStorage
->expects($this->once())
->method('getToken')
Expand All @@ -187,7 +201,7 @@ public function testJsonWithSerializer()
{
$container = new Container();

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer = $this->createMock(SerializerInterface::class);
$serializer
->expects($this->once())
->method('serialize')
Expand All @@ -208,7 +222,7 @@ public function testJsonWithSerializerContextOverride()
{
$container = new Container();

$serializer = $this->getMockBuilder(SerializerInterface::class)->getMock();
$serializer = $this->createMock(SerializerInterface::class);
$serializer
->expects($this->once())
->method('serialize')
Expand All @@ -230,7 +244,7 @@ public function testJsonWithSerializerContextOverride()
public function testFile()
{
$container = new Container();
$kernel = $this->getMockBuilder(\Symfony\Component\HttpKernel\HttpKernelInterface::class)->getMock();
$kernel = $this->createMock(HttpKernelInterface::class);
$container->set('http_kernel', $kernel);

$controller = $this->createController();
Expand Down Expand Up @@ -331,7 +345,7 @@ public function testFileFromPathWithCustomizedFileName()

public function testFileWhichDoesNotExist()
{
$this->expectException(\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException::class);
$this->expectException(FileNotFoundException::class);

$controller = $this->createController();

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

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

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

public function testdenyAccessUnlessGranted()
{
$this->expectException(\Symfony\Component\Security\Core\Exception\AccessDeniedException::class);
$this->expectException(AccessDeniedException::class);

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

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

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

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

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

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

public function testStreamTwig()
{
$twig = $this->getMockBuilder(\Twig\Environment::class)->disableOriginalConstructor()->getMock();
$twig = $this->createMock(Environment::class);

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

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

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

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

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

$this->assertInstanceOf(\Symfony\Component\HttpFoundation\RedirectResponse::class, $response);
$this->assertInstanceOf(RedirectResponse::class, $response);
$this->assertSame('/foo', $response->getTargetUrl());
$this->assertSame(302, $response->getStatusCode());
}
Expand All @@ -432,7 +446,7 @@ public function testRedirectToRoute()
public function testAddFlash()
{
$flashBag = new FlashBag();
$session = $this->getMockBuilder(\Symfony\Component\HttpFoundation\Session\Session::class)->getMock();
$session = $this->createMock(Session::class);
$session->expects($this->once())->method('getFlashBag')->willReturn($flashBag);

$container = new Container();
Expand All @@ -449,12 +463,12 @@ public function testCreateAccessDeniedException()
{
$controller = $this->createController();

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

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

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

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

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

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

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

public function testCreateForm()
{
$form = new Form($this->getMockBuilder(FormConfigInterface::class)->getMock());
$form = new Form($this->createMock(FormConfigInterface::class));

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

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

public function testCreateFormBuilder()
{
$formBuilder = $this->getMockBuilder(\Symfony\Component\Form\FormBuilderInterface::class)->getMock();
$formBuilder = $this->createMock(FormBuilderInterface::class);

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

$container = new Container();
Expand Down

0 comments on commit ff455b2

Please sign in to comment.