EtsyTest   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 13
eloc 118
dl 0
loc 277
c 4
b 0
f 0
rs 10

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue() 0 17 1
A testGetAccessTokenEndpoint() 0 12 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet() 0 15 1
A testConstructCorrectInstanceWithoutCustomUri() 0 10 1
A testGetAuthorizationEndpoint() 0 12 1
A testConstructCorrectInstanceWithCustomUri() 0 11 1
A testParseAccessTokenResponseValid() 0 20 1
A testParseRequestTokenResponseValid() 0 15 1
A testConstructCorrectInterfaceWithoutCustomUri() 0 10 1
A testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray() 0 15 1
A testParseRequestTokenResponseThrowsExceptionOnNulledResponse() 0 15 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 20 1
A testGetRequestTokenEndpoint() 0 19 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuth\OAuth1\Service\Etsy;
6
use PHPUnit\Framework\TestCase;
7
8
class EtsyTest extends TestCase
9
{
10
    /**
11
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
14
    {
15
        $service = new Etsy(
16
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
19
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
20
        );
21
22
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
23
    }
24
25
    /**
26
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new Etsy(
31
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
32
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
33
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
34
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
35
        );
36
37
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
38
    }
39
40
    /**
41
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
42
     */
43
    public function testConstructCorrectInstanceWithCustomUri(): void
44
    {
45
        $service = new Etsy(
46
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
47
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
48
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
49
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
50
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
51
        );
52
53
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
54
    }
55
56
    /**
57
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
58
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
59
     */
60
    public function testGetRequestTokenEndpoint(): void
61
    {
62
        $service = new Etsy(
63
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
64
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
65
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
66
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
67
        );
68
69
        self::assertSame(
70
            'https://openapi.etsy.com/v2/oauth/request_token',
71
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
72
        );
73
74
        $service->setScopes(['email_r', 'cart_rw']);
75
76
        self::assertSame(
77
            'https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20cart_rw',
78
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
79
        );
80
    }
81
82
    /**
83
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
84
     * @covers \OAuth\OAuth1\Service\Etsy::getAuthorizationEndpoint
85
     */
86
    public function testGetAuthorizationEndpoint(): void
87
    {
88
        $service = new Etsy(
89
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
90
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
91
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
92
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
93
        );
94
95
        self::assertSame(
96
            'https://openapi.etsy.com/v2/',
97
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
98
        );
99
    }
100
101
    /**
102
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
103
     * @covers \OAuth\OAuth1\Service\Etsy::getAccessTokenEndpoint
104
     */
105
    public function testGetAccessTokenEndpoint(): void
106
    {
107
        $service = new Etsy(
108
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
109
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
110
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
111
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
112
        );
113
114
        self::assertSame(
115
            'https://openapi.etsy.com/v2/oauth/access_token',
116
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
117
        );
118
    }
119
120
    /**
121
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
122
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
123
     * @covers \OAuth\OAuth1\Service\Etsy::parseRequestTokenResponse
124
     */
125
    public function testParseRequestTokenResponseThrowsExceptionOnNulledResponse(): void
126
    {
127
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
128
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
129
130
        $service = new Etsy(
131
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
132
            $client,
133
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
134
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
135
        );
136
137
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
138
139
        $service->requestRequestToken();
140
    }
141
142
    /**
143
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
144
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
145
     * @covers \OAuth\OAuth1\Service\Etsy::parseRequestTokenResponse
146
     */
147
    public function testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray(): void
148
    {
149
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
150
        $client->expects(self::once())->method('retrieveResponse')->willReturn('notanarray');
151
152
        $service = new Etsy(
153
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
154
            $client,
155
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
156
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
157
        );
158
159
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
160
161
        $service->requestRequestToken();
162
    }
163
164
    /**
165
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
166
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
167
     * @covers \OAuth\OAuth1\Service\Etsy::parseRequestTokenResponse
168
     */
169
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet(): void
170
    {
171
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
172
        $client->expects(self::once())->method('retrieveResponse')->willReturn('foo=bar');
173
174
        $service = new Etsy(
175
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
176
            $client,
177
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
178
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
179
        );
180
181
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
182
183
        $service->requestRequestToken();
184
    }
185
186
    /**
187
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
188
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
189
     * @covers \OAuth\OAuth1\Service\Etsy::parseRequestTokenResponse
190
     */
191
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue(): void
192
    {
193
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
194
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
195
            'oauth_callback_confirmed=false'
196
        );
197
198
        $service = new Etsy(
199
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
200
            $client,
201
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
202
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
203
        );
204
205
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
206
207
        $service->requestRequestToken();
208
    }
209
210
    /**
211
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
212
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
213
     * @covers \OAuth\OAuth1\Service\Etsy::parseAccessTokenResponse
214
     * @covers \OAuth\OAuth1\Service\Etsy::parseRequestTokenResponse
215
     */
216
    public function testParseRequestTokenResponseValid(): void
217
    {
218
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
219
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
220
            'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar'
221
        );
222
223
        $service = new Etsy(
224
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
225
            $client,
226
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
227
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
228
        );
229
230
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken());
231
    }
232
233
    /**
234
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
235
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
236
     * @covers \OAuth\OAuth1\Service\Etsy::parseAccessTokenResponse
237
     */
238
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
239
    {
240
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
241
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=bar');
242
243
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
244
245
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
246
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
247
248
        $service = new Etsy(
249
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
250
            $client,
251
            $storage,
252
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
253
        );
254
255
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
256
257
        $service->requestAccessToken('foo', 'bar', $token);
258
    }
259
260
    /**
261
     * @covers \OAuth\OAuth1\Service\Etsy::__construct
262
     * @covers \OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
263
     * @covers \OAuth\OAuth1\Service\Etsy::parseAccessTokenResponse
264
     */
265
    public function testParseAccessTokenResponseValid(): void
266
    {
267
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
268
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
269
            'oauth_token=foo&oauth_token_secret=bar'
270
        );
271
272
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
273
274
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
275
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
276
277
        $service = new Etsy(
278
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
279
            $client,
280
            $storage,
281
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
282
        );
283
284
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
285
    }
286
}
287