DropboxTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
eloc 91
dl 0
loc 222
c 1
b 0
f 0
rs 10

12 Methods

Rating   Name   Duplication   Size   Complexity  
A testGetAuthorizationEndpoint() 0 9 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 14 1
A testGetAuthorizationUriWithoutAdditionalParams() 0 15 1
A testGetAccessTokenEndpoint() 0 9 1
A testParseAccessTokenResponseValidWithRefreshToken() 0 12 1
A testConstructCorrectInstanceWithoutCustomUri() 0 9 1
A testGetAuthorizationMethod() 0 22 1
A testParseAccessTokenResponseValidWithoutRefreshToken() 0 12 1
A testConstructCorrectInterfaceWithoutCustomUri() 0 9 1
A testParseAccessTokenResponseThrowsExceptionOnNulledResponse() 0 14 1
A testGetAuthorizationUriWithAdditionalParams() 0 15 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\Dropbox;
7
use PHPUnit\Framework\TestCase;
8
9
class DropboxTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Dropbox(
17
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
20
        );
21
22
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
23
    }
24
25
    /**
26
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Dropbox(
31
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
32
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
33
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
34
        );
35
36
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
37
    }
38
39
    /**
40
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri(): void
43
    {
44
        $service = new Dropbox(
45
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
46
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
47
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
48
            [],
49
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
50
        );
51
52
        self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
53
    }
54
55
    /**
56
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
57
     * @covers \OAuth\OAuth2\Service\Dropbox::getAuthorizationUri
58
     */
59
    public function testGetAuthorizationUriWithoutAdditionalParams(): void
60
    {
61
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
62
        $credentials->expects(self::once())->method('getConsumerId')->willReturn('foo');
63
        $credentials->expects(self::once())->method('getCallbackUrl')->willReturn('bar');
64
65
        $service = new Dropbox(
66
            $credentials,
67
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
68
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
69
        );
70
71
        self::assertSame(
72
            'https://www.dropbox.com/1/oauth2/authorize?client_id=foo&redirect_uri=bar&response_type=code&scope=',
73
            $service->getAuthorizationUri()->getAbsoluteUri()
74
        );
75
    }
76
77
    /**
78
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
79
     * @covers \OAuth\OAuth2\Service\Dropbox::getAuthorizationUri
80
     */
81
    public function testGetAuthorizationUriWithAdditionalParams(): void
82
    {
83
        $credentials = $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
84
        $credentials->expects(self::once())->method('getConsumerId')->willReturn('foo');
85
        $credentials->expects(self::once())->method('getCallbackUrl')->willReturn('bar');
86
87
        $service = new Dropbox(
88
            $credentials,
89
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
90
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
91
        );
92
93
        self::assertSame(
94
            'https://www.dropbox.com/1/oauth2/authorize?client_id=foo&redirect_uri=bar&response_type=code&scope=',
95
            $service->getAuthorizationUri()->getAbsoluteUri()
96
        );
97
    }
98
99
    /**
100
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
101
     * @covers \OAuth\OAuth2\Service\Dropbox::getAuthorizationEndpoint
102
     */
103
    public function testGetAuthorizationEndpoint(): void
104
    {
105
        $service = new Dropbox(
106
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
107
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
108
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
109
        );
110
111
        self::assertSame('https://www.dropbox.com/1/oauth2/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
112
    }
113
114
    /**
115
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
116
     * @covers \OAuth\OAuth2\Service\Dropbox::getAccessTokenEndpoint
117
     */
118
    public function testGetAccessTokenEndpoint(): void
119
    {
120
        $service = new Dropbox(
121
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
122
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
123
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
124
        );
125
126
        self::assertSame('https://api.dropbox.com/1/oauth2/token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
127
    }
128
129
    /**
130
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
131
     * @covers \OAuth\OAuth2\Service\Dropbox::getAuthorizationMethod
132
     */
133
    public function testGetAuthorizationMethod(): void
134
    {
135
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
136
        $client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0);
137
138
        $token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
139
        $token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES);
140
        $token->expects(self::once())->method('getAccessToken')->willReturn('foo');
141
142
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
143
        $storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token);
144
145
        $service = new Dropbox(
146
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
147
            $client,
148
            $storage
149
        );
150
151
        $uri = $service->request('https://pieterhordijk.com/my/awesome/path');
152
        $absoluteUri = parse_url($uri->getAbsoluteUri());
153
154
        self::assertSame('access_token=foo', $absoluteUri['query']);
155
    }
156
157
    /**
158
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
159
     * @covers \OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
160
     */
161
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void
162
    {
163
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
164
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
165
166
        $service = new Dropbox(
167
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
168
            $client,
169
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
170
        );
171
172
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
173
174
        $service->requestAccessToken('foo');
175
    }
176
177
    /**
178
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
179
     * @covers \OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
180
     */
181
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
182
    {
183
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
184
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error');
185
186
        $service = new Dropbox(
187
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
188
            $client,
189
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
190
        );
191
192
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
193
194
        $service->requestAccessToken('foo');
195
    }
196
197
    /**
198
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
199
     * @covers \OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
200
     */
201
    public function testParseAccessTokenResponseValidWithoutRefreshToken(): void
202
    {
203
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
204
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}');
205
206
        $service = new Dropbox(
207
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
208
            $client,
209
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
210
        );
211
212
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
213
    }
214
215
    /**
216
     * @covers \OAuth\OAuth2\Service\Dropbox::__construct
217
     * @covers \OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
218
     */
219
    public function testParseAccessTokenResponseValidWithRefreshToken(): void
220
    {
221
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
222
        $client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}');
223
224
        $service = new Dropbox(
225
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
226
            $client,
227
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
228
        );
229
230
        self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
231
    }
232
}
233