Completed
Pull Request — master (#478)
by
unknown
02:35
created

AbstractServiceTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 9
lcom 0
cbo 2
dl 0
loc 244
rs 10
c 2
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructCorrectInterface() 0 15 1
A testConstructCorrectParent() 0 15 1
A testRequestRequestTokenBuildAuthHeaderTokenRequestWithoutParams() 0 17 1
A testGetAuthorizationUriWithoutParameters() 0 12 1
A testGetAuthorizationUriWithParameters() 0 15 1
B testRequestAccessTokenWithoutSecret() 0 24 1
B testRequestAccessTokenWithSecret() 0 24 1
A testRequest() 0 23 1
A testRequestNonArrayBody() 0 23 1
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuthTest\Mocks\OAuth1\Service\Mock;
6
7
class AbstractServiceTest extends \PHPUnit_Framework_TestCase
8
{
9
    /**
10
     * @covers OAuth\OAuth1\Service\AbstractService::__construct
11
     */
12
    public function testConstructCorrectInterface()
13
    {
14
        $service = $this->getMockForAbstractClass(
15
            '\\OAuth\\OAuth1\\Service\\AbstractService',
16
            array(
17
                $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
18
                $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
19
                $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
20
                $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
21
                $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
22
            )
23
        );
24
25
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
26
    }
27
28
    /**
29
     * @covers OAuth\OAuth1\Service\AbstractService::__construct
30
     */
31
    public function testConstructCorrectParent()
32
    {
33
        $service = $this->getMockForAbstractClass(
34
            '\\OAuth\\OAuth1\\Service\\AbstractService',
35
            array(
36
                $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
37
                $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
38
                $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
39
                $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
40
                $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
41
            )
42
        );
43
44
        $this->assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service);
45
    }
46
47
    /**
48
     * @covers OAuth\OAuth1\Service\AbstractService::requestRequestToken
49
     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForTokenRequest
50
     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
51
     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
52
     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
53
     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
54
     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
55
     * @covers OAuth\OAuth1\Service\AbstractService::parseRequestTokenResponse
56
     */
57
    public function testRequestRequestTokenBuildAuthHeaderTokenRequestWithoutParams()
58
    {
59
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
60
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
61
            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/token', $endpoint->getAbsoluteUri());
62
        }));
63
64
        $service = new Mock(
65
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
66
            $client,
67
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
68
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
69
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
70
        );
71
72
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestRequestToken());
73
    }
74
75
    /**
76
     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationUri
77
     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationEndpoint
78
     */
79
    public function testGetAuthorizationUriWithoutParameters()
80
    {
81
        $service = new Mock(
82
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
83
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
84
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
85
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
86
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
87
        );
88
89
        $this->assertSame('http://pieterhordijk.com/auth', $service->getAuthorizationUri()->getAbsoluteUri());
90
    }
91
92
    /**
93
     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationUri
94
     * @covers OAuth\OAuth1\Service\AbstractService::getAuthorizationEndpoint
95
     */
96
    public function testGetAuthorizationUriWithParameters()
97
    {
98
        $service = new Mock(
99
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
100
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
101
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
102
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
103
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
104
        );
105
106
        $this->assertSame('http://pieterhordijk.com/auth?foo=bar&baz=beer', $service->getAuthorizationUri(array(
107
            'foo' => 'bar',
108
            'baz' => 'beer',
109
        ))->getAbsoluteUri());
110
    }
111
112
    /**
113
     * @covers OAuth\OAuth1\Service\AbstractService::requestAccessToken
114
     * @covers OAuth\OAuth1\Service\AbstractService::service
115
     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
116
     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
117
     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
118
     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
119
     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
120
     * @covers OAuth\OAuth1\Service\AbstractService::getAccessTokenEndpoint
121
     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
122
     * @covers OAuth\OAuth1\Service\AbstractService::parseAccessTokenResponse
123
     */
124
    public function testRequestAccessTokenWithoutSecret()
125
    {
126
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
127
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
128
            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/access', $endpoint->getAbsoluteUri());
129
        }));
