TwitterTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 375
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 18
eloc 167
dl 0
loc 375
c 4
b 0
f 0
rs 10

18 Methods

Rating   Name   Duplication   Size   Complexity  
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue() 0 17 1
A testGetAuthorizationEndpoint() 0 22 1
A testParseAccessTokenErrorItsAnError() 0 14 1
A testParseRequestTokenResponseThrowsExceptionOnNulledResponse() 0 15 1
A testParseAccessTokenErrorItsMissingOauthTokenSecret() 0 14 1
A testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet() 0 15 1
A testSetAuthorizationEndpoint() 0 12 1
A testParseRequestTokenResponseValid() 0 15 1
A testConstructCorrectInstanceWithCustomUri() 0 11 1
A testConstructCorrectInstanceWithoutCustomUri() 0 10 1
A testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray() 0 15 1
A testGetAccessTokenEndpoint() 0 12 1
A testParseAccessTokenResponseValid() 0 20 1
A testConstructCorrectInterfaceWithoutCustomUri() 0 10 1
A testParseAccessTokenErrorItsMissingOauthToken() 0 14 1
A testGetRequestTokenEndpoint() 0 12 1
A testParseAccessTokenResponseThrowsExceptionOnError() 0 20 1
A testParseAccessTokenErrorTotalBullshit() 0 14 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuth\OAuth1\Service\Twitter;
6
use PHPUnit\Framework\TestCase;
7
use ReflectionMethod;
8
9
class TwitterTest extends TestCase
10
{
11
    /**
12
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
13
     */
14
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
15
    {
16
        $service = new Twitter(
17
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
20
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
21
        );
22
23
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
24
    }
25
26
    /**
27
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
28
     */
29
    public function testConstructCorrectInstanceWithoutCustomUri(): void
30
    {
31
        $service = new Twitter(
32
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
33
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
34
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
35
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
36
        );
37
38
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
39
    }
40
41
    /**
42
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
43
     */
44
    public function testConstructCorrectInstanceWithCustomUri(): void
45
    {
46
        $service = new Twitter(
47
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
48
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
49
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
50
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
51
            $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
52
        );
53
54
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\AbstractService', $service);
55
    }
56
57
    /**
58
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
59
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
60
     */
61
    public function testGetRequestTokenEndpoint(): void
62
    {
63
        $service = new Twitter(
64
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
65
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
66
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
67
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
68
        );
69
70
        self::assertSame(
71
            'https://api.twitter.com/oauth/request_token',
72
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
73
        );
74
    }
75
76
    /**
77
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
78
     * @covers \OAuth\OAuth1\Service\Twitter::getAuthorizationEndpoint
79
     */
80
    public function testGetAuthorizationEndpoint(): void
81
    {
82
        $service = new Twitter(
83
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
84
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
85
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
86
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
87
        );
88
89
        self::assertTrue(
90
            in_array(
91
                strtolower($service->getAuthorizationEndpoint()->getAbsoluteUri()),
92
                [\OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHENTICATE, \OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE]
93
            )
94
        );
95
96
        $service->setAuthorizationEndpoint(\OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE);
97
98
        self::assertTrue(
99
            in_array(
100
                strtolower($service->getAuthorizationEndpoint()->getAbsoluteUri()),
101
                [\OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHENTICATE, \OAuth\OAuth1\Service\Twitter::ENDPOINT_AUTHORIZE]
102
            )
103
        );
104
    }
105
106
    /**
107
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
108
     * @covers \OAuth\OAuth1\Service\Twitter::setAuthorizationEndpoint
109
     */
110
    public function testSetAuthorizationEndpoint(): void
111
    {
112
        $service = new Twitter(
113
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
114
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
115
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
116
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
117
        );
118
119
        $this->expectException('\\OAuth\\Common\\Exception\\Exception');
120
121
        $service->setAuthorizationEndpoint('foo');
122
    }
123
124
    /**
125
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
126
     * @covers \OAuth\OAuth1\Service\Twitter::getAccessTokenEndpoint
127
     */
128
    public function testGetAccessTokenEndpoint(): void
129
    {
130
        $service = new Twitter(
131
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
132
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
133
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
134
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
135
        );
136
137
        self::assertSame(
138
            'https://api.twitter.com/oauth/access_token',
139
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
140
        );
141
    }
142
143
    /**
144
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
145
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
146
     * @covers \OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse
147
     */
148
    public function testParseRequestTokenResponseThrowsExceptionOnNulledResponse(): void
149
    {
150
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
151
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
152
153
        $service = new Twitter(
154
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
155
            $client,
156
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
157
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
158
        );
159
160
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
161
162
        $service->requestRequestToken();
163
    }
164
165
    /**
166
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
167
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
168
     * @covers \OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse
169
     */
170
    public function testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray(): void
171
    {
172
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
173
        $client->expects(self::once())->method('retrieveResponse')->willReturn('notanarray');
174
175
        $service = new Twitter(
176
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
177
            $client,
178
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
179
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
180
        );
181
182
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
183
184
        $service->requestRequestToken();
185
    }
186
187
    /**
188
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
189
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
190
     * @covers \OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse
191
     */
192
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet(): void
193
    {
194
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
195
        $client->expects(self::once())->method('retrieveResponse')->willReturn('foo=bar');
196
197
        $service = new Twitter(
198
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
199
            $client,
200
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
201
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
202
        );
203
204
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
205
206
        $service->requestRequestToken();
207
    }
208
209
    /**
210
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
211
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
212
     * @covers \OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse
213
     */
214
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue(): void
215
    {
216
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
217
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
218
            'oauth_callback_confirmed=false'
219
        );
220
221
        $service = new Twitter(
222
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
223
            $client,
224
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
225
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
226
        );
227
228
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
229
230
        $service->requestRequestToken();
231
    }
