diff options
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import/PocketImportTest.php')
-rw-r--r-- | tests/Wallabag/ImportBundle/Import/PocketImportTest.php | 248 |
1 files changed, 214 insertions, 34 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index 8534e1c8..9ec7935c 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,38 @@ 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 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') |
60 | ->disableOriginalConstructor() | 45 | ->disableOriginalConstructor() |
61 | ->getMock(); | 46 | ->getMock(); |
62 | 47 | ||
63 | $config->expects($this->any()) | 48 | $this->em |
64 | ->method('get') | 49 | ->expects($this->any()) |
65 | ->with('pocket_consumer_key') | 50 | ->method('getUnitOfWork') |
66 | ->willReturn($consumerKey); | 51 | ->willReturn($this->uow); |
52 | |||
53 | $this->uow | ||
54 | ->expects($this->any()) | ||
55 | ->method('getScheduledEntityInsertions') | ||
56 | ->willReturn([]); | ||
67 | 57 | ||
68 | $pocket = new PocketImportMock( | 58 | $pocket = new PocketImport( |
69 | $this->tokenStorage, | ||
70 | $this->em, | 59 | $this->em, |
71 | $this->contentProxy, | 60 | $this->contentProxy |
72 | $config | ||
73 | ); | 61 | ); |
62 | $pocket->setUser($this->user); | ||
74 | 63 | ||
75 | $this->logHandler = new TestHandler(); | 64 | $this->logHandler = new TestHandler(); |
76 | $logger = new Logger('test', [$this->logHandler]); | 65 | $logger = new Logger('test', [$this->logHandler]); |
@@ -189,10 +178,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
189 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 178 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
190 | "favorite": "1", | 179 | "favorite": "1", |
191 | "status": "1", | 180 | "status": "1", |
181 | "time_added": "1473020899", | ||
182 | "time_updated": "1473020899", | ||
183 | "time_read": "0", | ||
184 | "time_favorited": "0", | ||
185 | "sort_id": 0, | ||
192 | "resolved_title": "The Massive Ryder Cup Preview", | 186 | "resolved_title": "The Massive Ryder Cup Preview", |
193 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 187 | "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.", | 188 | "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", | 189 | "is_article": "1", |
190 | "is_index": "0", | ||
196 | "has_video": "1", | 191 | "has_video": "1", |
197 | "has_image": "1", | 192 | "has_image": "1", |
198 | "word_count": "3197", | 193 | "word_count": "3197", |
@@ -236,10 +231,16 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
236 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 231 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
237 | "favorite": "1", | 232 | "favorite": "1", |
238 | "status": "1", | 233 | "status": "1", |
234 | "time_added": "1473020899", | ||
235 | "time_updated": "1473020899", | ||
236 | "time_read": "0", | ||
237 | "time_favorited": "0", | ||
238 | "sort_id": 1, | ||
239 | "resolved_title": "The Massive Ryder Cup Preview", | 239 | "resolved_title": "The Massive Ryder Cup Preview", |
240 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | 240 | "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.", | 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.", |
242 | "is_article": "1", | 242 | "is_article": "1", |
243 | "is_index": "0", | ||
243 | "has_video": "0", | 244 | "has_video": "0", |
244 | "has_image": "0", | 245 | "has_image": "0", |
245 | "word_count": "3197" | 246 | "word_count": "3197" |
@@ -279,7 +280,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
279 | $res = $pocketImport->import(); | 280 | $res = $pocketImport->import(); |
280 | 281 | ||
281 | $this->assertTrue($res); | 282 | $this->assertTrue($res); |
282 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | 283 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
283 | } | 284 | } |
284 | 285 | ||
285 | /** | 286 | /** |
@@ -302,6 +303,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
302 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 303 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
303 | "favorite": "1", | 304 | "favorite": "1", |
304 | "status": "1", | 305 | "status": "1", |
306 | "time_added": "1473020899", | ||
307 | "time_updated": "1473020899", | ||
308 | "time_read": "0", | ||
309 | "time_favorited": "0", | ||
310 | "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.", | 311 | "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", | 312 | "is_article": "1", |
307 | "has_video": "1", | 313 | "has_video": "1", |
@@ -315,6 +321,11 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
315 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | 321 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", |
316 | "favorite": "1", | 322 | "favorite": "1", |
317 | "status": "0", | 323 | "status": "0", |
324 | "time_added": "1473020899", | ||
325 | "time_updated": "1473020899", | ||
326 | "time_read": "0", | ||
327 | "time_favorited": "0", | ||
328 | "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.", | 329 | "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", | 330 | "is_article": "1", |
320 | "has_video": "0", | 331 | "has_video": "0", |
@@ -364,7 +375,174 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
364 | $res = $pocketImport->setMarkAsRead(true)->import(); | 375 | $res = $pocketImport->setMarkAsRead(true)->import(); |
365 | 376 | ||
366 | $this->assertTrue($res); | 377 | $this->assertTrue($res); |
367 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | 378 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $pocketImport->getSummary()); |
379 | } | ||
380 | |||
381 | /** | ||
382 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
383 | */ | ||
384 | public function testImportWithRabbit() | ||
385 | { | ||
386 | $client = new Client(); | ||
387 | |||
388 | $body = <<<'JSON' | ||
389 | { | ||
390 | "item_id": "229279689", | ||
391 | "resolved_id": "229279689", | ||
392 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
393 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
394 | "favorite": "1", | ||
395 | "status": "1", | ||
396 | "time_added": "1473020899", | ||
397 | "time_updated": "1473020899", | ||
398 | "time_read": "0", | ||
399 | "time_favorited": "0", | ||
400 | "sort_id": 0, | ||
401 | "resolved_title": "The Massive Ryder Cup Preview", | ||
402 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
403 | "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.", | ||
404 | "is_article": "1", | ||
405 | "has_video": "0", | ||
406 | "has_image": "0", | ||
407 | "word_count": "3197" | ||
408 | } | ||
409 | JSON; | ||
410 | |||
411 | $mock = new Mock([ | ||
412 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
413 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
414 | { | ||
415 | "status": 1, | ||
416 | "list": { | ||
417 | "229279690": '.$body.' | ||
418 | } | ||
419 | } | ||
420 | ')), | ||
421 | ]); | ||
422 | |||
423 | $client->getEmitter()->attach($mock); | ||
424 | |||
425 | $pocketImport = $this->getPocketImport(); | ||
426 | |||
427 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
428 | ->disableOriginalConstructor() | ||
429 | ->getMock(); | ||
430 | |||
431 | $entryRepo->expects($this->never()) | ||
432 | ->method('findByUrlAndUserId'); | ||
433 | |||
434 | $this->em | ||
435 | ->expects($this->never()) | ||
436 | ->method('getRepository'); | ||
437 | |||
438 | $entry = new Entry($this->user); | ||
439 | |||
440 | $this->contentProxy | ||
441 | ->expects($this->never()) | ||
442 | ->method('updateEntry'); | ||
443 | |||
444 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
445 | ->disableOriginalConstructor() | ||
446 | ->getMock(); | ||
447 | |||
448 | $bodyAsArray = json_decode($body, true); | ||
449 | // because with just use `new User()` so it doesn't have an id | ||
450 | $bodyAsArray['userId'] = null; | ||
451 | |||
452 | $producer | ||
453 | ->expects($this->once()) | ||
454 | ->method('publish') | ||
455 | ->with(json_encode($bodyAsArray)); | ||
456 | |||
457 | $pocketImport->setClient($client); | ||
458 | $pocketImport->setProducer($producer); | ||
459 | $pocketImport->authorize('wunderbar_code'); | ||
460 | |||
461 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
462 | |||
463 | $this->assertTrue($res); | ||
464 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
465 | } | ||
466 | |||
467 | /** | ||
468 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
469 | */ | ||
470 | public function testImportWithRedis() | ||
471 | { | ||
472 | $client = new Client(); | ||
473 | |||
474 | $body = <<<'JSON' | ||
475 | { | ||
476 | "item_id": "229279689", | ||
477 | "resolved_id": "229279689", | ||
478 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
479 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
480 | "favorite": "1", | ||
481 | "status": "1", | ||
482 | "time_added": "1473020899", | ||
483 | "time_updated": "1473020899", | ||
484 | "time_read": "0", | ||
485 | "time_favorited": "0", | ||
486 | "sort_id": 0, | ||
487 | "resolved_title": "The Massive Ryder Cup Preview", | ||
488 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
489 | "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.", | ||
490 | "is_article": "1", | ||
491 | "has_video": "0", | ||
492 | "has_image": "0", | ||
493 | "word_count": "3197" | ||
494 | } | ||
495 | JSON; | ||
496 | |||
497 | $mock = new Mock([ | ||
498 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
499 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
500 | { | ||
501 | "status": 1, | ||
502 | "list": { | ||
503 | "229279690": '.$body.' | ||
504 | } | ||
505 | } | ||
506 | ')), | ||
507 | ]); | ||
508 | |||
509 | $client->getEmitter()->attach($mock); | ||
510 | |||
511 | $pocketImport = $this->getPocketImport(); | ||
512 | |||
513 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
514 | ->disableOriginalConstructor() | ||
515 | ->getMock(); | ||
516 | |||
517 | $entryRepo->expects($this->never()) | ||
518 | ->method('findByUrlAndUserId'); | ||
519 | |||
520 | $this->em | ||
521 | ->expects($this->never()) | ||
522 | ->method('getRepository'); | ||
523 | |||
524 | $entry = new Entry($this->user); | ||
525 | |||
526 | $this->contentProxy | ||
527 | ->expects($this->never()) | ||
528 | ->method('updateEntry'); | ||
529 | |||
530 | $factory = new RedisMockFactory(); | ||
531 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
532 | |||
533 | $queue = new RedisQueue($redisMock, 'pocket'); | ||
534 | $producer = new Producer($queue); | ||
535 | |||
536 | $pocketImport->setClient($client); | ||
537 | $pocketImport->setProducer($producer); | ||
538 | $pocketImport->authorize('wunderbar_code'); | ||
539 | |||
540 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
541 | |||
542 | $this->assertTrue($res); | ||
543 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 1], $pocketImport->getSummary()); | ||
544 | |||
545 | $this->assertNotEmpty($redisMock->lpop('pocket')); | ||
368 | } | 546 | } |
369 | 547 | ||
370 | public function testImportBadResponse() | 548 | public function testImportBadResponse() |
@@ -402,6 +580,8 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
402 | "status": 1, | 580 | "status": 1, |
403 | "list": { | 581 | "list": { |
404 | "229279689": { | 582 | "229279689": { |
583 | "status": "1", | ||
584 | "favorite": "1", | ||
405 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" | 585 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview" |
406 | } | 586 | } |
407 | } | 587 | } |
@@ -439,6 +619,6 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
439 | $res = $pocketImport->import(); | 619 | $res = $pocketImport->import(); |
440 | 620 | ||
441 | $this->assertTrue($res); | 621 | $this->assertTrue($res); |
442 | $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary()); | 622 | $this->assertEquals(['skipped' => 0, 'imported' => 1, 'queued' => 0], $pocketImport->getSummary()); |
443 | } | 623 | } |
444 | } | 624 | } |