GitHubTest   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 11
eloc 88
dl 0
loc 210
c 3
b 0
f 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 12 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 14 1
A testGetExtraApiHeaders() 0 22 1
A testGetExtraOAuthHeaders() 0 17 1
A testGetAuthorizationMethod() 0 21 1
A testGetAuthorizationEndpoint() 0 9 1
A testConstructCorrectInstanceWithoutCustomUri() 0 9 1
A testConstructCorrectInterfaceWithoutCustomUri() 0 9 1
A testGetAccessTokenEndpoint() 0 9 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 14 1
A testConstructCorrectInstanceWithCustomUri() 0 11 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\GitHub;
7
use PHPUnit\Framework\Assert;
8
use PHPUnit\Framework\TestCase;
9
10
class GitHubTest extends TestCase
11
{
12
    /**
13
     * @covers \OAuth\OAuth2\Service\GitHub::__construct
14
     */
15
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
16
    {
17
        $service = new GitHub(
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\GitHub::__construct
28
     */
29
    public function testConstructCorrectInstanceWithoutCustomUri(): void
30
    {
31
        $service = new GitHub(
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\GitHub::__construct
42
     */
43
    public function testConstructCorrectInstanceWithCustomUri(): void
44
    {
45
        $service = new GitHub(
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\GitHub::__construct
58
     * @covers \OAuth\OAuth2\Service\GitHub::getAuthorizationEndpoint
59
     */
60
    public function testGetAuthorizationEndpoint(): void
61
    {
62
        $service = new GitHub(
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://github.com/login/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
69
    }
70
71
    /**
72
     * @covers \OAuth\OAuth2\Service\GitHub::__construct
73
     * @covers \OAuth\OAuth2\Service\GitHub::getAccessTokenEndpoint
74
     */
75
    public function testGetAccessTokenEndpoint(): void
76
    {
77
        $service = new GitHub(
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://github.com/login/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
84
    }
85
86
    /**
87
     * @covers \OAuth\OAuth2\Service\GitHub::__construct
88
     * @covers \OAuth\OAuth2\Service\GitHub::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 GitHub(
103
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
104
            $client,
105
            $storage
106
        );
107
108
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
109
        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

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

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

218
        self::assertArrayHasKey('Accept', /** @scrutinizer ignore-type */ $headers);
Loading history...
219
        self::assertSame('application/vnd.github.v3+json', $headers['Accept']);
220
    }
221
}
222