diff options
author | Jeremy Benoist <j0k3r@users.noreply.github.com> | 2016-09-19 07:15:40 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-09-19 07:15:40 +0200 |
commit | da18a4682f124b02278860d23ac1d59dee995277 (patch) | |
tree | eabbe9da7203eea41e0cb0ec3c26b9b6599cf58f /tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |
parent | 0ed8ce55b5caf2c88e8330afa83abef6c4aac9a4 (diff) | |
parent | 59b97fae996d8307b9d957d210d46200f6d206bf (diff) | |
download | wallabag-da18a4682f124b02278860d23ac1d59dee995277.tar.gz wallabag-da18a4682f124b02278860d23ac1d59dee995277.tar.zst wallabag-da18a4682f124b02278860d23ac1d59dee995277.zip |
Merge pull request #1941 from wallabag/v2-asynchronous-jobs
Use asynchronous jobs for imports
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import/PocketImportTest.php')
-rw-r--r-- | tests/Wallabag/ImportBundle/Import/PocketImportTest.php | 242 |
1 files changed, 204 insertions, 38 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index 8534e1c8..952521a2 100644 --- a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |||
@@ -4,21 +4,17 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
4 | 4 | ||
5 | use Wallabag\UserBundle\Entity\User; | 5 | use Wallabag\UserBundle\Entity\User; |
6 | use Wallabag\CoreBundle\Entity\Entry; | 6 | use Wallabag\CoreBundle\Entity\Entry; |
7 | use Wallabag\CoreBundle\Entity\Config; | ||
7 | use Wallabag\ImportBundle\Import\PocketImport; | 8 | use Wallabag\ImportBundle\Import\PocketImport; |
8 | use GuzzleHttp\Client; | 9 | use GuzzleHttp\Client; |
9 | use GuzzleHttp\Subscriber\Mock; | 10 | use GuzzleHttp\Subscriber\Mock; |
10 | use GuzzleHttp\Message\Response; | 11 | use GuzzleHttp\Message\Response; |
11 | use GuzzleHttp\Stream\Stream; | 12 | use GuzzleHttp\Stream\Stream; |
13 | use Wallabag\ImportBundle\Redis\Producer; | ||
12 | use Monolog\Logger; | 14 | use Monolog\Logger; |
13 | use Monolog\Handler\TestHandler; | 15 | use Monolog\Handler\TestHandler; |
14 | 16 | use Simpleue\Queue\RedisQueue; | |
15 | class PocketImportMock extends PocketImport | 17 | use M6Web\Component\RedisMock\RedisMockFactory; |
16 | { | ||
17 | public function getAccessToken() | ||
18 | { | ||
19 | return $this->accessToken; | ||
20 | } | ||
21 | } | ||
22 | 18 | ||
23 | class PocketImportTest extends \PHPUnit_Framework_TestCase | 19 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
24 | { | 20 | { |
@@ -32,45 +28,24 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
32 | { | 28 | { |
33 | $this->user = new User(); | 29 | $this->user = new User(); |
34 | 30 | ||
35 | $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') | 31 | $config = new Config($this->user); |
36 | ->disableOriginalConstructor() | 32 | $config->setPocketConsumerKey('xxx'); |
37 | ->getMock(); | ||
38 | 33 | ||
39 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | 34 | $this->user->setConfig($config); |
40 | ->disableOriginalConstructor() | ||
41 | ->getMock(); | ||
42 | 35 | ||
43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | 36 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
44 | ->disableOriginalConstructor() | 37 | ->disableOriginalConstructor() |
45 | ->getMock(); | 38 | ->getMock(); |
46 | 39 | ||
47 | $token->expects($this->once()) | ||
48 | ->method('getUser') | ||
49 | ->willReturn($this->user); | ||
50 | |||
51 | $this->tokenStorage->expects($this->once()) | ||
52 | ->method('getToken') | ||
53 | ->willReturn($token); | ||
54 | |||
55 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | 40 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
56 | ->disableOriginalConstructor() | 41 | ->disableOriginalConstructor() |
57 | ->getMock(); | 42 | ->getMock(); |
58 | 43 | ||
59 | $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') | 44 | $pocket = new PocketImport( |
60 | ->disableOriginalConstructor() | ||
61 | ->getMock(); | ||
62 | |||
63 | $config->expects($this->any()) | ||
64 | ->method('get') | ||
65 | ->with('pocket_consumer_key') | ||
66 | ->willReturn($consumerKey); | ||
67 | |||
68 | $pocket = new PocketImportMock( | ||
69 | $this->tokenStorage, | ||
70 | $this->em, | 45 | $this->em, |
71 | $this->contentProxy, | 46 | $this->contentProxy |
72 | $config | ||
73 | ); | 47 | ); |
48 | $pocket->setUser($this->user); | ||
74 | 49 | ||
75 | $this->logHandler = new TestHandler(); | 50 | $this->logHandler = new TestHandler(); |
76 | $logger = new Logger('test', [$this->logHandler]); | 51 | $logger = new Logger('test', [$this->logHandler]); |
@@ -189,10 +164,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
189 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 164 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
190 | "favorite": "1", | 165 | "favorite": "1", |
191 | "status": "1", | 166 | "status": "1", |
167 | "time_added": "1473020899", | ||
168 | "time_updated": "1473020899", | ||
169 | "time_read": "0", | ||
170 | "time_favorited": "0", | ||
171 | "sort_id": 0, | ||
192 | "resolved_title": "The Massive Ryder Cup Preview", | 172 | "resolved_title": "The Massive Ryder Cup Preview", |
193 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 173 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
194 | "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.", | 174 | "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.", |
195 | "is_article": "1", | 175 | "is_article": "1", |
176 | "is_index": "0", | ||
196 | "has_video": "1", | 177 | "has_video": "1", |
197 | "has_image": "1", | 178 | "has_image": "1", |
198 | "word_count": "3197", | 179 | "word_count": "3197", |
@@ -236,10 +217,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
236 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 217 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
237 | "favorite": "1", | 218 | "favorite": "1", |
238 | "status": "1", | 219 | "status": "1", |
220 | "time_added": "1473020899", | ||
221 | "time_updated": "1473020899", | ||
222 | "time_read": "0", | ||
223 | "time_favorited": "0", | ||
224 | "sort_id": 1, | ||
239 | "resolved_title": "The Massive Ryder Cup Preview", | 225 | "resolved_title": "The Massive Ryder Cup Preview", |
240 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 226 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", |
241 | "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.", | 227 | "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.", |
242 | "is_article": "1", | 228 | "is_article": "1", |
229 | "is_index": "0", | ||
243 | "has_video": "0", | 230 | "has_video": "0", |
244 | "has_image": "0", | 231 | "has_image": "0", |
245 | "word_count": "3197" | 232 | "word_count": "3197" |
@@ -279,7 +266,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
279 | $res = $pocketImport->import(); | 266 | $res = $pocketImport->import(); |
280 | 267 | ||
281 | $this->assertTrue($res); | 268 | $this->assertTrue($res); |
282 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | 269 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
283 | } | 270 | } |
284 | 271 | ||
285 | /** | 272 | /** |
@@ -302,6 +289,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
302 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 289 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
303 | "favorite": "1", | 290 | "favorite": "1", |
304 | "status": "1", | 291 | "status": "1", |
292 | "time_added": "1473020899", | ||
293 | "time_updated": "1473020899", | ||
294 | "time_read": "0", | ||
295 | "time_favorited": "0", | ||
296 | "sort_id": 0, | ||
305 | "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.", | 297 | "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.", |
306 | "is_article": "1", | 298 | "is_article": "1", |
307 | "has_video": "1", | 299 | "has_video": "1", |
@@ -315,6 +307,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
315 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 307 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
316 | "favorite": "1", | 308 | "favorite": "1", |
317 | "status": "0", | 309 | "status": "0", |
310 | "time_added": "1473020899", | ||
311 | "time_updated": "1473020899", | ||
312 | "time_read": "0", | ||
313 | "time_favorited": "0", | ||
314 | "sort_id": 1, | ||
318 | "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.", | 315 | "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.", |
319 | "is_article": "1", | 316 | "is_article": "1", |
320 | "has_video": "0", | 317 | "has_video": "0", |
@@ -364,7 +361,174 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
364 | $res = $pocketImport->setMarkAsRead(true)->import(); | 361 | $res = $pocketImport->setMarkAsRead(true)->import(); |
365 | 362 | ||
366 | $this->assertTrue($res); | 363 | $this->assertTrue($res); |
367 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | 364 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $pocketImport->getSummary()); |
365 | } | ||
366 | |||
367 | /** | ||
368 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
369 | */ | ||
370 | public function testImportWithRabbit() | ||
371 | { | ||
372 | $client = new Client(); | ||
373 | |||
374 | $body = <<<'JSON' | ||
375 | { | ||
376 | "item_id": "229279689", | ||
377 | "resolved_id": "229279689", | ||
378 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
379 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
380 | "favorite": "1", | ||
381 | "status": "1", | ||
382 | "time_added": "1473020899", | ||
383 | "time_updated": "1473020899", | ||
384 | "time_read": "0", | ||
385 | "time_favorited": "0", | ||
386 | "sort_id": 0, | ||
387 | "resolved_title": "The Massive Ryder Cup Preview", | ||
388 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
389 | "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.", | ||
390 | "is_article": "1", | ||
391 | "has_video": "0", | ||
392 | "has_image": "0", | ||
393 | "word_count": "3197" | ||
394 | } | ||
395 | JSON; | ||
396 | |||
397 | $mock = new Mock([ | ||
398 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
399 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
400 | { | ||
401 | "status": 1, | ||
402 | "list": { | ||
403 | "229279690": '.$body.' | ||
404 | } | ||
405 | } | ||
406 | ')), | ||
407 | ]); | ||
408 | |||
409 | $client->getEmitter()->attach($mock); | ||
410 | |||
411 | $pocketImport = $this->getPocketImport(); | ||
412 | |||
413 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
414 | ->disableOriginalConstructor() | ||
415 | ->getMock(); | ||
416 | |||
417 | $entryRepo->expects($this->never()) | ||
418 | ->method('findByUrlAndUserId'); | ||
419 | |||
420 | $this->em | ||
421 | ->expects($this->never()) | ||
422 | ->method('getRepository'); | ||
423 | |||
424 | $entry = new Entry($this->user); | ||
425 | |||
426 | $this->contentProxy | ||
427 | ->expects($this->never()) | ||
428 | ->method('updateEntry'); | ||
429 | |||
430 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
431 | ->disableOriginalConstructor() | ||
432 | ->getMock(); | ||
433 | |||
434 | $bodyAsArray = json_decode($body, true); | ||
435 | // because with just use `new User()` so it doesn't have an id | ||
436 | $bodyAsArray['userId'] = null; | ||
437 | |||
438 | $producer | ||
439 | ->expects($this->once()) | ||
440 | ->method('publish') | ||
441 | ->with(json_encode($bodyAsArray)); | ||
442 | |||
443 | $pocketImport->setClient($client); | ||
444 | $pocketImport->setProducer($producer); | ||
445 | $pocketImport->authorize('wunderbar_code'); | ||
446 | |||
447 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
448 | |||
449 | $this->assertTrue($res); | ||
450 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
451 | } | ||
452 | |||
453 | /** | ||
454 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
455 | */ | ||
456 | public function testImportWithRedis() | ||
457 | { | ||
458 | $client = new Client(); | ||
459 | |||
460 | $body = <<<'JSON' | ||
461 | { | ||
462 | "item_id": "229279689", | ||
463 | "resolved_id": "229279689", | ||
464 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
465 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
466 | "favorite": "1", | ||
467 | "status": "1", | ||
468 | "time_added": "1473020899", | ||
469 | "time_updated": "1473020899", | ||
470 | "time_read": "0", | ||
471 | "time_favorited": "0", | ||
472 | "sort_id": 0, | ||
473 | "resolved_title": "The Massive Ryder Cup Preview", | ||
474 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
475 | "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.", | ||
476 | "is_article": "1", | ||
477 | "has_video": "0", | ||
478 | "has_image": "0", | ||
479 | "word_count": "3197" | ||
480 | } | ||
481 | JSON; | ||
482 | |||
483 | $mock = new Mock([ | ||
484 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
485 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
486 | { | ||
487 | "status": 1, | ||
488 | "list": { | ||
489 | "229279690": '.$body.' | ||
490 | } | ||
491 | } | ||
492 | ')), | ||
493 | ]); | ||
494 | |||
495 | $client->getEmitter()->attach($mock); | ||
496 | |||
497 | $pocketImport = $this->getPocketImport(); | ||
498 | |||
499 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
500 | ->disableOriginalConstructor() | ||
501 | ->getMock(); | ||
502 | |||
503 | $entryRepo->expects($this->never()) | ||
504 | ->method('findByUrlAndUserId'); | ||
505 | |||
506 | $this->em | ||
507 | ->expects($this->never()) | ||
508 | ->method('getRepository'); | ||
509 | |||
510 | $entry = new Entry($this->user); | ||
511 | |||
512 | $this->contentProxy | ||
513 | ->expects($this->never()) | ||
514 | ->method('updateEntry'); | ||
515 | |||
516 | $factory = new RedisMockFactory(); | ||
517 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
518 | |||
519 | $queue = new RedisQueue($redisMock, 'pocket'); | ||
520 | $producer = new Producer($queue); | ||
521 | |||
522 | $pocketImport->setClient($client); | ||
523 | $pocketImport->setProducer($producer); | ||
524 | $pocketImport->authorize('wunderbar_code'); | ||
525 | |||
526 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
527 | |||
528 | $this->assertTrue($res); | ||
529 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
530 | |||
531 | $this->assertNotEmpty($redisMock->lpop('pocket')); | ||
368 | } | 532 | } |
369 | 533 | ||
370 | public function testImportBadResponse() | 534 | public function testImportBadResponse() |
@@ -402,6 +566,8 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
402 | "status": 1, | 566 | "status": 1, |
403 | "list": { | 567 | "list": { |
404 | "229279689": { | 568 | "229279689": { |
569 | "status": "1", | ||
570 | "favorite": "1", | ||
405 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | 571 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" |
406 | } | 572 | } |
407 | } | 573 | } |
@@ -439,6 +605,6 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
439 | $res = $pocketImport->import(); | 605 | $res = $pocketImport->import(); |
440 | 606 | ||
441 | $this->assertTrue($res); | 607 | $this->assertTrue($res); |
442 | $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary()); | 608 | $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
443 | } | 609 | } |
444 | } | 610 | } |