From 514a06083a84f451b383e3eeb6ffb80ff9eb64c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Wed, 16 Mar 2016 19:49:12 +0100 Subject: [PATCH] [FrameworkBundle] Add tests for the Controller class --- .../Tests/Controller/ControllerTest.php | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php index 2bb59d9c6f908..997a20895419b 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Controller/ControllerTest.php @@ -17,6 +17,7 @@ use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\HttpFoundation\Session\Flash\FlashBag; use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\User\User; @@ -124,6 +125,62 @@ private function getContainerWithTokenStorage($token = null) return $container; } + + public function testRedirectToRoute() + { + $router = $this->getMock('Symfony\Component\Routing\RouterInterface'); + $router->expects($this->once())->method('generate')->willReturn('/foo'); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('get')->will($this->returnValue($router)); + + $controller = new TestController(); + $controller->setContainer($container); + $response = $controller->redirectToRoute('foo'); + + $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response); + $this->assertSame('/foo', $response->getTargetUrl()); + $this->assertSame(302, $response->getStatusCode()); + } + + public function testAddFlash() + { + $flashBag = new FlashBag(); + $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session'); + $session->expects($this->once())->method('getFlashBag')->willReturn($flashBag); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(1))->method('get')->will($this->returnValue($session)); + + $controller = new TestController(); + $controller->setContainer($container); + $controller->addFlash('foo', 'bar'); + + $this->assertSame(array('bar'), $flashBag->get('foo')); + } + + public function testCreateAccessDeniedException() + { + $controller = new TestController(); + + $this->assertInstanceOf('Symfony\Component\Security\Core\Exception\AccessDeniedException', $controller->createAccessDeniedException()); + } + + public function testIsCsrfTokenValid() + { + $tokenManager = $this->getMock('Symfony\Component\Security\Csrf\CsrfTokenManagerInterface'); + $tokenManager->expects($this->once())->method('isTokenValid')->willReturn(true); + + $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + $container->expects($this->at(0))->method('has')->will($this->returnValue(true)); + $container->expects($this->at(1))->method('get')->will($this->returnValue($tokenManager)); + + $controller = new TestController(); + $controller->setContainer($container); + + $this->assertTrue($controller->isCsrfTokenValid('foo', 'bar')); + } } class TestController extends Controller @@ -137,4 +194,19 @@ public function getUser() { return parent::getUser(); } + + public function redirectToRoute($route, array $parameters = array(), $status = 302) + { + return parent::redirectToRoute($route, $parameters, $status); + } + + public function addFlash($type, $message) + { + parent::addFlash($type, $message); + } + + public function isCsrfTokenValid($id, $token) + { + return parent::isCsrfTokenValid($id, $token); + } }