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\SessionInterface; |
14
|
|
|
use Core23\FacebookBundle\Session\SessionManager; |
15
|
|
|
use DateTime; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
use Prophecy\Argument; |
18
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
19
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
20
|
|
|
|
21
|
|
|
class SessionManagerTest extends TestCase |
22
|
|
|
{ |
23
|
|
View Code Duplication |
public function testIsAuthenticated(): void |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
26
|
|
|
$session = $this->prophesize(Session::class); |
27
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
28
|
|
|
->willReturn(true) |
29
|
|
|
; |
30
|
|
|
|
31
|
|
|
$manager = new SessionManager($session->reveal()); |
32
|
|
|
$this->assertTrue($manager->isAuthenticated()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
View Code Duplication |
public function testIsNotAuthenticated(): void |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
38
|
|
|
$session = $this->prophesize(Session::class); |
39
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
40
|
|
|
->willReturn(false) |
41
|
|
|
; |
42
|
|
|
|
43
|
|
|
$manager = new SessionManager($session->reveal()); |
44
|
|
|
$this->assertFalse($manager->isAuthenticated()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
public function testGetUsername(): void |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
50
|
|
|
$session = $this->prophesize(Session::class); |
51
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
52
|
|
|
->willReturn('MyUser') |
53
|
|
|
; |
54
|
|
|
|
55
|
|
|
$manager = new SessionManager($session->reveal()); |
56
|
|
|
$this->assertSame('MyUser', $manager->getUsername()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
View Code Duplication |
public function testGetUsernameNotExist(): void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
62
|
|
|
$session = $this->prophesize(Session::class); |
63
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
64
|
|
|
->willReturn(null) |
65
|
|
|
; |
66
|
|
|
|
67
|
|
|
$manager = new SessionManager($session->reveal()); |
68
|
|
|
$this->assertNull($manager->getUsername()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testStore(): void |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', new DateTime()); |
74
|
|
|
|
75
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
76
|
|
|
$session = $this->prophesize(Session::class); |
77
|
|
|
$session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); |
78
|
|
|
$session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); |
79
|
|
|
$session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); |
80
|
|
|
$session->set('_CORE23_FACEBOOK_EXPIRES', Argument::type(DateTime::class))->shouldBeCalled(); |
81
|
|
|
|
82
|
|
|
$manager = new SessionManager($session->reveal()); |
83
|
|
|
$manager->store($facebookSession); |
84
|
|
|
|
85
|
|
|
$this->assertTrue(true); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
View Code Duplication |
public function testStoreWithNoExpiryDate(): void |
|
|
|
|
89
|
|
|
{ |
90
|
|
|
$facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', null); |
91
|
|
|
|
92
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
93
|
|
|
$session = $this->prophesize(Session::class); |
94
|
|
|
$session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); |
95
|
|
|
$session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); |
96
|
|
|
$session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); |
97
|
|
|
$session->set('_CORE23_FACEBOOK_EXPIRES', Argument::is(null))->shouldBeCalled(); |
98
|
|
|
|
99
|
|
|
$manager = new SessionManager($session->reveal()); |
100
|
|
|
$manager->store($facebookSession); |
101
|
|
|
|
102
|
|
|
$this->assertTrue(true); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testClear(): void |
106
|
|
|
{ |
107
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
108
|
|
|
$session = $this->prophesize(Session::class); |
109
|
|
|
$session->remove('_CORE23_FACEBOOK_ID')->shouldBeCalled(); |
110
|
|
|
$session->remove('_CORE23_FACEBOOK_TOKEN')->shouldBeCalled(); |
111
|
|
|
$session->remove('_CORE23_FACEBOOK_NAME')->shouldBeCalled(); |
112
|
|
|
$session->remove('_CORE23_FACEBOOK_EXPIRES')->shouldBeCalled(); |
113
|
|
|
|
114
|
|
|
$manager = new SessionManager($session->reveal()); |
115
|
|
|
$manager->clear(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function testGetSession(): void |
119
|
|
|
{ |
120
|
|
|
$tomorrow = new DateTime('tomorrow'); |
121
|
|
|
|
122
|
|
|
/** @var ObjectProphecy&Session $session */ |
|
|
|
|
123
|
|
|
$session = $this->prophesize(Session::class); |
124
|
|
|
$session->get('_CORE23_FACEBOOK_ID') |
125
|
|
|
->willReturn('0815') |
126
|
|
|
; |
127
|
|
|
$session->get('_CORE23_FACEBOOK_TOKEN') |
128
|
|
|
->willReturn('TheToken') |
129
|
|
|
; |
130
|
|
|
$session->get('_CORE23_FACEBOOK_NAME') |
131
|
|
|
->willReturn('MyUser') |
132
|
|
|
; |
133
|
|
|
$session->get('_CORE23_FACEBOOK_EXPIRES') |
134
|
|
|
->willReturn($tomorrow) |
135
|
|
|
; |
136
|
|
|
|
137
|
|
|
$manager = new SessionManager($session->reveal()); |
138
|
|
|
|
139
|
|
|
/** @var SessionInterface $facebookSession */ |
140
|
|
|
$facebookSession = $manager->getSession(); |
141
|
|
|
|
142
|
|
|
$this->assertNotNull($facebookSession); |
143
|
|
|
$this->assertSame('0815', $facebookSession->getFacebookId()); |
144
|
|
|
$this->assertSame('MyUser', $facebookSession->getName()); |
145
|
|
|
$this->assertSame('TheToken', $facebookSession->getToken()); |
146
|
|
|
$this->assertSame($tomorrow, $facebookSession->getExpireDate()); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
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.