1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Token\TokenInterface; |
6
|
|
|
use OAuth\OAuth2\Service\Foursquare; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class FoursquareTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
13
|
|
|
*/ |
14
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri(): void |
15
|
|
|
{ |
16
|
|
|
$service = new Foursquare( |
17
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
18
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
19
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
27
|
|
|
*/ |
28
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri(): void |
29
|
|
|
{ |
30
|
|
|
$service = new Foursquare( |
31
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
32
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
33
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
34
|
|
|
); |
35
|
|
|
|
36
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
41
|
|
|
*/ |
42
|
|
|
public function testConstructCorrectInstanceWithCustomUri(): void |
43
|
|
|
{ |
44
|
|
|
$service = new Foursquare( |
45
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
46
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
47
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
48
|
|
|
[], |
49
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
57
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::getAuthorizationEndpoint |
58
|
|
|
*/ |
59
|
|
|
public function testGetAuthorizationEndpoint(): void |
60
|
|
|
{ |
61
|
|
|
$service = new Foursquare( |
62
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
63
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
64
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
self::assertSame('https://foursquare.com/oauth2/authenticate', $service->getAuthorizationEndpoint()->getAbsoluteUri()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
72
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::getAccessTokenEndpoint |
73
|
|
|
*/ |
74
|
|
|
public function testGetAccessTokenEndpoint(): void |
75
|
|
|
{ |
76
|
|
|
$service = new Foursquare( |
77
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
78
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
79
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
self::assertSame('https://foursquare.com/oauth2/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
87
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::getAuthorizationMethod |
88
|
|
|
*/ |
89
|
|
|
public function testGetAuthorizationMethod(): void |
90
|
|
|
{ |
91
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
92
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2); |
93
|
|
|
|
94
|
|
|
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
95
|
|
|
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES); |
96
|
|
|
$token->expects(self::once())->method('getAccessToken')->willReturn('foo'); |
97
|
|
|
|
98
|
|
|
$storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
99
|
|
|
$storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token); |
100
|
|
|
|
101
|
|
|
$service = new Foursquare( |
102
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
103
|
|
|
$client, |
104
|
|
|
$storage |
105
|
|
|
); |
106
|
|
|
|
107
|
|
|
$headers = $service->request('https://pieterhordijk.com/my/awesome/path'); |
108
|
|
|
|
109
|
|
|
self::assertArrayHasKey('Authorization', $headers); |
|
|
|
|
110
|
|
|
self::assertTrue(in_array('OAuth foo', $headers, true)); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
115
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse |
116
|
|
|
*/ |
117
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void |
118
|
|
|
{ |
119
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
120
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn(null); |
121
|
|
|
|
122
|
|
|
$service = new Foursquare( |
123
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
124
|
|
|
$client, |
125
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
126
|
|
|
); |
127
|
|
|
|
128
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
129
|
|
|
|
130
|
|
|
$service->requestAccessToken('foo'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
135
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse |
136
|
|
|
*/ |
137
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnError(): void |
138
|
|
|
{ |
139
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
140
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"error":"some_error"}'); |
141
|
|
|
|
142
|
|
|
$service = new Foursquare( |
143
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
144
|
|
|
$client, |
145
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
149
|
|
|
|
150
|
|
|
$service->requestAccessToken('foo'); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
155
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse |
156
|
|
|
*/ |
157
|
|
|
public function testParseAccessTokenResponseValidWithoutRefreshToken(): void |
158
|
|
|
{ |
159
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
160
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}'); |
161
|
|
|
|
162
|
|
|
$service = new Foursquare( |
163
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
164
|
|
|
$client, |
165
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
166
|
|
|
); |
167
|
|
|
|
168
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
173
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::request |
174
|
|
|
*/ |
175
|
|
|
public function testRequest(): void |
176
|
|
|
{ |
177
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
178
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0); |
179
|
|
|
|
180
|
|
|
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
181
|
|
|
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES); |
182
|
|
|
$token->expects(self::once())->method('getAccessToken')->willReturn('foo'); |
183
|
|
|
|
184
|
|
|
$storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
185
|
|
|
$storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token); |
186
|
|
|
|
187
|
|
|
$service = new Foursquare( |
188
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
189
|
|
|
$client, |
190
|
|
|
$storage |
191
|
|
|
); |
192
|
|
|
|
193
|
|
|
self::assertSame( |
194
|
|
|
'https://pieterhordijk.com/my/awesome/path?v=20130829', |
195
|
|
|
$service->request('https://pieterhordijk.com/my/awesome/path')->getAbsoluteUri() |
196
|
|
|
); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::__construct |
201
|
|
|
* @covers \OAuth\OAuth2\Service\Foursquare::request |
202
|
|
|
*/ |
203
|
|
|
public function testRequestShortPath(): void |
204
|
|
|
{ |
205
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
206
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(0); |
207
|
|
|
|
208
|
|
|
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
209
|
|
|
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES); |
210
|
|
|
$token->expects(self::once())->method('getAccessToken')->willReturn('foo'); |
211
|
|
|
|
212
|
|
|
$storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
213
|
|
|
$storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token); |
214
|
|
|
|
215
|
|
|
$service = new Foursquare( |
216
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
217
|
|
|
$client, |
218
|
|
|
$storage |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
self::assertSame( |
222
|
|
|
'https://api.foursquare.com/v2/my/awesome/path?v=20130829', |
223
|
|
|
$service->request('my/awesome/path')->getAbsoluteUri() |
224
|
|
|
); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|