testParseAccessTokenResponseValidWithoutRefreshToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 12
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\Spotify;
7
use PHPUnit\Framework\Assert;
8
use PHPUnit\Framework\TestCase;
9
10
class SpotifyTest extends TestCase
11
{
12
    /**
13
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
14
     */
15
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
16
    {
17
        $service = new Spotify(
18
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
19
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
20
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
21
        );
22
23
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
24
    }
25
26
    /**
27
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
28
     */
29
    public function testConstructCorrectInstanceWithoutCustomUri(): void
30
    {
31
        $service = new Spotify(
32
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
33
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
34
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
35
        );
36
37
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
38
    }
39
40
    /**
41
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
42
     */
43
    public function testConstructCorrectInstanceWithCustomUri(): void
44
    {
45
        $service = new Spotify(
46
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
47
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
48
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
49
            [],
50
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
51
        );
52
53
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
54
    }
55
56
    /**
57
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
58
     * @covers \OAuth\OAuth2\Service\Spotify::getAuthorizationEndpoint
59
     */
60
    public function testGetAuthorizationEndpoint(): void
61
    {
62
        $service = new Spotify(
63
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
64
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
65
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
66
        );
67
68
        self::assertSame('https://accounts.spotify.com/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
69
    }
70
71
    /**
72
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
73
     * @covers \OAuth\OAuth2\Service\Spotify::getAccessTokenEndpoint
74
     */
75
    public function testGetAccessTokenEndpoint(): void
76
    {
77
        $service = new Spotify(
78
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
79
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
80
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
81
        );
82
83
        self::assertSame('https://accounts.spotify.com/api/token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
84
    }
85
86
    /**
87
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
88
     * @covers \OAuth\OAuth2\Service\Spotify::getAuthorizationMethod
89
     */
90
    public function testGetAuthorizationMethod(): void
91
    {
92
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
93
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2);
94
95
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
96
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
97
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
98
99
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
100
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
101
102
        $service = new Spotify(
103
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
104
            $client,
105
            $storage
106
        );
107
108
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
109
110
        self::assertArrayHasKey('Authorization', $headers);
0 ignored issues
show
Bug introduced by
$headers of type string is incompatible with the type ArrayAccess|array expected by parameter $array of PHPUnit\Framework\Assert::assertArrayHasKey(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

110
        self::assertArrayHasKey('Authorization', /** @scrutinizer ignore-type */ $headers);
Loading history...
111
        self::assertTrue(in_array('Bearer foo', $headers, true));
0 ignored issues
show
Bug introduced by
$headers of type string is incompatible with the type array expected by parameter $haystack of in_array(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

111
        self::assertTrue(in_array('Bearer foo', /** @scrutinizer ignore-type */ $headers, true));
Loading history...
112
    }
113
114
    /**
115
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
116
     * @covers \OAuth\OAuth2\Service\Spotify::parseAccessTokenResponse
117
     */
118
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
119
    {
120
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
121
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
122
123
        $service = new Spotify(
124
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
125
            $client,
126
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
127
        );
128
129
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
130
131
        $service->requestAccessToken('foo');
132
    }
133
134
    /**
135
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
136
     * @covers \OAuth\OAuth2\Service\Spotify::parseAccessTokenResponse
137
     */
138
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
139
    {
140
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
141
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
142
143
        $service = new Spotify(
144
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
145
            $client,
146
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
147
        );
148
149
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
150
151
        $service->requestAccessToken('foo');
152
    }
153
154
    /**
155
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
156
     * @covers \OAuth\OAuth2\Service\Spotify::parseAccessTokenResponse
157
     */
158
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
159
    {
160
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
161
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
162
163
        $service = new Spotify(
164
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
165
            $client,
166
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
167
        );
168
169
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
170
    }
171
172
    /**
173
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
174
     * @covers \OAuth\OAuth2\Service\Spotify::parseAccessTokenResponse
175
     */
176
    public function testParseAccessTokenResponseValidWithRefreshToken(): void
177
    {
178
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
179
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}');
180
181
        $service = new Spotify(
182
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
183
            $client,
184
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
185
        );
186
187
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
188
    }
189
190
    /**
191
     * @covers \OAuth\OAuth2\Service\Spotify::__construct
192
     * @covers \OAuth\OAuth2\Service\Spotify::getExtraOAuthHeaders
193
     */
194
    public function testGetExtraOAuthHeaders(): void
195
    {
196
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
197
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($uri, $params, $extraHeaders) {
198
            Assert::assertTrue(array_key_exists('Authorization', $extraHeaders));
199
            Assert::assertSame('Basic ' . base64_encode('foo:bar'), $extraHeaders['Authorization']);
200
201
            return '{"access_token":"foo","expires_in":"bar"}';
202
        });
203
204
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
205
        $credentials->expects(self::any())->method('getConsumerId')->willReturn('foo');
206
        $credentials->expects(self::any())->method('getConsumerSecret')->willReturn('bar');
207
208
        $service = new Spotify(
209
            $credentials,
210
            $client,
211
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
212
        );
213
214
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
215
    }
216
}
217