1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth1\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth1\Service\FiveHundredPx; |
|
|
|
|
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
|
8
|
|
|
class FiveHundredPx extends TestCase |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::__construct |
12
|
|
|
*/ |
13
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri(): void |
14
|
|
|
{ |
15
|
|
|
$service = new FiveHundredPx( |
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\FiveHundredPx::__construct |
27
|
|
|
*/ |
28
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri(): void |
29
|
|
|
{ |
30
|
|
|
$service = new FiveHundredPx( |
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\FiveHundredPx::__construct |
42
|
|
|
*/ |
43
|
|
|
public function testConstructCorrectInstanceWithCustomUri(): void |
44
|
|
|
{ |
45
|
|
|
$service = new FiveHundredPx( |
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\FiveHundredPx::__construct |
58
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
59
|
|
|
*/ |
60
|
|
|
public function testGetRequestTokenEndpoint(): void |
61
|
|
|
{ |
62
|
|
|
$service = new FiveHundredPx( |
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://api.500px.com/v1/oauth/request_token', |
71
|
|
|
$service->getRequestTokenEndpoint()->getAbsoluteUri() |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::__construct |
77
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getAuthorizationEndpoint |
78
|
|
|
*/ |
79
|
|
|
public function testGetAuthorizationEndpoint(): void |
80
|
|
|
{ |
81
|
|
|
$service = new FiveHundredPx( |
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://api.500px.com/v1/oauth/authorize', |
90
|
|
|
$service->getAuthorizationEndpoint()->getAbsoluteUri() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::__construct |
96
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getAccessTokenEndpoint |
97
|
|
|
*/ |
98
|
|
|
public function testGetAccessTokenEndpoint(): void |
99
|
|
|
{ |
100
|
|
|
$service = new FiveHundredPx( |
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://api.500px.com/v1/oauth/access_token', |
109
|
|
|
$service->getAccessTokenEndpoint()->getAbsoluteUri() |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::__construct |
115
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
116
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
137
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
138
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
159
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
160
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
181
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
182
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
205
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
206
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::parseAccessTokenResponse |
207
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
228
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
229
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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\FiveHundredPx::__construct |
255
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::getRequestTokenEndpoint |
256
|
|
|
* @covers \OAuth\OAuth1\Service\FiveHundredPx::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 FiveHundredPx( |
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
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: