Update unit test for Redirect

This commit is contained in:
Jeremy Benoist 2016-11-16 23:07:00 +01:00
parent 54fd55fda1
commit 00bf45b6f2
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C

View file

@ -2,7 +2,11 @@
namespace Tests\Wallabag\CoreBundle\Helper;
use Wallabag\CoreBundle\Entity\Config;
use Wallabag\UserBundle\Entity\User;
use Wallabag\CoreBundle\Helper\Redirect;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
class RedirectTest extends \PHPUnit_Framework_TestCase
{
@ -17,9 +21,37 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->routerMock = $this->getRouterMock();
$user = $this->createUser();
$tokenStorage = $this->createTokenStorage($user);
$this->routerMock = $this->getMockBuilder('Symfony\Component\Routing\Router')
->disableOriginalConstructor()
->getMock();
$this->routerMock->expects($this->any())
->method('generate')
->with('homepage')
->willReturn('homepage');
$user = new User();
$user->setName('youpi');
$user->setEmail('youpi@youpi.org');
$user->setUsername('youpi');
$user->setPlainPassword('youpi');
$user->setEnabled(true);
$user->addRole('ROLE_SUPER_ADMIN');
$config = new Config($user);
$config->setTheme('material');
$config->setItemsPerPage(30);
$config->setReadingSpeed(1);
$config->setLanguage('en');
$config->setPocketConsumerKey('xxxxx');
$config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
$user->setConfig($config);
$this->token = new UsernamePasswordToken($user, 'password', 'key');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($this->token);
$this->redirect = new Redirect($this->routerMock, $tokenStorage);
}
@ -44,70 +76,20 @@ class RedirectTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/unread/list', $redirectUrl);
}
private function getRouterMock()
public function testWithNotLoggedUser()
{
$mock = $this->getMockBuilder('Symfony\Component\Routing\Router')
->disableOriginalConstructor()
->getMock();
$redirect = new Redirect($this->routerMock, new TokenStorage());
$redirectUrl = $redirect->to('/unread/list');
$mock->expects($this->any())
->method('generate')
->with('homepage')
->willReturn('homepage');
return $mock;
$this->assertEquals('/unread/list', $redirectUrl);
}
protected function createTokenStorage($user = null)
public function testUserForRedirectToHomepage()
{
$token = $this->createAuthenticationToken($user);
$this->token->getUser()->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
->disableOriginalConstructor()
->getMock();
$redirectUrl = $this->redirect->to('/unread/list');
$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;
$this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
}
}