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