testGetAuthorizationUriWithoutParameters()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
dl 0
loc 11
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth1\Service;
4
5
use OAuthTest\Mocks\OAuth1\Service\Mock;
6
use PHPUnit\Framework\Assert;
7
use PHPUnit\Framework\TestCase;
8
9
class AbstractServiceTest extends TestCase
10
{
11
    /**
12
     * @covers \AbstractService::__construct
13
     */
14
    public function testConstructCorrectInterface(): void
15
    {
16
        $service = $this->getMockForAbstractClass(
17
            '\\OAuth\\OAuth1\\Service\\AbstractService',
18
            [
19
                $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
20
                $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
21
                $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
22
                $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
23
                $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
24
            ]
25
        );
26
27
        self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\ServiceInterface', $service);
28
    }
29
30
    /**
31
     * @covers \AbstractService::__construct
32
     */
33
    public function testConstructCorrectParent(): void
34
    {
35
        $service = $this->getMockForAbstractClass(
36
            '\\OAuth\\OAuth1\\Service\\AbstractService',
37
            [
38
                $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
39
                $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
40
                $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
41
                $this->createMock('\\OAuth\\OAuth1\\Signature\\SignatureInterface'),
42
                $this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),
43
            ]
44
        );
45
46
        self::assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service);
47
    }
48
49
    /**
50
     * @covers \AbstractService::buildAuthorizationHeaderForTokenRequest
51
     * @covers \AbstractService::generateNonce
52
     * @covers \AbstractService::getBasicAuthorizationHeaderInfo
53
     * @covers \AbstractService::getExtraOAuthHeaders
54
     * @covers \AbstractService::getSignatureMethod
55
     * @covers \AbstractService::getVersion
56
     * @covers \AbstractService::parseRequestTokenResponse
57
     * @covers \AbstractService::requestRequestToken
58
     */
59
    public function testRequestRequestTokenBuildAuthHeaderTokenRequestWithoutParams(): void
60
    {
61
        $client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
62
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, $array, $headers): void {
0 ignored issues
show
Unused Code introduced by
The parameter $array is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, /** @scrutinizer ignore-unused */ $array, $headers): void {

This check looks for 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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, $array, /** @scrutinizer ignore-unused */ $headers): void {

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

129
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, /** @scrutinizer ignore-unused */ $array, $headers): void {

This check looks for 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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

129
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, $array, /** @scrutinizer ignore-unused */ $headers): void {

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

165
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, /** @scrutinizer ignore-unused */ $array, $headers): void {

This check looks for 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. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

165
        $client->expects(self::once())->method('retrieveResponse')->willReturnCallback(function ($endpoint, $array, /** @scrutinizer ignore-unused */ $headers): void {

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

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

241
        self::assertSame('response!', $service->request('/my/awesome/path', 'GET', /** @scrutinizer ignore-type */ 'A text body'));
Loading history...
242
    }
243
}
244