]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/Helper/RedirectTest.php
Tried to fix tests
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Helper / RedirectTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Helper;
4
5 use Wallabag\CoreBundle\Helper\Redirect;
6
7 class RedirectTest extends \PHPUnit_Framework_TestCase
8 {
9 /** @var \PHPUnit_Framework_MockObject_MockObject */
10 private $routerMock;
11
12 /** @var Redirect */
13 private $redirect;
14
15 const PASSWORD = 's3Cr3t';
16 const SALT = '^S4lt$';
17
18 public function setUp()
19 {
20 $this->routerMock = $this->getRouterMock();
21 $user = $this->createUser();
22 $tokenStorage = $this->createTokenStorage($user);
23 $this->redirect = new Redirect($this->routerMock, $tokenStorage);
24 }
25
26 public function testRedirectToNullWithFallback()
27 {
28 $redirectUrl = $this->redirect->to(null, 'fallback');
29
30 $this->assertEquals('fallback', $redirectUrl);
31 }
32
33 public function testRedirectToNullWithoutFallback()
34 {
35 $redirectUrl = $this->redirect->to(null);
36
37 $this->assertEquals($this->routerMock->generate('homepage'), $redirectUrl);
38 }
39
40 public function testRedirectToValidUrl()
41 {
42 $redirectUrl = $this->redirect->to('/unread/list');
43
44 $this->assertEquals('/unread/list', $redirectUrl);
45 }
46
47 private function getRouterMock()
48 {
49 $mock = $this->getMockBuilder('Symfony\Component\Routing\Router')
50 ->disableOriginalConstructor()
51 ->getMock();
52
53 $mock->expects($this->any())
54 ->method('generate')
55 ->with('homepage')
56 ->willReturn('homepage');
57
58 return $mock;
59 }
60
61 protected function createTokenStorage($user = null)
62 {
63 $token = $this->createAuthenticationToken($user);
64
65 $mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
66 ->disableOriginalConstructor()
67 ->getMock();
68
69 $mock
70 ->expects($this->any())
71 ->method('getToken')
72 ->will($this->returnValue($token))
73 ;
74
75 return $mock;
76 }
77
78 protected function createUser()
79 {
80 $mock = $this->getMockBuilder('Symfony\Component\Security\Core\User\UserInterface')
81 ->disableOriginalConstructor()
82 ->getMock();
83
84 $mock
85 ->expects($this->any())
86 ->method('getPassword')
87 ->will($this->returnValue(static::PASSWORD))
88 ;
89
90 $mock
91 ->expects($this->any())
92 ->method('getSalt')
93 ->will($this->returnValue(static::SALT))
94 ;
95
96 return $mock;
97 }
98
99 protected function createAuthenticationToken($user = null)
100 {
101 $mock = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
102 ->disableOriginalConstructor()
103 ->getMock();
104
105 $mock
106 ->expects($this->any())
107 ->method('getUser')
108 ->will($this->returnValue($user))
109 ;
110
111 return $mock;
112 }
113 }