diff options
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import')
4 files changed, 601 insertions, 44 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 | } |
diff --git a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php new file mode 100644 index 00000000..d98cd486 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php | |||
@@ -0,0 +1,233 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\ReadabilityImport; | ||
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
9 | use Monolog\Logger; | ||
10 | use Monolog\Handler\TestHandler; | ||
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
13 | |||
14 | class ReadabilityImportTest extends \PHPUnit_Framework_TestCase | ||
15 | { | ||
16 | protected $user; | ||
17 | protected $em; | ||
18 | protected $logHandler; | ||
19 | protected $contentProxy; | ||
20 | |||
21 | private function getReadabilityImport($unsetUser = false) | ||
22 | { | ||
23 | $this->user = new User(); | ||
24 | |||
25 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
26 | ->disableOriginalConstructor() | ||
27 | ->getMock(); | ||
28 | |||
29 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
30 | ->disableOriginalConstructor() | ||
31 | ->getMock(); | ||
32 | |||
33 | $wallabag = new ReadabilityImport($this->em, $this->contentProxy); | ||
34 | |||
35 | $this->logHandler = new TestHandler(); | ||
36 | $logger = new Logger('test', [$this->logHandler]); | ||
37 | $wallabag->setLogger($logger); | ||
38 | |||
39 | if (false === $unsetUser) { | ||
40 | $wallabag->setUser($this->user); | ||
41 | } | ||
42 | |||
43 | return $wallabag; | ||
44 | } | ||
45 | |||
46 | public function testInit() | ||
47 | { | ||
48 | $readabilityImport = $this->getReadabilityImport(); | ||
49 | |||
50 | $this->assertEquals('Readability', $readabilityImport->getName()); | ||
51 | $this->assertNotEmpty($readabilityImport->getUrl()); | ||
52 | $this->assertEquals('import.readability.description', $readabilityImport->getDescription()); | ||
53 | } | ||
54 | |||
55 | public function testImport() | ||
56 | { | ||
57 | $readabilityImport = $this->getReadabilityImport(); | ||
58 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
59 | |||
60 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
61 | ->disableOriginalConstructor() | ||
62 | ->getMock(); | ||
63 | |||
64 | $entryRepo->expects($this->exactly(24)) | ||
65 | ->method('findByUrlAndUserId') | ||
66 | ->willReturn(false); | ||
67 | |||
68 | $this->em | ||
69 | ->expects($this->any()) | ||
70 | ->method('getRepository') | ||
71 | ->willReturn($entryRepo); | ||
72 | |||
73 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
74 | ->disableOriginalConstructor() | ||
75 | ->getMock(); | ||
76 | |||
77 | $this->contentProxy | ||
78 | ->expects($this->exactly(24)) | ||
79 | ->method('updateEntry') | ||
80 | ->willReturn($entry); | ||
81 | |||
82 | $res = $readabilityImport->import(); | ||
83 | |||
84 | $this->assertTrue($res); | ||
85 | $this->assertEquals(['skipped' => 0, 'imported' => 24, 'queued' => 0], $readabilityImport->getSummary()); | ||
86 | } | ||
87 | |||
88 | public function testImportAndMarkAllAsRead() | ||
89 | { | ||
90 | $readabilityImport = $this->getReadabilityImport(); | ||
91 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability-read.json'); | ||
92 | |||
93 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
94 | ->disableOriginalConstructor() | ||
95 | ->getMock(); | ||
96 | |||
97 | $entryRepo->expects($this->exactly(2)) | ||
98 | ->method('findByUrlAndUserId') | ||
99 | ->will($this->onConsecutiveCalls(false, true)); | ||
100 | |||
101 | $this->em | ||
102 | ->expects($this->any()) | ||
103 | ->method('getRepository') | ||
104 | ->willReturn($entryRepo); | ||
105 | |||
106 | $this->contentProxy | ||
107 | ->expects($this->exactly(1)) | ||
108 | ->method('updateEntry') | ||
109 | ->willReturn(new Entry($this->user)); | ||
110 | |||
111 | // check that every entry persisted are archived | ||
112 | $this->em | ||
113 | ->expects($this->any()) | ||
114 | ->method('persist') | ||
115 | ->with($this->callback(function ($persistedEntry) { | ||
116 | return $persistedEntry->isArchived(); | ||
117 | })); | ||
118 | |||
119 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
120 | |||
121 | $this->assertTrue($res); | ||
122 | |||
123 | $this->assertEquals(['skipped' => 1, 'imported' => 1, 'queued' => 0], $readabilityImport->getSummary()); | ||
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $readabilityImport = $this->getReadabilityImport(); | ||
129 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(24)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $readabilityImport->setProducer($producer); | ||
159 | |||
160 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $readabilityImport->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $readabilityImport = $this->getReadabilityImport(); | ||
169 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'readability'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $readabilityImport->setProducer($producer); | ||
197 | |||
198 | $res = $readabilityImport->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $readabilityImport->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('readability')); | ||
204 | } | ||
205 | |||
206 | public function testImportBadFile() | ||
207 | { | ||
208 | $readabilityImport = $this->getReadabilityImport(); | ||
209 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
210 | |||
211 | $res = $readabilityImport->import(); | ||
212 | |||
213 | $this->assertFalse($res); | ||
214 | |||
215 | $records = $this->logHandler->getRecords(); | ||
216 | $this->assertContains('ReadabilityImport: unable to read file', $records[0]['message']); | ||
217 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
218 | } | ||
219 | |||
220 | public function testImportUserNotDefined() | ||
221 | { | ||
222 | $readabilityImport = $this->getReadabilityImport(true); | ||
223 | $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); | ||
224 | |||
225 | $res = $readabilityImport->import(); | ||
226 | |||
227 | $this->assertFalse($res); | ||
228 | |||
229 | $records = $this->logHandler->getRecords(); | ||
230 | $this->assertContains('ReadabilityImport: user is not defined', $records[0]['message']); | ||
231 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
232 | } | ||
233 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php index bdc47dac..5ab4ad00 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php | |||
@@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
5 | use Wallabag\ImportBundle\Import\WallabagV1Import; | 5 | use Wallabag\ImportBundle\Import\WallabagV1Import; |
6 | use Wallabag\UserBundle\Entity\User; | 6 | use Wallabag\UserBundle\Entity\User; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
8 | use Monolog\Logger; | 9 | use Monolog\Logger; |
9 | use Monolog\Handler\TestHandler; | 10 | use Monolog\Handler\TestHandler; |
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
10 | 13 | ||
11 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | 14 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase |
12 | { | 15 | { |
@@ -79,7 +82,7 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |||
79 | $res = $wallabagV1Import->import(); | 82 | $res = $wallabagV1Import->import(); |
80 | 83 | ||
81 | $this->assertTrue($res); | 84 | $this->assertTrue($res); |
82 | $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary()); | 85 | $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
83 | } | 86 | } |
84 | 87 | ||
85 | public function testImportAndMarkAllAsRead() | 88 | public function testImportAndMarkAllAsRead() |
@@ -117,7 +120,87 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | |||
117 | 120 | ||
118 | $this->assertTrue($res); | 121 | $this->assertTrue($res); |
119 | 122 | ||
120 | $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary()); | 123 | $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary()); |
124 | } | ||
125 | |||
126 | public function testImportWithRabbit() | ||
127 | { | ||
128 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
129 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
130 | |||
131 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
132 | ->disableOriginalConstructor() | ||
133 | ->getMock(); | ||
134 | |||
135 | $entryRepo->expects($this->never()) | ||
136 | ->method('findByUrlAndUserId'); | ||
137 | |||
138 | $this->em | ||
139 | ->expects($this->never()) | ||
140 | ->method('getRepository'); | ||
141 | |||
142 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $this->contentProxy | ||
147 | ->expects($this->never()) | ||
148 | ->method('updateEntry'); | ||
149 | |||
150 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
151 | ->disableOriginalConstructor() | ||
152 | ->getMock(); | ||
153 | |||
154 | $producer | ||
155 | ->expects($this->exactly(4)) | ||
156 | ->method('publish'); | ||
157 | |||
158 | $wallabagV1Import->setProducer($producer); | ||
159 | |||
160 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | ||
161 | |||
162 | $this->assertTrue($res); | ||
163 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); | ||
164 | } | ||
165 | |||
166 | public function testImportWithRedis() | ||
167 | { | ||
168 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
169 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
170 | |||
171 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
172 | ->disableOriginalConstructor() | ||
173 | ->getMock(); | ||
174 | |||
175 | $entryRepo->expects($this->never()) | ||
176 | ->method('findByUrlAndUserId'); | ||
177 | |||
178 | $this->em | ||
179 | ->expects($this->never()) | ||
180 | ->method('getRepository'); | ||
181 | |||
182 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
183 | ->disableOriginalConstructor() | ||
184 | ->getMock(); | ||
185 | |||
186 | $this->contentProxy | ||
187 | ->expects($this->never()) | ||
188 | ->method('updateEntry'); | ||
189 | |||
190 | $factory = new RedisMockFactory(); | ||
191 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
192 | |||
193 | $queue = new RedisQueue($redisMock, 'wallabag_v1'); | ||
194 | $producer = new Producer($queue); | ||
195 | |||
196 | $wallabagV1Import->setProducer($producer); | ||
197 | |||
198 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | ||
199 | |||
200 | $this->assertTrue($res); | ||
201 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary()); | ||
202 | |||
203 | $this->assertNotEmpty($redisMock->lpop('wallabag_v1')); | ||
121 | } | 204 | } |
122 | 205 | ||
123 | public function testImportBadFile() | 206 | public function testImportBadFile() |
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php index 4a45e0f0..12bd6bdd 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php | |||
@@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; | |||
5 | use Wallabag\ImportBundle\Import\WallabagV2Import; | 5 | use Wallabag\ImportBundle\Import\WallabagV2Import; |
6 | use Wallabag\UserBundle\Entity\User; | 6 | use Wallabag\UserBundle\Entity\User; |
7 | use Wallabag\CoreBundle\Entity\Entry; | 7 | use Wallabag\CoreBundle\Entity\Entry; |
8 | use Wallabag\ImportBundle\Redis\Producer; | ||
8 | use Monolog\Logger; | 9 | use Monolog\Logger; |
9 | use Monolog\Handler\TestHandler; | 10 | use Monolog\Handler\TestHandler; |
11 | use Simpleue\Queue\RedisQueue; | ||
12 | use M6Web\Component\RedisMock\RedisMockFactory; | ||
10 | 13 | ||
11 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | 14 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase |
12 | { | 15 | { |
@@ -75,7 +78,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
75 | $res = $wallabagV2Import->import(); | 78 | $res = $wallabagV2Import->import(); |
76 | 79 | ||
77 | $this->assertTrue($res); | 80 | $this->assertTrue($res); |
78 | $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); | 81 | $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
79 | } | 82 | } |
80 | 83 | ||
81 | public function testImportAndMarkAllAsRead() | 84 | public function testImportAndMarkAllAsRead() |
@@ -113,7 +116,79 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
113 | 116 | ||
114 | $this->assertTrue($res); | 117 | $this->assertTrue($res); |
115 | 118 | ||
116 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); | 119 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
120 | } | ||
121 | |||
122 | public function testImportWithRabbit() | ||
123 | { | ||
124 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
125 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
126 | |||
127 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
128 | ->disableOriginalConstructor() | ||
129 | ->getMock(); | ||
130 | |||
131 | $entryRepo->expects($this->never()) | ||
132 | ->method('findByUrlAndUserId'); | ||
133 | |||
134 | $this->em | ||
135 | ->expects($this->never()) | ||
136 | ->method('getRepository'); | ||
137 | |||
138 | $this->contentProxy | ||
139 | ->expects($this->never()) | ||
140 | ->method('updateEntry'); | ||
141 | |||
142 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | ||
143 | ->disableOriginalConstructor() | ||
144 | ->getMock(); | ||
145 | |||
146 | $producer | ||
147 | ->expects($this->exactly(24)) | ||
148 | ->method('publish'); | ||
149 | |||
150 | $wallabagV2Import->setProducer($producer); | ||
151 | |||
152 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | ||
153 | |||
154 | $this->assertTrue($res); | ||
155 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); | ||
156 | } | ||
157 | |||
158 | public function testImportWithRedis() | ||
159 | { | ||
160 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
161 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
162 | |||
163 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
164 | ->disableOriginalConstructor() | ||
165 | ->getMock(); | ||
166 | |||
167 | $entryRepo->expects($this->never()) | ||
168 | ->method('findByUrlAndUserId'); | ||
169 | |||
170 | $this->em | ||
171 | ->expects($this->never()) | ||
172 | ->method('getRepository'); | ||
173 | |||
174 | $this->contentProxy | ||
175 | ->expects($this->never()) | ||
176 | ->method('updateEntry'); | ||
177 | |||
178 | $factory = new RedisMockFactory(); | ||
179 | $redisMock = $factory->getAdapter('Predis\Client', true); | ||
180 | |||
181 | $queue = new RedisQueue($redisMock, 'wallabag_v2'); | ||
182 | $producer = new Producer($queue); | ||
183 | |||
184 | $wallabagV2Import->setProducer($producer); | ||
185 | |||
186 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | ||
187 | |||
188 | $this->assertTrue($res); | ||
189 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary()); | ||
190 | |||
191 | $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); | ||
117 | } | 192 | } |
118 | 193 | ||
119 | public function testImportBadFile() | 194 | public function testImportBadFile() |
@@ -152,7 +227,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
152 | $res = $wallabagV2Import->import(); | 227 | $res = $wallabagV2Import->import(); |
153 | 228 | ||
154 | $this->assertFalse($res); | 229 | $this->assertFalse($res); |
155 | $this->assertEquals(['skipped' => 0, 'imported' => 0], $wallabagV2Import->getSummary()); | 230 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary()); |
156 | } | 231 | } |
157 | 232 | ||
158 | public function testImportWithExceptionFromGraby() | 233 | public function testImportWithExceptionFromGraby() |
@@ -181,6 +256,6 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | |||
181 | $res = $wallabagV2Import->import(); | 256 | $res = $wallabagV2Import->import(); |
182 | 257 | ||
183 | $this->assertTrue($res); | 258 | $this->assertTrue($res); |
184 | $this->assertEquals(['skipped' => 24, 'imported' => 0], $wallabagV2Import->getSummary()); | 259 | $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
185 | } | 260 | } |
186 | } | 261 | } |