Tried to fix tests

This commit is contained in:
Nicolas Lœuillet 2016-11-10 15:50:10 +01:00 committed by Jeremy Benoist
parent 65cd8a4a9a
commit 54fd55fda1
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
2 changed files with 60 additions and 6 deletions

View file

@ -13,7 +13,6 @@ class Redirect
{ {
private $router; private $router;
private $tokenStorage; private $tokenStorage;
private $actionMarkAsRead;
public function __construct(Router $router, TokenStorageInterface $tokenStorage) public function __construct(Router $router, TokenStorageInterface $tokenStorage)
{ {

View file

@ -12,12 +12,14 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
/** @var Redirect */ /** @var Redirect */
private $redirect; private $redirect;
const PASSWORD = 's3Cr3t';
const SALT = '^S4lt$';
public function setUp() public function setUp()
{ {
$this->routerMock = $this->getRouterMock(); $this->routerMock = $this->getRouterMock();
$tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') $user = $this->createUser();
->disableOriginalConstructor() $tokenStorage = $this->createTokenStorage($user);
->getMock();
$this->redirect = new Redirect($this->routerMock, $tokenStorage); $this->redirect = new Redirect($this->routerMock, $tokenStorage);
} }
@ -25,14 +27,14 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
{ {
$redirectUrl = $this->redirect->to(null, 'fallback'); $redirectUrl = $this->redirect->to(null, 'fallback');
$this->assertEquals(null, $redirectUrl); $this->assertEquals('fallback', $redirectUrl);
} }
public function testRedirectToNullWithoutFallback() public function testRedirectToNullWithoutFallback()
{ {
$redirectUrl = $this->redirect->to(null); $redirectUrl = $this->redirect->to(null);
$this->assertEquals(null, $redirectUrl); $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
} }
public function testRedirectToValidUrl() public function testRedirectToValidUrl()
@ -55,4 +57,57 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
return $mock; return $mock;
} }
protected function createTokenStorage($user = null)
{
$token = $this->createAuthenticationToken($user);
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->any())
->method('getToken')
->will($this->returnValue($token))
;
return $mock;
}
protected function createUser()
{
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->any())
->method('getPassword')
->will($this->returnValue(static::PASSWORD))
;
$mock
->expects($this->any())
->method('getSalt')
->will($this->returnValue(static::SALT))
;
return $mock;
}
protected function createAuthenticationToken($user = null)
{
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
->disableOriginalConstructor()
->getMock();
$mock
->expects($this->any())
->method('getUser')
->will($this->returnValue($user))
;
return $mock;
}
} }