1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Token\TokenInterface; |
6
|
|
|
use OAuth\OAuth2\Service\EveOnline; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
|
9
|
|
|
class EveOnlineTest extends TestCase |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
13
|
|
|
*/ |
14
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri(): void |
15
|
|
|
{ |
16
|
|
|
$service = new EveOnline( |
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\EveOnline::__construct |
27
|
|
|
*/ |
28
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri(): void |
29
|
|
|
{ |
30
|
|
|
$service = new EveOnline( |
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\EveOnline::__construct |
41
|
|
|
*/ |
42
|
|
|
public function testConstructCorrectInstanceWithCustomUri(): void |
43
|
|
|
{ |
44
|
|
|
$service = new EveOnline( |
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\EveOnline::__construct |
57
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::getAuthorizationEndpoint |
58
|
|
|
*/ |
59
|
|
|
public function testGetAuthorizationEndpoint(): void |
60
|
|
|
{ |
61
|
|
|
$service = new EveOnline( |
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://login.eveonline.com/oauth/authorize', |
68
|
|
|
$service->getAuthorizationEndpoint()->getAbsoluteUri()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
73
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::getAccessTokenEndpoint |
74
|
|
|
*/ |
75
|
|
|
public function testGetAccessTokenEndpoint(): void |
76
|
|
|
{ |
77
|
|
|
$service = new EveOnline( |
78
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
79
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
80
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
self::assertSame('https://login.eveonline.com/oauth/token', |
84
|
|
|
$service->getAccessTokenEndpoint()->getAbsoluteUri()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
89
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::getAuthorizationMethod |
90
|
|
|
*/ |
91
|
|
|
public function testGetAuthorizationMethod(): void |
92
|
|
|
{ |
93
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
94
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2); |
95
|
|
|
|
96
|
|
|
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
97
|
|
|
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES); |
98
|
|
|
$token->expects(self::once())->method('getAccessToken')->willReturn('foo'); |
99
|
|
|
|
100
|
|
|
$storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
101
|
|
|
$storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token); |
102
|
|
|
|
103
|
|
|
$service = new EveOnline( |
104
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
105
|
|
|
$client, |
|
|
|
|
106
|
|
|
$storage |
|
|
|
|
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
$headers = $service->request('https://pieterhordijk.com/my/awesome/path'); |
110
|
|
|
|
111
|
|
|
self::assertArrayHasKey('Authorization', $headers); |
|
|
|
|
112
|
|
|
self::assertTrue(in_array('Bearer foo', $headers, true)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
117
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse |
118
|
|
|
*/ |
119
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void |
120
|
|
|
{ |
121
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
122
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn(null); |
123
|
|
|
|
124
|
|
|
$service = new EveOnline( |
125
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
126
|
|
|
$client, |
|
|
|
|
127
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
131
|
|
|
|
132
|
|
|
$service->requestAccessToken('foo'); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
137
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse |
138
|
|
|
*/ |
139
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription(): void |
140
|
|
|
{ |
141
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
142
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('error_description=some_error'); |
143
|
|
|
|
144
|
|
|
$service = new EveOnline( |
145
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
146
|
|
|
$client, |
|
|
|
|
147
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
148
|
|
|
); |
149
|
|
|
|
150
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
151
|
|
|
|
152
|
|
|
$service->requestAccessToken('foo'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
157
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse |
158
|
|
|
*/ |
159
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnError(): void |
160
|
|
|
{ |
161
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
162
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error'); |
163
|
|
|
|
164
|
|
|
$service = new EveOnline( |
165
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
166
|
|
|
$client, |
|
|
|
|
167
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
168
|
|
|
); |
169
|
|
|
|
170
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
171
|
|
|
|
172
|
|
|
$service->requestAccessToken('foo'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
177
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse |
178
|
|
|
*/ |
179
|
|
|
public function testParseAccessTokenResponseValidWithoutRefreshToken(): void |
180
|
|
|
{ |
181
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
182
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}'); |
183
|
|
|
|
184
|
|
|
$service = new EveOnline( |
185
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
186
|
|
|
$client, |
|
|
|
|
187
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::__construct |
195
|
|
|
* @covers \OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse |
196
|
|
|
*/ |
197
|
|
|
public function testParseAccessTokenResponseValidWithRefreshToken(): void |
198
|
|
|
{ |
199
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
200
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'); |
201
|
|
|
|
202
|
|
|
$service = new EveOnline( |
203
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
204
|
|
|
$client, |
|
|
|
|
205
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
206
|
|
|
); |
207
|
|
|
|
208
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|
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: