1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\Common\Storage; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Storage\Session; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class SessionTest extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @covers \Session::__construct |
12
|
|
|
* |
13
|
|
|
* @runInSeparateProcess |
14
|
|
|
*/ |
15
|
|
|
public function testConstructCorrectInterface(): void |
16
|
|
|
{ |
17
|
|
|
$storage = new Session(); |
18
|
|
|
|
19
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $storage); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @covers \Session::__construct |
24
|
|
|
* |
25
|
|
|
* @runInSeparateProcess |
26
|
|
|
*/ |
27
|
|
|
public function testConstructWithoutStartingSession(): void |
28
|
|
|
{ |
29
|
|
|
session_start(); |
30
|
|
|
|
31
|
|
|
$storage = new Session(false); |
32
|
|
|
|
33
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $storage); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers \Session::__construct |
38
|
|
|
* |
39
|
|
|
* @runInSeparateProcess |
40
|
|
|
*/ |
41
|
|
|
public function testConstructTryingToStartWhileSessionAlreadyExists(): void |
42
|
|
|
{ |
43
|
|
|
session_start(); |
44
|
|
|
|
45
|
|
|
$storage = new Session(); |
46
|
|
|
|
47
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $storage); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @covers \Session::__construct |
52
|
|
|
* |
53
|
|
|
* @runInSeparateProcess |
54
|
|
|
*/ |
55
|
|
|
public function testConstructWithExistingSessionKey(): void |
56
|
|
|
{ |
57
|
|
|
session_start(); |
58
|
|
|
|
59
|
|
|
$_SESSION['lusitanian_oauth_token'] = []; |
60
|
|
|
|
61
|
|
|
$storage = new Session(); |
62
|
|
|
|
63
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $storage); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @covers \Session::__construct |
68
|
|
|
* @covers \Session::storeAccessToken |
69
|
|
|
* |
70
|
|
|
* @runInSeparateProcess |
71
|
|
|
*/ |
72
|
|
|
public function testStoreAccessTokenIsAlreadyArray(): void |
73
|
|
|
{ |
74
|
|
|
$storage = new Session(); |
75
|
|
|
|
76
|
|
|
self::assertInstanceOf( |
77
|
|
|
'\\OAuth\\Common\\Storage\\Session', |
78
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @covers \Session::__construct |
84
|
|
|
* @covers \Session::storeAccessToken |
85
|
|
|
* |
86
|
|
|
* @runInSeparateProcess |
87
|
|
|
*/ |
88
|
|
|
public function testStoreAccessTokenIsNotArray(): void |
89
|
|
|
{ |
90
|
|
|
$storage = new Session(); |
91
|
|
|
|
92
|
|
|
$_SESSION['lusitanian_oauth_token'] = 'foo'; |
93
|
|
|
|
94
|
|
|
self::assertInstanceOf( |
95
|
|
|
'\\OAuth\\Common\\Storage\\Session', |
96
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @covers \Session::__construct |
102
|
|
|
* @covers \Session::hasAccessToken |
103
|
|
|
* @covers \Session::retrieveAccessToken |
104
|
|
|
* @covers \Session::storeAccessToken |
105
|
|
|
* |
106
|
|
|
* @runInSeparateProcess |
107
|
|
|
*/ |
108
|
|
|
public function testRetrieveAccessTokenValid(): void |
109
|
|
|
{ |
110
|
|
|
$storage = new Session(); |
111
|
|
|
|
112
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')); |
113
|
|
|
|
114
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Token\\TokenInterface', $storage->retrieveAccessToken('foo')); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @covers \Session::__construct |
119
|
|
|
* @covers \Session::hasAccessToken |
120
|
|
|
* @covers \Session::retrieveAccessToken |
121
|
|
|
* |
122
|
|
|
* @runInSeparateProcess |
123
|
|
|
*/ |
124
|
|
|
public function testRetrieveAccessTokenThrowsExceptionWhenTokenIsNotFound(): void |
125
|
|
|
{ |
126
|
|
|
$this->expectException('\\OAuth\\Common\\Storage\\Exception\\TokenNotFoundException'); |
127
|
|
|
|
128
|
|
|
$storage = new Session(); |
129
|
|
|
|
130
|
|
|
$storage->retrieveAccessToken('foo'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @covers \Session::__construct |
135
|
|
|
* @covers \Session::hasAccessToken |
136
|
|
|
* @covers \Session::storeAccessToken |
137
|
|
|
* |
138
|
|
|
* @runInSeparateProcess |
139
|
|
|
*/ |
140
|
|
|
public function testHasAccessTokenTrue(): void |
141
|
|
|
{ |
142
|
|
|
$storage = new Session(); |
143
|
|
|
|
144
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')); |
145
|
|
|
|
146
|
|
|
self::assertTrue($storage->hasAccessToken('foo')); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @covers \Session::__construct |
151
|
|
|
* @covers \Session::hasAccessToken |
152
|
|
|
* |
153
|
|
|
* @runInSeparateProcess |
154
|
|
|
*/ |
155
|
|
|
public function testHasAccessTokenFalse(): void |
156
|
|
|
{ |
157
|
|
|
$storage = new Session(); |
158
|
|
|
|
159
|
|
|
self::assertFalse($storage->hasAccessToken('foo')); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @covers \Session::__construct |
164
|
|
|
* @covers \Session::clearToken |
165
|
|
|
* |
166
|
|
|
* @runInSeparateProcess |
167
|
|
|
*/ |
168
|
|
|
public function testClearTokenIsNotSet(): void |
169
|
|
|
{ |
170
|
|
|
$storage = new Session(); |
171
|
|
|
|
172
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\Session', $storage->clearToken('foo')); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @covers \Session::__construct |
177
|
|
|
* @covers \Session::clearToken |
178
|
|
|
* @covers \Session::storeAccessToken |
179
|
|
|
* |
180
|
|
|
* @runInSeparateProcess |
181
|
|
|
*/ |
182
|
|
|
public function testClearTokenSet(): void |
183
|
|
|
{ |
184
|
|
|
$storage = new Session(); |
185
|
|
|
|
186
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')); |
187
|
|
|
|
188
|
|
|
self::assertTrue($storage->hasAccessToken('foo')); |
189
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\Session', $storage->clearToken('foo')); |
190
|
|
|
self::assertFalse($storage->hasAccessToken('foo')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @covers \Session::__construct |
195
|
|
|
* @covers \Session::clearAllTokens |
196
|
|
|
* @covers \Session::storeAccessToken |
197
|
|
|
* |
198
|
|
|
* @runInSeparateProcess |
199
|
|
|
*/ |
200
|
|
|
public function testClearAllTokens(): void |
201
|
|
|
{ |
202
|
|
|
$storage = new Session(); |
203
|
|
|
|
204
|
|
|
$storage->storeAccessToken('foo', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')); |
205
|
|
|
$storage->storeAccessToken('bar', $this->createMock('\\OAuth\\Common\\Token\\TokenInterface')); |
206
|
|
|
|
207
|
|
|
self::assertTrue($storage->hasAccessToken('foo')); |
208
|
|
|
self::assertTrue($storage->hasAccessToken('bar')); |
209
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Storage\\Session', $storage->clearAllTokens()); |
210
|
|
|
self::assertFalse($storage->hasAccessToken('foo')); |
211
|
|
|
self::assertFalse($storage->hasAccessToken('bar')); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @covers \Session::__construct |
216
|
|
|
* @covers \Session::__destruct |
217
|
|
|
* |
218
|
|
|
* @runInSeparateProcess |
219
|
|
|
*/ |
220
|
|
|
public function testDestruct(): void |
221
|
|
|
{ |
222
|
|
|
$storage = new Session(); |
223
|
|
|
|
224
|
|
|
unset($storage); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @covers \Session::retrieveAccessToken |
229
|
|
|
* @covers \Session::storeAccessToken |
230
|
|
|
* |
231
|
|
|
* @runInSeparateProcess |
232
|
|
|
*/ |
233
|
|
|
public function testSerializeUnserialize(): void |
234
|
|
|
{ |
235
|
|
|
$mock = $this->createMock('\\OAuth\\Common\\Token\\AbstractToken', ['__sleep']); |
|
|
|
|
236
|
|
|
$mock->expects(self::once()) |
237
|
|
|
->method('__sleep') |
238
|
|
|
->willReturn(['accessToken']); |
239
|
|
|
|
240
|
|
|
$storage = new Session(); |
241
|
|
|
$storage->storeAccessToken('foo', $mock); |
242
|
|
|
$retrievedToken = $storage->retrieveAccessToken('foo'); |
243
|
|
|
|
244
|
|
|
self::assertInstanceOf('\\OAuth\\Common\\Token\\AbstractToken', $retrievedToken); |
245
|
|
|
} |
246
|
|
|
} |
247
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.