RedisTest::testStorage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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