]>
Commit | Line | Data |
---|---|---|
7ec2897e JB |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
7ec2897e JB |
4 | |
5 | use Wallabag\UserBundle\Entity\User; | |
79d0e38e | 6 | use Wallabag\CoreBundle\Entity\Entry; |
7ec2897e | 7 | use Wallabag\ImportBundle\Import\PocketImport; |
7ec2897e JB |
8 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | |
10 | use GuzzleHttp\Message\Response; | |
11 | use GuzzleHttp\Stream\Stream; | |
252ebd60 JB |
12 | use Monolog\Logger; |
13 | use Monolog\Handler\TestHandler; | |
14 | ||
7ec2897e JB |
15 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
16 | { | |
17 | protected $token; | |
18 | protected $user; | |
7ec2897e | 19 | protected $em; |
252ebd60 JB |
20 | protected $contentProxy; |
21 | protected $logHandler; | |
7ec2897e | 22 | |
ef75e122 | 23 | private function getPocketImport($consumerKey = 'ConsumerKey') |
7ec2897e JB |
24 | { |
25 | $this->user = new User(); | |
26 | ||
252ebd60 JB |
27 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
28 | ->disableOriginalConstructor() | |
29 | ->getMock(); | |
30 | ||
7ec2897e JB |
31 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
32 | ->disableOriginalConstructor() | |
33 | ->getMock(); | |
34 | ||
63e40f2d JB |
35 | $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') |
36 | ->disableOriginalConstructor() | |
37 | ->getMock(); | |
38 | ||
39 | $config->expects($this->any()) | |
40 | ->method('get') | |
41 | ->with('pocket_consumer_key') | |
42 | ->willReturn($consumerKey); | |
43 | ||
02f64895 | 44 | $pocket = new PocketImport( |
7ec2897e | 45 | $this->em, |
252ebd60 | 46 | $this->contentProxy, |
ef75e122 | 47 | $config |
7ec2897e | 48 | ); |
ef75e122 | 49 | $pocket->setUser($this->user); |
252ebd60 JB |
50 | |
51 | $this->logHandler = new TestHandler(); | |
4094ea47 | 52 | $logger = new Logger('test', [$this->logHandler]); |
252ebd60 JB |
53 | $pocket->setLogger($logger); |
54 | ||
55 | return $pocket; | |
7ec2897e JB |
56 | } |
57 | ||
58 | public function testInit() | |
59 | { | |
60 | $pocketImport = $this->getPocketImport(); | |
61 | ||
62 | $this->assertEquals('Pocket', $pocketImport->getName()); | |
7019c7cf | 63 | $this->assertNotEmpty($pocketImport->getUrl()); |
0d42217e | 64 | $this->assertEquals('import.pocket.description', $pocketImport->getDescription()); |
7ec2897e JB |
65 | } |
66 | ||
67 | public function testOAuthRequest() | |
68 | { | |
69 | $client = new Client(); | |
70 | ||
71 | $mock = new Mock([ | |
252ebd60 | 72 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), |
7ec2897e JB |
73 | ]); |
74 | ||
75 | $client->getEmitter()->attach($mock); | |
76 | ||
77 | $pocketImport = $this->getPocketImport(); | |
78 | $pocketImport->setClient($client); | |
79 | ||
252ebd60 | 80 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); |
7ec2897e | 81 | |
252ebd60 JB |
82 | $this->assertEquals('wunderbar_code', $code); |
83 | } | |
84 | ||
85 | public function testOAuthRequestBadResponse() | |
86 | { | |
87 | $client = new Client(); | |
88 | ||
89 | $mock = new Mock([ | |
90 | new Response(403), | |
91 | ]); | |
92 | ||
93 | $client->getEmitter()->attach($mock); | |
94 | ||
95 | $pocketImport = $this->getPocketImport(); | |
96 | $pocketImport->setClient($client); | |
97 | ||
98 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | |
99 | ||
100 | $this->assertFalse($code); | |
101 | ||
102 | $records = $this->logHandler->getRecords(); | |
103 | $this->assertContains('PocketImport: Failed to request token', $records[0]['message']); | |
104 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
7ec2897e JB |
105 | } |
106 | ||
107 | public function testOAuthAuthorize() | |
108 | { | |
109 | $client = new Client(); | |
110 | ||
111 | $mock = new Mock([ | |
252ebd60 | 112 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
7ec2897e JB |
113 | ]); |
114 | ||
115 | $client->getEmitter()->attach($mock); | |
116 | ||
117 | $pocketImport = $this->getPocketImport(); | |
118 | $pocketImport->setClient($client); | |
119 | ||
252ebd60 | 120 | $res = $pocketImport->authorize('wunderbar_code'); |
7ec2897e | 121 | |
252ebd60 JB |
122 | $this->assertTrue($res); |
123 | $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken()); | |
7ec2897e JB |
124 | } |
125 | ||
252ebd60 JB |
126 | public function testOAuthAuthorizeBadResponse() |
127 | { | |
128 | $client = new Client(); | |
129 | ||
130 | $mock = new Mock([ | |
131 | new Response(403), | |
132 | ]); | |
133 | ||
134 | $client->getEmitter()->attach($mock); | |
135 | ||
136 | $pocketImport = $this->getPocketImport(); | |
137 | $pocketImport->setClient($client); | |
138 | ||
139 | $res = $pocketImport->authorize('wunderbar_code'); | |
140 | ||
141 | $this->assertFalse($res); | |
142 | ||
143 | $records = $this->logHandler->getRecords(); | |
144 | $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']); | |
145 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
146 | } | |
147 | ||
148 | /** | |
149 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | |
150 | */ | |
7ec2897e JB |
151 | public function testImport() |
152 | { | |
153 | $client = new Client(); | |
154 | ||
155 | $mock = new Mock([ | |
252ebd60 JB |
156 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
157 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
158 | { | |
159 | "status": 1, | |
160 | "list": { | |
161 | "229279689": { | |
162 | "item_id": "229279689", | |
163 | "resolved_id": "229279689", | |
164 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
165 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
166 | "favorite": "1", | |
167 | "status": "1", | |
168 | "resolved_title": "The Massive Ryder Cup Preview", | |
169 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
170 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | |
171 | "is_article": "1", | |
172 | "has_video": "1", | |
173 | "has_image": "1", | |
174 | "word_count": "3197", | |
175 | "images": { | |
176 | "1": { | |
177 | "item_id": "229279689", | |
178 | "image_id": "1", | |
179 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", | |
180 | "width": "0", | |
181 | "height": "0", | |
182 | "credit": "Jamie Squire/Getty Images", | |
183 | "caption": "" | |
184 | } | |
185 | }, | |
186 | "videos": { | |
187 | "1": { | |
188 | "item_id": "229279689", | |
189 | "video_id": "1", | |
190 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", | |
191 | "width": "420", | |
192 | "height": "315", | |
193 | "type": "1", | |
194 | "vid": "Er34PbFkVGk" | |
195 | } | |
196 | }, | |
197 | "tags": { | |
198 | "grantland": { | |
199 | "item_id": "1147652870", | |
200 | "tag": "grantland" | |
201 | }, | |
202 | "Ryder Cup": { | |
203 | "item_id": "1147652870", | |
204 | "tag": "Ryder Cup" | |
205 | } | |
206 | } | |
207 | }, | |
208 | "229279690": { | |
209 | "item_id": "229279689", | |
210 | "resolved_id": "229279689", | |
211 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
212 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
213 | "favorite": "1", | |
214 | "status": "1", | |
215 | "resolved_title": "The Massive Ryder Cup Preview", | |
216 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
217 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | |
218 | "is_article": "1", | |
219 | "has_video": "0", | |
220 | "has_image": "0", | |
221 | "word_count": "3197" | |
222 | } | |
223 | } | |
224 | } | |
225 | ')), | |
226 | ]); | |
227 | ||
228 | $client->getEmitter()->attach($mock); | |
229 | ||
230 | $pocketImport = $this->getPocketImport(); | |
231 | ||
232 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
233 | ->disableOriginalConstructor() | |
234 | ->getMock(); | |
235 | ||
236 | $entryRepo->expects($this->exactly(2)) | |
78833672 | 237 | ->method('findByUrlAndUserId') |
252ebd60 JB |
238 | ->will($this->onConsecutiveCalls(false, true)); |
239 | ||
252ebd60 | 240 | $this->em |
c2656f96 | 241 | ->expects($this->exactly(2)) |
252ebd60 | 242 | ->method('getRepository') |
c2656f96 | 243 | ->willReturn($entryRepo); |
252ebd60 | 244 | |
79d0e38e | 245 | $entry = new Entry($this->user); |
252ebd60 JB |
246 | |
247 | $this->contentProxy | |
248 | ->expects($this->once()) | |
249 | ->method('updateEntry') | |
250 | ->willReturn($entry); | |
251 | ||
252 | $pocketImport->setClient($client); | |
253 | $pocketImport->authorize('wunderbar_code'); | |
254 | ||
255 | $res = $pocketImport->import(); | |
256 | ||
257 | $this->assertTrue($res); | |
258 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | |
259 | } | |
260 | ||
79d0e38e JB |
261 | /** |
262 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | |
263 | */ | |
264 | public function testImportAndMarkAllAsRead() | |
265 | { | |
266 | $client = new Client(); | |
267 | ||
268 | $mock = new Mock([ | |
269 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
270 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
271 | { | |
272 | "status": 1, | |
273 | "list": { | |
274 | "229279689": { | |
275 | "item_id": "229279689", | |
276 | "resolved_id": "229279689", | |
277 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | |
278 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
279 | "favorite": "1", | |
280 | "status": "1", | |
79d0e38e JB |
281 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
282 | "is_article": "1", | |
283 | "has_video": "1", | |
284 | "has_image": "1", | |
285 | "word_count": "3197" | |
286 | }, | |
287 | "229279690": { | |
288 | "item_id": "229279689", | |
289 | "resolved_id": "229279689", | |
290 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", | |
291 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | |
292 | "favorite": "1", | |
293 | "status": "0", | |
79d0e38e JB |
294 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", |
295 | "is_article": "1", | |
296 | "has_video": "0", | |
297 | "has_image": "0", | |
298 | "word_count": "3197" | |
299 | } | |
300 | } | |
301 | } | |
302 | ')), | |
303 | ]); | |
304 | ||
305 | $client->getEmitter()->attach($mock); | |
306 | ||
307 | $pocketImport = $this->getPocketImport(); | |
308 | ||
309 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
310 | ->disableOriginalConstructor() | |
311 | ->getMock(); | |
312 | ||
313 | $entryRepo->expects($this->exactly(2)) | |
314 | ->method('findByUrlAndUserId') | |
315 | ->will($this->onConsecutiveCalls(false, false)); | |
316 | ||
317 | $this->em | |
318 | ->expects($this->exactly(2)) | |
319 | ->method('getRepository') | |
320 | ->willReturn($entryRepo); | |
321 | ||
322 | // check that every entry persisted are archived | |
323 | $this->em | |
324 | ->expects($this->any()) | |
325 | ->method('persist') | |
cebb4223 | 326 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
327 | return $persistedEntry->isArchived(); |
328 | })); | |
329 | ||
330 | $entry = new Entry($this->user); | |
331 | ||
332 | $this->contentProxy | |
333 | ->expects($this->exactly(2)) | |
334 | ->method('updateEntry') | |
335 | ->willReturn($entry); | |
336 | ||
337 | $pocketImport->setClient($client); | |
338 | $pocketImport->authorize('wunderbar_code'); | |
339 | ||
340 | $res = $pocketImport->setMarkAsRead(true)->import(); | |
341 | ||
342 | $this->assertTrue($res); | |
343 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | |
344 | } | |
345 | ||
252ebd60 JB |
346 | public function testImportBadResponse() |
347 | { | |
348 | $client = new Client(); | |
349 | ||
350 | $mock = new Mock([ | |
351 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
352 | new Response(403), | |
7ec2897e JB |
353 | ]); |
354 | ||
355 | $client->getEmitter()->attach($mock); | |
356 | ||
357 | $pocketImport = $this->getPocketImport(); | |
358 | $pocketImport->setClient($client); | |
252ebd60 JB |
359 | $pocketImport->authorize('wunderbar_code'); |
360 | ||
361 | $res = $pocketImport->import(); | |
7ec2897e | 362 | |
252ebd60 | 363 | $this->assertFalse($res); |
7ec2897e | 364 | |
252ebd60 JB |
365 | $records = $this->logHandler->getRecords(); |
366 | $this->assertContains('PocketImport: Failed to import', $records[0]['message']); | |
367 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
7ec2897e | 368 | } |
19d9efab JB |
369 | |
370 | public function testImportWithExceptionFromGraby() | |
371 | { | |
372 | $client = new Client(); | |
373 | ||
374 | $mock = new Mock([ | |
375 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | |
376 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | |
377 | { | |
378 | "status": 1, | |
379 | "list": { | |
380 | "229279689": { | |
381 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | |
382 | } | |
383 | } | |
384 | } | |
385 | ')), | |
386 | ]); | |
387 | ||
388 | $client->getEmitter()->attach($mock); | |
389 | ||
390 | $pocketImport = $this->getPocketImport(); | |
391 | ||
392 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
393 | ->disableOriginalConstructor() | |
394 | ->getMock(); | |
395 | ||
396 | $entryRepo->expects($this->once()) | |
397 | ->method('findByUrlAndUserId') | |
398 | ->will($this->onConsecutiveCalls(false, true)); | |
399 | ||
400 | $this->em | |
401 | ->expects($this->once()) | |
402 | ->method('getRepository') | |
403 | ->willReturn($entryRepo); | |
404 | ||
405 | $entry = new Entry($this->user); | |
406 | ||
407 | $this->contentProxy | |
408 | ->expects($this->once()) | |
409 | ->method('updateEntry') | |
410 | ->will($this->throwException(new \Exception())); | |
411 | ||
412 | $pocketImport->setClient($client); | |
413 | $pocketImport->authorize('wunderbar_code'); | |
414 | ||
415 | $res = $pocketImport->import(); | |
416 | ||
417 | $this->assertTrue($res); | |
418 | $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary()); | |
419 | } | |
7ec2897e | 420 | } |