1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* (c) Christian Gripp <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core23\FacebookBundle\Tests\Session; |
11
|
|
|
|
12
|
|
|
use Core23\FacebookBundle\Session\Session as FacebookSession; |
13
|
|
|
use Core23\FacebookBundle\Session\SessionManager; |
14
|
|
|
use DateTime; |
15
|
|
|
use PHPUnit\Framework\TestCase; |
16
|
|
|
use Prophecy\Argument; |
17
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
18
|
|
|
|
19
|
|
|
final class SessionManagerTest extends TestCase |
20
|
|
|
{ |
21
|
|
View Code Duplication |
public function testIsAuthenticated(): void |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$session = $this->prophesize(Session::class); |
24
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
25
|
|
|
->willReturn(true) |
26
|
|
|
; |
27
|
|
|
|
28
|
|
|
$manager = new SessionManager($session->reveal()); |
29
|
|
|
static::assertTrue($manager->isAuthenticated()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testIsNotAuthenticated(): void |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$session = $this->prophesize(Session::class); |
35
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
36
|
|
|
->willReturn(false) |
37
|
|
|
; |
38
|
|
|
|
39
|
|
|
$manager = new SessionManager($session->reveal()); |
40
|
|
|
static::assertFalse($manager->isAuthenticated()); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
View Code Duplication |
public function testGetUsername(): void |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$session = $this->prophesize(Session::class); |
46
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
47
|
|
|
->willReturn('MyUser') |
48
|
|
|
; |
49
|
|
|
|
50
|
|
|
$manager = new SessionManager($session->reveal()); |
51
|
|
|
static::assertSame('MyUser', $manager->getUsername()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
View Code Duplication |
public function testGetUsernameNotExist(): void |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$session = $this->prophesize(Session::class); |
57
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
58
|
|
|
->willReturn(null) |
59
|
|
|
; |
60
|
|
|
|
61
|
|
|
$manager = new SessionManager($session->reveal()); |
62
|
|
|
static::assertNull($manager->getUsername()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function testStore(): void |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', new DateTime()); |
68
|
|
|
|
69
|
|
|
$session = $this->prophesize(Session::class); |
70
|
|
|
$session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); |
71
|
|
|
$session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); |
72
|
|
|
$session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); |
73
|
|
|
$session->set('_CORE23_FACEBOOK_EXPIRES', Argument::type(DateTime::class))->shouldBeCalled(); |
74
|
|
|
|
75
|
|
|
$manager = new SessionManager($session->reveal()); |
76
|
|
|
$manager->store($facebookSession); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
View Code Duplication |
public function testStoreWithNoExpiryDate(): void |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
$facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', null); |
82
|
|
|
|
83
|
|
|
$session = $this->prophesize(Session::class); |
84
|
|
|
$session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); |
85
|
|
|
$session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); |
86
|
|
|
$session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); |
87
|
|
|
$session->set('_CORE23_FACEBOOK_EXPIRES', Argument::is(null))->shouldBeCalled(); |
88
|
|
|
|
89
|
|
|
$manager = new SessionManager($session->reveal()); |
90
|
|
|
$manager->store($facebookSession); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testClear(): void |
94
|
|
|
{ |
95
|
|
|
$session = $this->prophesize(Session::class); |
96
|
|
|
$session->remove('_CORE23_FACEBOOK_ID')->shouldBeCalled(); |
97
|
|
|
$session->remove('_CORE23_FACEBOOK_TOKEN')->shouldBeCalled(); |
98
|
|
|
$session->remove('_CORE23_FACEBOOK_NAME')->shouldBeCalled(); |
99
|
|
|
$session->remove('_CORE23_FACEBOOK_EXPIRES')->shouldBeCalled(); |
100
|
|
|
|
101
|
|
|
$manager = new SessionManager($session->reveal()); |
102
|
|
|
$manager->clear(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGetSession(): void |
106
|
|
|
{ |
107
|
|
|
$tomorrow = new DateTime('tomorrow'); |
108
|
|
|
|
109
|
|
|
$session = $this->prophesize(Session::class); |
110
|
|
|
$session->get('_CORE23_FACEBOOK_ID') |
111
|
|
|
->willReturn('0815') |
112
|
|
|
; |
113
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
114
|
|
|
->willReturn('TheToken') |
115
|
|
|
; |
116
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
117
|
|
|
->willReturn('MyUser') |
118
|
|
|
; |
119
|
|
|
$session->get('_CORE23_FACEBOOK_EXPIRES') |
120
|
|
|
->willReturn($tomorrow) |
121
|
|
|
; |
122
|
|
|
|
123
|
|
|
$manager = new SessionManager($session->reveal()); |
124
|
|
|
|
125
|
|
|
$facebookSession = $manager->getSession(); |
126
|
|
|
|
127
|
|
|
static::assertNotNull($facebookSession); |
128
|
|
|
static::assertSame('0815', $facebookSession->getFacebookId()); |
129
|
|
|
static::assertSame('MyUser', $facebookSession->getName()); |
130
|
|
|
static::assertSame('TheToken', $facebookSession->getToken()); |
131
|
|
|
static::assertSame($tomorrow, $facebookSession->getExpireDate()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
View Code Duplication |
public function testGetSessionWithNoAuth(): void |
|
|
|
|
135
|
|
|
{ |
136
|
|
|
$session = $this->prophesize(Session::class); |
137
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
138
|
|
|
->willReturn(null) |
139
|
|
|
; |
140
|
|
|
|
141
|
|
|
$manager = new SessionManager($session->reveal()); |
142
|
|
|
|
143
|
|
|
static::assertNull($manager->getSession()); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.