Completed
Pull Request — master (#479)
by Andrey
02:39
created

BitBucketTest   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 272
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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