130
131
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
132
        $token->expects($this->exactly(2))->method('getRequestTokenSecret')->will($this->returnValue('baz'));
133
        $token->expects($this->once())->method('getAccessTokenSecret')->willReturn('baz');
134
135
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
136
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
137
138
        $service = new Mock(
139
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
140
            $client,
141
            $storage,
142
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
143
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
144
        );
145
146
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar'));
147
    }
148
149
    /**
150
     * @covers OAuth\OAuth1\Service\AbstractService::requestAccessToken
151
     * @covers OAuth\OAuth1\Service\AbstractService::service
152
     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
153
     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
154
     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
155
     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
156
     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
157
     * @covers OAuth\OAuth1\Service\AbstractService::getAccessTokenEndpoint
158
     * @covers OAuth\OAuth1\Service\AbstractService::getExtraOAuthHeaders
159
     * @covers OAuth\OAuth1\Service\AbstractService::parseAccessTokenResponse
160
     */
161
    public function testRequestAccessTokenWithSecret()
162
    {
163
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
164
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnCallback(function($endpoint, $array, $headers) {
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $headers is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
165
            \PHPUnit_Framework_Assert::assertSame('http://pieterhordijk.com/access', $endpoint->getAbsoluteUri());
166
        }));
167
168
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
169
        $token->expects($this->once())->method('getRequestTokenSecret')->will($this->returnValue('baz'));
170
        $token->expects($this->once())->method('getAccessTokenSecret')->willReturn('baz');
171
172
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
173
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
174
175
        $service = new Mock(
176
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
177
            $client,
178
            $storage,
179
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
180
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
181
        );
182
183
        $this->assertInstanceOf('\\OAuth\\OAuth1\\Token\\StdOAuth1Token', $service->requestAccessToken('foo', 'bar', $token));
184
    }
185
186
    /**
187
     * @covers OAuth\OAuth1\Service\AbstractService::request
188
     * @covers OAuth\OAuth1\Service\AbstractService::determineRequestUriFromPath
189
     * @covers OAuth\OAuth1\Service\AbstractService::service
190
     * @covers OAuth\OAuth1\Service\AbstractService::getExtraApiHeaders
191
     * @covers OAuth\OAuth1\Service\AbstractService::buildAuthorizationHeaderForAPIRequest
192
     * @covers OAuth\OAuth1\Service\AbstractService::getBasicAuthorizationHeaderInfo
193
     * @covers OAuth\OAuth1\Service\AbstractService::generateNonce
194
     * @covers OAuth\OAuth1\Service\AbstractService::getSignatureMethod
195
     * @covers OAuth\OAuth1\Service\AbstractService::getVersion
196
     */
197
    public function testRequest()
198
    {
199
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
200
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('response!'));
201
202
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
203
        $token->expects($this->once())
204
                ->method('getAccessTokenSecret')
205
                ->willReturn('accessTokenSecret');
206
        
207
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
208
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
209
210
        $service = new Mock(
211
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
212
            $client,
213
            $storage,
214
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
215
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
216
        );
217
218
        $this->assertSame('response!', $service->request('/my/awesome/path'));
219
    }
220
221
    /**
222
     * This test only captures a regression in php 5.3.
223
     *
224
     * @covers OAuth\OAuth1\Service\AbstractService::request
225
     */
226
    public function testRequestNonArrayBody()
227
    {
228
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
229
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('response!'));
230
231
        $token = $this->getMock('\\OAuth\\OAuth1\\Token\\TokenInterface');
232
        $token->expects($this->once())
233
                ->method('getAccessTokenSecret')
234
                ->willReturn('accessTokenSecret');
235
236
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
237
        $storage->expects($this->any())->method('retrieveAccessToken')->will($this->returnValue($token));
238
239
        $service = new Mock(
240
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
241
            $client,
242
            $storage,
243
            $this->getMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
244
            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
245
        );
246
247
        $this->assertSame('response!', $service->request('/my/awesome/path', 'GET', 'A text body'));
0 ignored issues
show
Documentation introduced by
'A text body' is of type string, but the function expects a array|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
248
    }
249
250
}
251