232
233
    /**
234
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
235
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
236
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
237
     * @covers \OAuth\OAuth1\Service\Twitter::parseRequestTokenResponse
238
     */
239
    public function testParseRequestTokenResponseValid(): void
240
    {
241
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
242
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
243
            'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar'
244
        );
245
246
        $service = new Twitter(
247
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
248
            $client,
249
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
250
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
251
        );
252
253
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken());
254
    }
255
256
    /**
257
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
258
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
259
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
260
     */
261
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
262
    {
263
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
264
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=bar');
265
266
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
267
268
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
269
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
270
271
        $service = new Twitter(
272
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
273
            $client,
274
            $storage,
275
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
276
        );
277
278
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
279
280
        $service->requestAccessToken('foo', 'bar', $token);
281
    }
282
283
    /**
284
     * @covers \OAuth\OAuth1\Service\Twitter::__construct
285
     * @covers \OAuth\OAuth1\Service\Twitter::getRequestTokenEndpoint
286
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
287
     */
288
    public function testParseAccessTokenResponseValid(): void
289
    {
290
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
291
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
292
            'oauth_token=foo&oauth_token_secret=bar'
293
        );
294
295
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
296
297
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
298
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
299
300
        $service = new Twitter(
301
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
302
            $client,
303
            $storage,
304
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
305
        );
306
307
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
308
    }
309
310
    /**
311
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
312
     */
313
    public function testParseAccessTokenErrorTotalBullshit(): void
314
    {
315
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
316
        $service = new Twitter(
317
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
318
            $client,
319
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
320
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
321
        );
322
323
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
324
        $method = new ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
325
        $method->setAccessible(true);
326
        $method->invokeArgs($service, ['hoho']);
327
    }
328
329
    /**
330
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
331
     */
332
    public function testParseAccessTokenErrorItsAnError(): void
333
    {
334
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
335
        $service = new Twitter(
336
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
337
            $client,
338
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
339
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
340
        );
341
342
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
343
        $method = new ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
344
        $method->setAccessible(true);
345
        $method->invokeArgs($service, ['error=hihihaha']);
346
    }
347
348
    /**
349
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
350
     */
351
    public function testParseAccessTokenErrorItsMissingOauthToken(): void
352
    {
353
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
354
        $service = new Twitter(
355
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
356
            $client,
357
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
358
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
359
        );
360
361
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
362
        $method = new ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
363
        $method->setAccessible(true);
364
        $method->invokeArgs($service, ['oauth_token_secret=1']);
365
    }
366
367
    /**
368
     * @covers \OAuth\OAuth1\Service\Twitter::parseAccessTokenResponse
369
     */
370
    public function testParseAccessTokenErrorItsMissingOauthTokenSecret(): void
371
    {
372
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
373
        $service = new Twitter(
374
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
375
            $client,
376
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
377
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
378
        );
379
380
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
381
        $method = new ReflectionMethod(get_class($service), 'parseAccessTokenResponse');
382
        $method->setAccessible(true);
383
        $method->invokeArgs($service, ['oauth_token=1']);
384
    }
385
}
386