Skip to content

[FrameworkBundle][2.7] Add tests for the Controller class #18204

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}