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