AbstractServiceTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 391
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 17
eloc 154
dl 0
loc 391
c 1
b 0
f 0
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A testRequestOauthAuthorizationMethod() 0 22 1
A testConstructThrowsExceptionOnInvalidScope() 0 9 1
A testRequestBearerMethod() 0 24 1
A testRefreshAccessTokenSuccess() 0 12 1
A testRequestQueryStringMethod() 0 24 1
A testConstructCorrectParent() 0 13 1
A testConstructCorrectInterface() 0 13 1
A testIsValidScopeFalse() 0 9 1
A testGetAuthorizationUriWithParametersAndScopes() 0 16 1
A testRequestThrowsExceptionWhenTokenIsExpired() 0 19 1
A testRequestAccessToken() 0 9 1
A testGetAuthorizationUriWithParametersWithoutScopes() 0 15 1
A testGetStorage() 0 9 1
A testIsValidScopeTrue() 0 9 1
A testRequestQueryStringTwoMethod() 0 24 1
A testConstructCorrectParentCustomUri() 0 14 1
A testGetAuthorizationUriWithoutParametersOrScopes() 0 15 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use DateTime;
6
use OAuth\Common\Token\TokenInterface;
7
use OAuthTest\Mocks\OAuth2\Service\Mock;
8
use PHPUnit\Framework\TestCase;
9
10
class AbstractServiceTest extends TestCase
11
{
12
    /**
13
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
14
     */
15
    public function testConstructCorrectInterface(): void
16
    {
17
        $service = $this->getMockForAbstractClass(
18
            '\\OAuth\\OAuth2\\Service\\AbstractService',
19
            [
20
                $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
21
                $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
22
                $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
23
                [],
24
            ]
25
        );
26
27
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
28
    }
29
30
    /**
31
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
32
     */
33
    public function testConstructCorrectParent(): void
34
    {
35
        $service = $this->getMockForAbstractClass(
36
            '\\OAuth\\OAuth2\\Service\\AbstractService',
37
            [
38
                $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
39
                $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
40
                $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
41
                [],
42
            ]
43
        );
44
45
        self::assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service);
46
    }
47
48
    /**
49
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
50
     */
51
    public function testConstructCorrectParentCustomUri(): void
52
    {
53
        $service = $this->getMockForAbstractClass(
54
            '\\OAuth\\OAuth2\\Service\\AbstractService',
55
            [
56
                $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
57
                $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
58
                $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
59
                [],
60
                $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
61
            ]
62
        );
63
64
        self::assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service);
65
    }
66
67
    /**
68
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
69
     * @covers \OAuth\OAuth2\Service\AbstractService::isValidScope
70
     */
71
    public function testConstructThrowsExceptionOnInvalidScope(): void
72
    {
73
        $this->expectException('\\OAuth\\OAuth2\\Service\\Exception\\InvalidScopeException');
74
75
        $service = new Mock(
0 ignored issues
show
Unused Code introduced by
The assignment to $service is dead and can be removed.
Loading history...
76
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
77
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
78
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
79
            ['invalidscope']
80
        );
81
    }
82
83
    /**
84
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
85
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint
86
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationUri
87
     */
88
    public function testGetAuthorizationUriWithoutParametersOrScopes(): void
89
    {
90
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
91
        $credentials->expects(self::once())->method('getConsumerId')->willReturn('foo');
92
        $credentials->expects(self::once())->method('getCallbackUrl')->willReturn('bar');
93
94
        $service = new Mock(
95
            $credentials,
96
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
97
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
98
        );
99
100
        self::assertSame(
101
            'http://pieterhordijk.com/auth?type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=',
102
            $service->getAuthorizationUri()->getAbsoluteUri()
103
        );
104
    }
105
106
    /**
107
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
108
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint
109
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationUri
110
     */
111
    public function testGetAuthorizationUriWithParametersWithoutScopes(): void
112
    {
113
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
114
        $credentials->expects(self::once())->method('getConsumerId')->willReturn('foo');
115
        $credentials->expects(self::once())->method('getCallbackUrl')->willReturn('bar');
116
117
        $service = new Mock(
118
            $credentials,
119
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
120
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
121
        );
122
123
        self::assertSame(
124
            'http://pieterhordijk.com/auth?foo=bar&baz=beer&type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=',
125
            $service->getAuthorizationUri(['foo' => 'bar', 'baz' => 'beer'])->getAbsoluteUri()
126
        );
127
    }
128
129
    /**
130
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
131
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint
132
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationUri
133
     * @covers \OAuth\OAuth2\Service\AbstractService::isValidScope
134
     */
135
    public function testGetAuthorizationUriWithParametersAndScopes(): void
136
    {
137
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
138
        $credentials->expects(self::once())->method('getConsumerId')->willReturn('foo');
139
        $credentials->expects(self::once())->method('getCallbackUrl')->willReturn('bar');
140
141
        $service = new Mock(
142
            $credentials,
143
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
144
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
145
            ['mock', 'mock2']
146
        );
147
148
        self::assertSame(
149
            'http://pieterhordijk.com/auth?foo=bar&baz=beer&type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=mock+mock2',
150
            $service->getAuthorizationUri(['foo' => 'bar', 'baz' => 'beer'])->getAbsoluteUri()
151
        );
152
    }
153
154
    /**
155
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
156
     * @covers \OAuth\OAuth2\Service\AbstractService::getAccessTokenEndpoint
157
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraOAuthHeaders
158
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
159
     * @covers \OAuth\OAuth2\Service\AbstractService::requestAccessToken
160
     * @covers \OAuth\OAuth2\Service\AbstractService::service
161
     */
162
    public function testRequestAccessToken(): void
163
    {
164
        $service = new Mock(
165
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
166
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
167
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
168
        );
169
170
        $this->assertInstanceof('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('code'));
171
    }
172
173
    /**
174
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
175
     * @covers \OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
176
     * @covers \OAuth\OAuth2\Service\AbstractService::request
177
     */
178
    public function testRequestThrowsExceptionWhenTokenIsExpired(): void
179
    {
180
        $tokenExpiration = new DateTime('26-03-1984 00:00:00');
181
182
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
183
        $token->expects(self::any())->method('getEndOfLife')->willReturn($tokenExpiration->format('U'));
184
185
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
186
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
187
188
        $service = new Mock(
189
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
190
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
191
            $storage
192
        );
193
194
        $this->expectException('\\OAuth\\Common\\Token\\Exception\\ExpiredTokenException', 'Token expired on 03/26/1984 at 12:00:00 AM');
0 ignored issues
show
Unused Code introduced by
The call to PHPUnit\Framework\TestCase::expectException() has too many arguments starting with 'Token expired on 03/26/1984 at 12:00:00 AM'. ( Ignorable by Annotation )

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

194
        $this->/** @scrutinizer ignore-call */ 
195
               expectException('\\OAuth\\Common\\Token\\Exception\\ExpiredTokenException', 'Token expired on 03/26/1984 at 12:00:00 AM');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
195
196
        $service->request('https://pieterhordijk.com/my/awesome/path');
197
    }
198
199
    /**
200
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
201
     * @covers \OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
202
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod
203
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders
204
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
205
     * @covers \OAuth\OAuth2\Service\AbstractService::request
206
     * @covers \OAuth\OAuth2\Service\AbstractService::service
207
     */
208
    public function testRequestOauthAuthorizationMethod(): void
209
    {
210
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
211
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2);
212
213
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
214
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
215
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
216
217
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
218
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
219
220
        $service = new Mock(
221
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
222
            $client,
223
            $storage
224
        );
225
226
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
227
228
        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

228
        self::assertArrayHasKey('Authorization', /** @scrutinizer ignore-type */ $headers);
Loading history...
229
        self::assertTrue(in_array('OAuth 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

229
        self::assertTrue(in_array('OAuth foo', /** @scrutinizer ignore-type */ $headers, true));
Loading history...
230
    }
231
232
    /**
233
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
234
     * @covers \OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
235
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod
236
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders
237
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
238
     * @covers \OAuth\OAuth2\Service\AbstractService::request
239
     * @covers \OAuth\OAuth2\Service\AbstractService::service
240
     */
241
    public function testRequestQueryStringMethod(): void
242
    {
243
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
244
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
245
246
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
247
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
248
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
249
250
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
251
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
252
253
        $service = new Mock(
254
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
255
            $client,
256
            $storage
257
        );
258
259
        $service->setAuthorizationMethod('querystring');
260
261
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
262
        $absoluteUri = parse_url($uri->getAbsoluteUri());
263
264
        self::assertSame('access_token=foo', $absoluteUri['query']);
265
    }
266
267
    /**
268
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
269
     * @covers \OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
270
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod
271
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders
272
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
273
     * @covers \OAuth\OAuth2\Service\AbstractService::request
274
     * @covers \OAuth\OAuth2\Service\AbstractService::service
275
     */
276
    public function testRequestQueryStringTwoMethod(): void
277
    {
278
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
279
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
280
281
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
282
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
283
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
284
285
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
286
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
287
288
        $service = new Mock(
289
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
290
            $client,
291
            $storage
292
        );
293
294
        $service->setAuthorizationMethod('querystring2');
295
296
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
297
        $absoluteUri = parse_url($uri->getAbsoluteUri());
298
299
        self::assertSame('oauth2_access_token=foo', $absoluteUri['query']);
300
    }
301
302
    /**
303
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
304
     * @covers \OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath
305
     * @covers \OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod
306
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders
307
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
308
     * @covers \OAuth\OAuth2\Service\AbstractService::request
309
     * @covers \OAuth\OAuth2\Service\AbstractService::service
310
     */
311
    public function testRequestBearerMethod(): void
312
    {
313
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
314
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2);
315
316
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
317
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
318
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
319
320
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
321
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
322
323
        $service = new Mock(
324
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
325
            $client,
326
            $storage
327
        );
328
329
        $service->setAuthorizationMethod('bearer');
330
331
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
332
333
        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

333
        self::assertArrayHasKey('Authorization', /** @scrutinizer ignore-type */ $headers);
Loading history...
334
        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

334
        self::assertTrue(in_array('Bearer foo', /** @scrutinizer ignore-type */ $headers, true));
Loading history...
335
    }
336
337
    /**
338
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
339
     * @covers \OAuth\OAuth2\Service\AbstractService::getStorage
340
     */
341
    public function testGetStorage(): void
342
    {
343
        $service = new Mock(
344
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
345
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
346
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
347
        );
348
349
        self::assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $service->getStorage());
350
    }
351
352
    /**
353
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
354
     * @covers \OAuth\OAuth2\Service\AbstractService::getAccessTokenEndpoint
355
     * @covers \OAuth\OAuth2\Service\AbstractService::getExtraOAuthHeaders
356
     * @covers \OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse
357
     * @covers \OAuth\OAuth2\Service\AbstractService::refreshAccessToken
358
     */
359
    public function testRefreshAccessTokenSuccess(): void
360
    {
361
        $service = new Mock(
362
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
363
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
364
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
365
        );
366
367
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token');
368
        $token->expects(self::once())->method('getRefreshToken')->willReturn('foo');
369
370
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->refreshAccessToken($token));
371
    }
372
373
    /**
374
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
375
     * @covers \OAuth\OAuth2\Service\AbstractService::isValidScope
376
     */
377
    public function testIsValidScopeTrue(): void
378
    {
379
        $service = new Mock(
380
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
381
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
382
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
383
        );
384
385
        self::assertTrue($service->isValidScope('mock'));
386
    }
387
388
    /**
389
     * @covers \OAuth\OAuth2\Service\AbstractService::__construct
390
     * @covers \OAuth\OAuth2\Service\AbstractService::isValidScope
391
     */
392
    public function testIsValidScopeFalse(): void
393
    {
394
        $service = new Mock(
395
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
396
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
397
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
398
        );
399
400
        self::assertFalse($service->isValidScope('invalid'));
401
    }
402
}
403