1 | <?php |
||
2 | /** |
||
3 | * @author David Desberg <[email protected]> |
||
4 | * @author Chris Heng <[email protected]> |
||
5 | * @author Pieter Hordijk <[email protected]> |
||
6 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
||
7 | */ |
||
8 | |||
9 | namespace OAuth\Unit; |
||
10 | |||
11 | use OAuth\ServiceFactory; |
||
12 | use PHPUnit\Framework\TestCase; |
||
13 | |||
14 | class ServiceFactoryTest extends TestCase |
||
15 | { |
||
16 | /** |
||
17 | * @covers \OAuth\ServiceFactory::setHttpClient |
||
18 | */ |
||
19 | public function testSetHttpClient(): void |
||
20 | { |
||
21 | $factory = new ServiceFactory(); |
||
22 | |||
23 | self::assertInstanceOf( |
||
24 | '\\OAuth\\ServiceFactory', |
||
25 | $factory->setHttpClient($this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface')) |
||
26 | ); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @covers \OAuth\ServiceFactory::registerService |
||
31 | */ |
||
32 | public function testRegisterServiceThrowsExceptionNonExistentClass(): void |
||
33 | { |
||
34 | $this->expectException('\\OAuth\Common\Exception\Exception'); |
||
35 | |||
36 | $factory = new ServiceFactory(); |
||
37 | $factory->registerService('foo', 'bar'); |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * @covers \OAuth\ServiceFactory::registerService |
||
42 | */ |
||
43 | public function testRegisterServiceThrowsExceptionWithClassIncorrectImplementation(): void |
||
44 | { |
||
45 | $this->expectException('\\OAuth\Common\Exception\Exception'); |
||
46 | |||
47 | $factory = new ServiceFactory(); |
||
48 | $factory->registerService('foo', 'OAuth\\ServiceFactory'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * @covers \OAuth\ServiceFactory::registerService |
||
53 | */ |
||
54 | public function testRegisterServiceSuccessOAuth1(): void |
||
55 | { |
||
56 | $factory = new ServiceFactory(); |
||
57 | |||
58 | self::assertInstanceOf( |
||
59 | '\\OAuth\\ServiceFactory', |
||
60 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake') |
||
61 | ); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @covers \OAuth\ServiceFactory::registerService |
||
66 | */ |
||
67 | public function testRegisterServiceSuccessOAuth2(): void |
||
68 | { |
||
69 | $factory = new ServiceFactory(); |
||
70 | |||
71 | self::assertInstanceOf( |
||
72 | '\\OAuth\\ServiceFactory', |
||
73 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake') |
||
74 | ); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * @covers \OAuth\ServiceFactory::buildV1Service |
||
79 | * @covers \OAuth\ServiceFactory::createService |
||
80 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
81 | */ |
||
82 | public function testCreateServiceOAuth1NonRegistered(): void |
||
83 | { |
||
84 | $factory = new ServiceFactory(); |
||
85 | |||
86 | $service = $factory->createService( |
||
87 | 'twitter', |
||
88 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
89 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
90 | ); |
||
91 | |||
92 | self::assertInstanceOf('\\OAuth\\OAuth1\\Service\\Twitter', $service); |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * @covers \OAuth\ServiceFactory::buildV1Service |
||
97 | * @covers \OAuth\ServiceFactory::createService |
||
98 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
99 | * @covers \OAuth\ServiceFactory::registerService |
||
100 | */ |
||
101 | public function testCreateServiceOAuth1Registered(): void |
||
102 | { |
||
103 | $factory = new ServiceFactory(); |
||
104 | |||
105 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake'); |
||
106 | |||
107 | $service = $factory->createService( |
||
108 | 'foo', |
||
109 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
110 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
111 | ); |
||
112 | |||
113 | self::assertInstanceOf('\\OAuth\OAuth1\Service\\ServiceInterface', $service); |
||
114 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake', $service); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @covers \OAuth\ServiceFactory::buildV1Service |
||
119 | * @covers \OAuth\ServiceFactory::createService |
||
120 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
121 | * @covers \OAuth\ServiceFactory::registerService |
||
122 | */ |
||
123 | public function testCreateServiceOAuth1RegisteredAndNonRegisteredSameName(): void |
||
124 | { |
||
125 | $factory = new ServiceFactory(); |
||
126 | |||
127 | $factory->registerService('twitter', '\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake'); |
||
128 | |||
129 | $service = $factory->createService( |
||
130 | 'twitter', |
||
131 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
132 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
133 | ); |
||
134 | |||
135 | self::assertInstanceOf('\\OAuth\OAuth1\Service\\ServiceInterface', $service); |
||
136 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake', $service); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
141 | * @covers \OAuth\ServiceFactory::createService |
||
142 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
143 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
144 | */ |
||
145 | public function testCreateServiceOAuth2NonRegistered(): void |
||
146 | { |
||
147 | $factory = new ServiceFactory(); |
||
148 | |||
149 | $service = $factory->createService( |
||
150 | 'facebook', |
||
151 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
152 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
153 | ); |
||
154 | |||
155 | self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\Facebook', $service); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
160 | * @covers \OAuth\ServiceFactory::createService |
||
161 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
162 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
163 | */ |
||
164 | public function testCreateServiceOAuth2Registered(): void |
||
165 | { |
||
166 | $factory = new ServiceFactory(); |
||
167 | |||
168 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake'); |
||
169 | |||
170 | $service = $factory->createService( |
||
171 | 'foo', |
||
172 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
173 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
174 | ); |
||
175 | |||
176 | self::assertInstanceOf('\\OAuth\OAuth2\Service\\ServiceInterface', $service); |
||
177 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake', $service); |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
182 | * @covers \OAuth\ServiceFactory::createService |
||
183 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
184 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
185 | */ |
||
186 | public function testCreateServiceOAuth2RegisteredAndNonRegisteredSameName(): void |
||
187 | { |
||
188 | $factory = new ServiceFactory(); |
||
189 | |||
190 | $factory->registerService('facebook', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake'); |
||
191 | |||
192 | $service = $factory->createService( |
||
193 | 'facebook', |
||
194 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
195 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
196 | ); |
||
197 | |||
198 | self::assertInstanceOf('\\OAuth\OAuth2\Service\\ServiceInterface', $service); |
||
199 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake', $service); |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @covers \OAuth\ServiceFactory::buildV1Service |
||
204 | * @covers \OAuth\ServiceFactory::createService |
||
205 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
206 | * @covers \OAuth\ServiceFactory::registerService |
||
207 | */ |
||
208 | public function testCreateServiceThrowsExceptionOnPassingScopesToV1Service(): void |
||
209 | { |
||
210 | $this->expectException('\\OAuth\Common\Exception\Exception'); |
||
211 | |||
212 | $factory = new ServiceFactory(); |
||
213 | |||
214 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake'); |
||
215 | |||
216 | $service = $factory->createService( |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
217 | 'foo', |
||
218 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
219 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
||
220 | ['bar'] |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @covers \OAuth\ServiceFactory::createService |
||
226 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
227 | */ |
||
228 | public function testCreateServiceNonExistentService(): void |
||
229 | { |
||
230 | $factory = new ServiceFactory(); |
||
231 | |||
232 | $service = $factory->createService( |
||
233 | 'foo', |
||
234 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
235 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
236 | ); |
||
237 | |||
238 | self::assertNull($service); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
243 | * @covers \OAuth\ServiceFactory::createService |
||
244 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
245 | * @covers \OAuth\ServiceFactory::registerService |
||
246 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
247 | */ |
||
248 | public function testCreateServicePrefersOauth2(): void |
||
249 | { |
||
250 | $factory = new ServiceFactory(); |
||
251 | |||
252 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth1\\Service\\Fake'); |
||
253 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake'); |
||
254 | |||
255 | $service = $factory->createService( |
||
256 | 'foo', |
||
257 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
258 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
||
259 | ); |
||
260 | |||
261 | self::assertInstanceOf('\\OAuth\OAuth2\Service\\ServiceInterface', $service); |
||
262 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake', $service); |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
267 | * @covers \OAuth\ServiceFactory::createService |
||
268 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
269 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
270 | */ |
||
271 | public function testCreateServiceOAuth2RegisteredWithClassConstantsAsScope(): void |
||
272 | { |
||
273 | $factory = new ServiceFactory(); |
||
274 | |||
275 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake'); |
||
276 | |||
277 | $service = $factory->createService( |
||
278 | 'foo', |
||
279 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
280 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
||
281 | ['FOO'] |
||
282 | ); |
||
283 | |||
284 | self::assertInstanceOf('\\OAuth\OAuth2\Service\\ServiceInterface', $service); |
||
285 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake', $service); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * @covers \OAuth\ServiceFactory::buildV2Service |
||
290 | * @covers \OAuth\ServiceFactory::createService |
||
291 | * @covers \OAuth\ServiceFactory::getFullyQualifiedServiceName |
||
292 | * @covers \OAuth\ServiceFactory::resolveScopes |
||
293 | */ |
||
294 | public function testCreateServiceOAuth2RegisteredWithCustomScope(): void |
||
295 | { |
||
296 | $factory = new ServiceFactory(); |
||
297 | |||
298 | $factory->registerService('foo', '\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake'); |
||
299 | |||
300 | $service = $factory->createService( |
||
301 | 'foo', |
||
302 | $this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
||
303 | $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
||
304 | ['custom'] |
||
305 | ); |
||
306 | |||
307 | self::assertInstanceOf('\\OAuth\OAuth2\Service\\ServiceInterface', $service); |
||
308 | self::assertInstanceOf('\\OAuthTest\\Mocks\\OAuth2\\Service\\Fake', $service); |
||
309 | } |
||
310 | } |
||
311 |