Completed
Pull Request — master (#479)
by Andrey
03:26
created

EtsyTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 280
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 280
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInterfaceWithoutCustomUri() 0 11 1
A testConstructCorrectInstanceWithoutCustomUri() 0 11 1
A testConstructCorrectInstanceWithCustomUri() 0 12 1
A testGetRequestTokenEndpoint() 0 22 1
A testGetAuthorizationEndpoint() 0 14 1
A testGetAccessTokenEndpoint() 0 14 1
A testParseRequestTokenResponseThrowsExceptionOnNulledResponse() 0 16 1
A testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray() 0 16 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet() 0 16 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue() 0 18 1
A testParseRequestTokenResponseValid() 0 16 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 21 1
A testParseAccessTokenResponseValid() 0 21 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuth\OAuth1\Service\Etsy;
6
7
class EtsyTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @covers OAuth\OAuth1\Service\Etsy::__construct
11
     */
12
    public function testConstructCorrectInterfaceWithoutCustomUri()
13
    {
14
        $service = new Etsy(
15
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
16
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
17
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
18
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
19
        );
20
21
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
22
    }
23
24
    /**
25
     * @covers OAuth\OAuth1\Service\Etsy::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new Etsy(
30
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
31
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
32
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
33
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
34
        );
35
36
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
37
    }
38
39
    /**
40
     * @covers OAuth\OAuth1\Service\Etsy::__construct
41
     */
42
    public function testConstructCorrectInstanceWithCustomUri()
43
    {
44
        $service = new Etsy(
45
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
46
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
47
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
48
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
49
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
50
        );
51
52
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
53
    }
54
55
    /**
56
     * @covers OAuth\OAuth1\Service\Etsy::__construct
57
     * @covers OAuth\OAuth1\Service\Etsy::getRequestTokenEndpoint
58
     */
59
    public function testGetRequestTokenEndpoint()
60
    {
61
        $service = new Etsy(
62
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
63
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
64
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
65
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
66
        );
67
68
        $this->assertSame(
69
            'https://openapi.etsy.com/v2/oauth/request_token',
70
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
71
        );
72
73
		$service->setScopes(array('email_r', 'cart_rw'));
74
75
        $this->assertSame(
76
            'https://openapi.etsy.com/v2/oauth/request_token?scope=email_r%20cart_rw',
77
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
78
        );
79
80
    }
81
82
    /**
83
     * @covers OAuth\OAuth1\Service\Etsy::__construct
84
     * @covers OAuth\OAuth1\Service\Etsy::getAuthorizationEndpoint
85
     */
86
    public function testGetAuthorizationEndpoint()
87
    {
88
        $service = new Etsy(
89
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
90
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
91
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
92
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
93
        );
94
95
        $this->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()
106
    {
107
        $service = new Etsy(
108
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
109
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
110
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
111
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
112
        );
113
114
        $this->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()
126
    {
127
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
128
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
129
130
        $service = new Etsy(
131
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
132
            $client,
133
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
134
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
135
        );
136
137
        $this->setExpectedException('\\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()
148
    {
149
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
150
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('notanarray'));
151
152
        $service = new Etsy(
153
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
154
            $client,
155
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
156
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
157
        );
158
159
        $this->setExpectedException('\\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()
170
    {
171
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
172
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('foo=bar'));
173
174
        $service = new Etsy(
175
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
176
            $client,
177
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
178
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
179
        );
180
181
        $this->setExpectedException('\\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()
192
    {
193
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
194
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(
195
            'oauth_callback_confirmed=false'
196
        ));
197
198
        $service = new Etsy(
199
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
200
            $client,
201
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
202
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
203
        );
204
205
        $this->setExpectedException('\\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::parseRequestTokenResponse
214
     * @covers OAuth\OAuth1\Service\Etsy::parseAccessTokenResponse
215
     */
216
    public function testParseRequestTokenResponseValid()
217
    {
218
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
219
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(
220
            'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar'
221
        ));
222
223
        $service = new Etsy(
224
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
225
            $client,
226
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
227
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
228
        );
229
230
        $this->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()
239
    {
240
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
241
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=bar'));
242
243
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
244
245
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
246
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
247
248
        $service = new Etsy(
249
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
250
            $client,
251
            $storage,
252
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
253
        );
254
255
        $this->setExpectedException('\\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()
266
    {
267
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
268
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(
269
            'oauth_token=foo&oauth_token_secret=bar'
270
        ));
271
272
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
273
274
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
275
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
276
277
        $service = new Etsy(
278
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
279
            $client,
280
            $storage,
281
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
282
        );
283
284
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
285
    }
286
}
287