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\Redis; |
14
|
|
|
use Predis\Client as Predis; |
15
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
16
|
|
|
|
17
|
|
|
class RedisTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
protected $storage; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
// connect to a redis daemon |
24
|
|
|
$predis = new Predis(array( |
25
|
|
|
'host' => $_ENV['redis_host'], |
26
|
|
|
'port' => $_ENV['redis_port'], |
27
|
|
|
)); |
28
|
|
|
|
29
|
|
|
// set it |
30
|
|
|
$this->storage = new Redis($predis, 'test_user_token', 'test_user_state'); |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$predis->connect(); |
34
|
|
|
} catch (\Predis\Connection\ConnectionException $e) { |
35
|
|
|
$this->markTestSkipped('No redis instance available: ' . $e->getMessage()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function tearDown() |
40
|
|
|
{ |
41
|
|
|
// delete |
42
|
|
|
$this->storage->clearAllTokens(); |
43
|
|
|
|
44
|
|
|
// close connection |
45
|
|
|
$this->storage->getRedis()->quit(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check that the token gets properly stored. |
50
|
|
|
*/ |
51
|
|
|
public function testStorage() |
52
|
|
|
{ |
53
|
|
|
// arrange |
54
|
|
|
$service_1 = 'Facebook'; |
55
|
|
|
$service_2 = 'Foursquare'; |
56
|
|
|
|
57
|
|
|
$token_1 = new StdOAuth2Token('access_1', 'refresh_1', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param')); |
58
|
|
|
$token_2 = new StdOAuth2Token('access_2', 'refresh_2', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param')); |
59
|
|
|
|
60
|
|
|
// act |
61
|
|
|
$this->storage->storeAccessToken($service_1, $token_1); |
62
|
|
|
$this->storage->storeAccessToken($service_2, $token_2); |
63
|
|
|
|
64
|
|
|
// assert |
65
|
|
|
$extraParams = $this->storage->retrieveAccessToken($service_1)->getExtraParams(); |
66
|
|
|
$this->assertEquals('param', $extraParams['extra']); |
67
|
|
|
$this->assertEquals($token_1, $this->storage->retrieveAccessToken($service_1)); |
68
|
|
|
$this->assertEquals($token_2, $this->storage->retrieveAccessToken($service_2)); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Test hasAccessToken. |
73
|
|
|
*/ |
74
|
|
|
public function testHasAccessToken() |
75
|
|
|
{ |
76
|
|
|
// arrange |
77
|
|
|
$service = 'Facebook'; |
78
|
|
|
$this->storage->clearToken($service); |
79
|
|
|
|
80
|
|
|
// act |
81
|
|
|
// assert |
82
|
|
|
$this->assertFalse($this->storage->hasAccessToken($service)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Check that the token gets properly deleted. |
87
|
|
|
*/ |
88
|
|
|
public function testStorageClears() |
89
|
|
|
{ |
90
|
|
|
// arrange |
91
|
|
|
$service = 'Facebook'; |
92
|
|
|
$token = new StdOAuth2Token('access', 'refresh', StdOAuth2Token::EOL_NEVER_EXPIRES, array('extra' => 'param')); |
93
|
|
|
|
94
|
|
|
// act |
95
|
|
|
$this->storage->storeAccessToken($service, $token); |
96
|
|
|
$this->storage->clearToken($service); |
97
|
|
|
|
98
|
|
|
// assert |
99
|
|
|
$this->setExpectedException('OAuth\Common\Storage\Exception\TokenNotFoundException'); |
100
|
|
|
$this->storage->retrieveAccessToken($service); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|