1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author David Desberg <[email protected]> |
5
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace OAuth\Unit\Common\Storage; |
9
|
|
|
|
10
|
|
|
use OAuth\Common\Storage\SymfonySession; |
11
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
12
|
|
|
use PHPUnit\Framework\TestCase; |
13
|
|
|
use Symfony\Component\HttpFoundation\Session\Session; |
14
|
|
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; |
15
|
|
|
|
16
|
|
|
class SymfonySessionTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
protected $session; |
19
|
|
|
|
20
|
|
|
protected $storage; |
21
|
|
|
|
22
|
|
|
protected function setUp(): void |
23
|
|
|
{ |
24
|
|
|
// set it |
25
|
|
|
$this->session = new Session(new MockArraySessionStorage()); |
26
|
|
|
$this->storage = new SymfonySession($this->session); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function tearDown(): void |
30
|
|
|
{ |
31
|
|
|
// delete |
32
|
|
|
$this->storage->getSession()->clear(); |
33
|
|
|
$this->storage = null; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Check that the token survives the constructor. |
38
|
|
|
*/ |
39
|
|
|
public function testStorageSurvivesConstructor(): void |
40
|
|
|
{ |
41
|
|
|
$service = 'Facebook'; |
42
|
|
|
$token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, ['extra' => 'param']); |
43
|
|
|
|
44
|
|
|
// act |
45
|
|
|
$this->storage->storeAccessToken($service, $token); |
46
|
|
|
$this->storage = null; |
47
|
|
|
$this->storage = new SymfonySession($this->session); |
48
|
|
|
|
49
|
|
|
// assert |
50
|
|
|
$extraParams = $this->storage->retrieveAccessToken($service)->getExtraParams(); |
51
|
|
|
self::assertEquals('param', $extraParams['extra']); |
52
|
|
|
self::assertEquals($token, $this->storage->retrieveAccessToken($service)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Check that the token gets properly stored. |
57
|
|
|
*/ |
58
|
|
|
public function testStorage(): void |
59
|
|
|
{ |
60
|
|
|
// arrange |
61
|
|
|
$service_1 = 'Facebook'; |
62
|
|
|
$service_2 = 'Foursquare'; |
63
|
|
|
|
64
|
|
|
$token_1 = new StdOAuth2Token('access_1', 'refresh_1', StdOAuth2Token::EOL_NEVER_EXPIRES, ['extra' => 'param']); |
65
|
|
|
$token_2 = new StdOAuth2Token('access_2', 'refresh_2', StdOAuth2Token::EOL_NEVER_EXPIRES, ['extra' => 'param']); |
66
|
|
|
|
67
|
|
|
// act |
68
|
|
|
$this->storage->storeAccessToken($service_1, $token_1); |
69
|
|
|
$this->storage->storeAccessToken($service_2, $token_2); |
70
|
|
|
|
71
|
|
|
// assert |
72
|
|
|
$extraParams = $this->storage->retrieveAccessToken($service_1)->getExtraParams(); |
73
|
|
|
self::assertEquals('param', $extraParams['extra']); |
74
|
|
|
self::assertEquals($token_1, $this->storage->retrieveAccessToken($service_1)); |
75
|
|
|
self::assertEquals($token_2, $this->storage->retrieveAccessToken($service_2)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Test hasAccessToken. |
80
|
|
|
*/ |
81
|
|
|
public function testHasAccessToken(): void |
82
|
|
|
{ |
83
|
|
|
// arrange |
84
|
|
|
$service = 'Facebook'; |
85
|
|
|
$this->storage->clearToken($service); |
86
|
|
|
|
87
|
|
|
// act |
88
|
|
|
// assert |
89
|
|
|
self::assertFalse($this->storage->hasAccessToken($service)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Check that the token gets properly deleted. |
94
|
|
|
*/ |
95
|
|
|
public function testStorageClears(): void |
96
|
|
|
{ |
97
|
|
|
// arrange |
98
|
|
|
$service = 'Facebook'; |
99
|
|
|
$token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, ['extra' => 'param']); |
100
|
|
|
|
101
|
|
|
// act |
102
|
|
|
$this->storage->storeAccessToken($service, $token); |
103
|
|
|
$this->storage->clearToken($service); |
104
|
|
|
|
105
|
|
|
// assert |
106
|
|
|
$this->expectException('OAuth\Common\Storage\Exception\TokenNotFoundException'); |
107
|
|
|
$this->storage->retrieveAccessToken($service); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|