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