testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
dl 0
loc 17
c 1
b 0
f 0
rs 9.9332
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuth\OAuth1\Service\BitBucket;
6
use PHPUnit\Framework\TestCase;
7
8
class BitBucketTest extends TestCase
9
{
10
    /**
11
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri(): void
14
    {
15
        $service = new BitBucket(
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\BitBucket::__construct
27
     */
28
    public function testConstructCorrectInstanceWithoutCustomUri(): void
29
    {
30
        $service = new BitBucket(
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\BitBucket::__construct
42
     */
43
    public function testConstructCorrectInstanceWithCustomUri(): void
44
    {
45
        $service = new BitBucket(
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\BitBucket::__construct
58
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
59
     */
60
    public function testGetRequestTokenEndpoint(): void
61
    {
62
        $service = new BitBucket(
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://bitbucket.org/!api/1.0/oauth/request_token',
71
            $service->getRequestTokenEndpoint()->getAbsoluteUri()
72
        );
73
    }
74
75
    /**
76
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
77
     * @covers \OAuth\OAuth1\Service\BitBucket::getAuthorizationEndpoint
78
     */
79
    public function testGetAuthorizationEndpoint(): void
80
    {
81
        $service = new BitBucket(
82
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
83
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
84
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
85
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
86
        );
87
88
        self::assertSame(
89
            'https://bitbucket.org/!api/1.0/oauth/authenticate',
90
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
91
        );
92
    }
93
94
    /**
95
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
96
     * @covers \OAuth\OAuth1\Service\BitBucket::getAccessTokenEndpoint
97
     */
98
    public function testGetAccessTokenEndpoint(): void
99
    {
100
        $service = new BitBucket(
101
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
102
            $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
103
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
104
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
105
        );
106
107
        self::assertSame(
108
            'https://bitbucket.org/!api/1.0/oauth/access_token',
109
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
110
        );
111
    }
112
113
    /**
114
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
115
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
116
     * @covers \OAuth\OAuth1\Service\BitBucket::parseRequestTokenResponse
117
     */
118
    public function testParseRequestTokenResponseThrowsExceptionOnNulledResponse(): void
119
    {
120
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
121
        $client->expects(self::once())->method('retrieveResponse')->willReturn(null);
122
123
        $service = new BitBucket(
124
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
125
            $client,
126
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
127
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
128
        );
129
130
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
131
132
        $service->requestRequestToken();
133
    }
134
135
    /**
136
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
137
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
138
     * @covers \OAuth\OAuth1\Service\BitBucket::parseRequestTokenResponse
139
     */
140
    public function testParseRequestTokenResponseThrowsExceptionOnResponseNotAnArray(): void
141
    {
142
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
143
        $client->expects(self::once())->method('retrieveResponse')->willReturn('notanarray');
144
145
        $service = new BitBucket(
146
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
147
            $client,
148
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
149
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
150
        );
151
152
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
153
154
        $service->requestRequestToken();
155
    }
156
157
    /**
158
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
159
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
160
     * @covers \OAuth\OAuth1\Service\BitBucket::parseRequestTokenResponse
161
     */
162
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotSet(): void
163
    {
164
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
165
        $client->expects(self::once())->method('retrieveResponse')->willReturn('foo=bar');
166
167
        $service = new BitBucket(
168
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
169
            $client,
170
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
171
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
172
        );
173
174
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
175
176
        $service->requestRequestToken();
177
    }
178
179
    /**
180
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
181
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
182
     * @covers \OAuth\OAuth1\Service\BitBucket::parseRequestTokenResponse
183
     */
184
    public function testParseRequestTokenResponseThrowsExceptionOnResponseCallbackNotTrue(): void
185
    {
186
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
187
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
188
            'oauth_callback_confirmed=false'
189
        );
190
191
        $service = new BitBucket(
192
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
193
            $client,
194
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
195
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
196
        );
197
198
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
199
200
        $service->requestRequestToken();
201
    }
202
203
    /**
204
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
205
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
206
     * @covers \OAuth\OAuth1\Service\BitBucket::parseAccessTokenResponse
207
     * @covers \OAuth\OAuth1\Service\BitBucket::parseRequestTokenResponse
208
     */
209
    public function testParseRequestTokenResponseValid(): void
210
    {
211
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
212
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
213
            'oauth_callback_confirmed=true&oauth_token=foo&oauth_token_secret=bar'
214
        );
215
216
        $service = new BitBucket(
217
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
218
            $client,
219
            $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
220
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
221
        );
222
223
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken());
224
    }
225
226
    /**
227
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
228
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
229
     * @covers \OAuth\OAuth1\Service\BitBucket::parseAccessTokenResponse
230
     */
231
    public function testParseAccessTokenResponseThrowsExceptionOnError(): void
232
    {
233
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
234
        $client->expects(self::once())->method('retrieveResponse')->willReturn('error=bar');
235
236
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
237
238
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
239
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
240
241
        $service = new BitBucket(
242
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
243
            $client,
244
            $storage,
245
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
246
        );
247
248
        $this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
249
250
        $service->requestAccessToken('foo', 'bar', $token);
251
    }
252
253
    /**
254
     * @covers \OAuth\OAuth1\Service\BitBucket::__construct
255
     * @covers \OAuth\OAuth1\Service\BitBucket::getRequestTokenEndpoint
256
     * @covers \OAuth\OAuth1\Service\BitBucket::parseAccessTokenResponse
257
     */
258
    public function testParseAccessTokenResponseValid(): void
259
    {
260
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
261
        $client->expects(self::once())->method('retrieveResponse')->willReturn(
262
            'oauth_token=foo&oauth_token_secret=bar'
263
        );
264
265
        $token = $this->createMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
266
267
        $storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
268
        $storage->expects(self::any())->method('retrieveAccessToken')->willReturn($token);
269
270
        $service = new BitBucket(
271
            $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
272
            $client,
273
            $storage,
274
            $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface')
275
        );
276
277
        self::assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
278
    }
279
}
280