From b3437d58ae224121375c99e9288d8b808524e624 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 9 Sep 2016 21:02:03 +0200 Subject: Enable Redis async import - using javibravo/simpleue - internal config value are now `import_with_redis` & `import_with_rabbit` which are more clear - if both option are enable rabbit will be choosen - services imports related to async are now splitted into 2 files: `redis.yml` & `rabbit.yml` - --- .../Consumer/AMPQ/EntryConsumerTest.php | 225 --------------------- .../Consumer/AMPQEntryConsumerTest.php | 225 +++++++++++++++++++++ .../Consumer/RedisEntryConsumerTest.php | 224 ++++++++++++++++++++ .../Controller/PocketControllerTest.php | 19 +- .../Controller/ReadabilityControllerTest.php | 20 +- .../Controller/WallabagV1ControllerTest.php | 20 +- .../Controller/WallabagV2ControllerTest.php | 20 +- .../ImportBundle/Import/PocketImportTest.php | 86 +++++++- .../ImportBundle/Import/ReadabilityImportTest.php | 45 ++++- .../ImportBundle/Import/WallabagV1ImportTest.php | 45 ++++- .../ImportBundle/Import/WallabagV2ImportTest.php | 41 +++- 11 files changed, 733 insertions(+), 237 deletions(-) delete mode 100644 tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php create mode 100644 tests/Wallabag/ImportBundle/Consumer/AMPQEntryConsumerTest.php create mode 100644 tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php (limited to 'tests/Wallabag/ImportBundle') diff --git a/tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php b/tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php deleted file mode 100644 index 7141874c..00000000 --- a/tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php +++ /dev/null @@ -1,225 +0,0 @@ -getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $em - ->expects($this->once()) - ->method('flush'); - - $em - ->expects($this->exactly(2)) - ->method('clear'); - - $body = <<<'JSON' -{ - "item_id": "1402935436", - "resolved_id": "1402935436", - "given_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", - "given_title": "Leslie Jones is back on Twitter and her comeback tweet rules", - "favorite": "0", - "status": "0", - "time_added": "1473020899", - "time_updated": "1473020899", - "time_read": "0", - "time_favorited": "0", - "sort_id": 0, - "resolved_title": "Leslie Jones is back on Twitter and her comeback tweet rules", - "resolved_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", - "excerpt": "Leslie Jones is back to communicating with her adoring public on Twitter after cowardly hacker-trolls drove her away, probably to compensate for their own failings. It all started with a mic drop ...", - "is_article": "1", - "is_index": "0", - "has_video": "0", - "has_image": "1", - "word_count": "200", - "tags": { - "ifttt": { - "item_id": "1402935436", - "tag": "ifttt" - }, - "mashable": { - "item_id": "1402935436", - "tag": "mashable" - } - }, - "authors": { - "2484273": { - "item_id": "1402935436", - "author_id": "2484273", - "name": "Adam Rosenberg", - "url": "http://mashable.com/author/adam-rosenberg/" - } - }, - "image": { - "item_id": "1402935436", - "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", - "width": "0", - "height": "0" - }, - "images": { - "1": { - "item_id": "1402935436", - "image_id": "1", - "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", - "width": "0", - "height": "0", - "credit": "Image: Steve Eichner/NameFace/Sipa USA", - "caption": "" - } - }, - "userId": 1 -} -JSON; - - $user = new User(); - $entry = new Entry($user); - - $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') - ->disableOriginalConstructor() - ->getMock(); - - $userRepository - ->expects($this->once()) - ->method('find') - // userId from the body json above - ->with(1) - ->willReturn($user); - - $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') - ->disableOriginalConstructor() - ->getMock(); - - $import - ->expects($this->once()) - ->method('setUser') - ->with($user); - - $import - ->expects($this->once()) - ->method('parseEntry') - ->with(json_decode($body, true)) - ->willReturn($entry); - - $consumer = new EntryConsumer( - $em, - $userRepository, - $import - ); - - $message = new AMQPMessage($body); - - $consumer->execute($message); - } - - public function testMessageWithBadUser() - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $em - ->expects($this->never()) - ->method('flush'); - - $em - ->expects($this->never()) - ->method('clear'); - - $body = '{ "userId": 123 }'; - - $user = new User(); - $entry = new Entry($user); - - $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') - ->disableOriginalConstructor() - ->getMock(); - - $userRepository - ->expects($this->once()) - ->method('find') - // userId from the body json above - ->with(123) - ->willReturn(null); - - $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') - ->disableOriginalConstructor() - ->getMock(); - - $consumer = new EntryConsumer( - $em, - $userRepository, - $import - ); - - $message = new AMQPMessage($body); - - $consumer->execute($message); - } - - public function testMessageWithEntryProcessed() - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $em - ->expects($this->never()) - ->method('flush'); - - $em - ->expects($this->never()) - ->method('clear'); - - $body = '{ "userId": 123 }'; - - $user = new User(); - - $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') - ->disableOriginalConstructor() - ->getMock(); - - $userRepository - ->expects($this->once()) - ->method('find') - // userId from the body json above - ->with(123) - ->willReturn($user); - - $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') - ->disableOriginalConstructor() - ->getMock(); - - $import - ->expects($this->once()) - ->method('setUser') - ->with($user); - - $import - ->expects($this->once()) - ->method('parseEntry') - ->with(json_decode($body, true)) - ->willReturn(null); - - $consumer = new EntryConsumer( - $em, - $userRepository, - $import - ); - - $message = new AMQPMessage($body); - - $consumer->execute($message); - } -} diff --git a/tests/Wallabag/ImportBundle/Consumer/AMPQEntryConsumerTest.php b/tests/Wallabag/ImportBundle/Consumer/AMPQEntryConsumerTest.php new file mode 100644 index 00000000..b13ade1d --- /dev/null +++ b/tests/Wallabag/ImportBundle/Consumer/AMPQEntryConsumerTest.php @@ -0,0 +1,225 @@ +getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->once()) + ->method('flush'); + + $em + ->expects($this->exactly(2)) + ->method('clear'); + + $body = <<<'JSON' +{ + "item_id": "1402935436", + "resolved_id": "1402935436", + "given_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", + "given_title": "Leslie Jones is back on Twitter and her comeback tweet rules", + "favorite": "0", + "status": "0", + "time_added": "1473020899", + "time_updated": "1473020899", + "time_read": "0", + "time_favorited": "0", + "sort_id": 0, + "resolved_title": "Leslie Jones is back on Twitter and her comeback tweet rules", + "resolved_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", + "excerpt": "Leslie Jones is back to communicating with her adoring public on Twitter after cowardly hacker-trolls drove her away, probably to compensate for their own failings. It all started with a mic drop ...", + "is_article": "1", + "is_index": "0", + "has_video": "0", + "has_image": "1", + "word_count": "200", + "tags": { + "ifttt": { + "item_id": "1402935436", + "tag": "ifttt" + }, + "mashable": { + "item_id": "1402935436", + "tag": "mashable" + } + }, + "authors": { + "2484273": { + "item_id": "1402935436", + "author_id": "2484273", + "name": "Adam Rosenberg", + "url": "http://mashable.com/author/adam-rosenberg/" + } + }, + "image": { + "item_id": "1402935436", + "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", + "width": "0", + "height": "0" + }, + "images": { + "1": { + "item_id": "1402935436", + "image_id": "1", + "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", + "width": "0", + "height": "0", + "credit": "Image: Steve Eichner/NameFace/Sipa USA", + "caption": "" + } + }, + "userId": 1 +} +JSON; + + $user = new User(); + $entry = new Entry($user); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(1) + ->willReturn($user); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $import + ->expects($this->once()) + ->method('setUser') + ->with($user); + + $import + ->expects($this->once()) + ->method('parseEntry') + ->with(json_decode($body, true)) + ->willReturn($entry); + + $consumer = new AMPQEntryConsumer( + $em, + $userRepository, + $import + ); + + $message = new AMQPMessage($body); + + $consumer->execute($message); + } + + public function testMessageWithBadUser() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->never()) + ->method('flush'); + + $em + ->expects($this->never()) + ->method('clear'); + + $body = '{ "userId": 123 }'; + + $user = new User(); + $entry = new Entry($user); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(123) + ->willReturn(null); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $consumer = new AMPQEntryConsumer( + $em, + $userRepository, + $import + ); + + $message = new AMQPMessage($body); + + $consumer->execute($message); + } + + public function testMessageWithEntryProcessed() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->never()) + ->method('flush'); + + $em + ->expects($this->never()) + ->method('clear'); + + $body = '{ "userId": 123 }'; + + $user = new User(); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(123) + ->willReturn($user); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $import + ->expects($this->once()) + ->method('setUser') + ->with($user); + + $import + ->expects($this->once()) + ->method('parseEntry') + ->with(json_decode($body, true)) + ->willReturn(null); + + $consumer = new AMPQEntryConsumer( + $em, + $userRepository, + $import + ); + + $message = new AMQPMessage($body); + + $consumer->execute($message); + } +} diff --git a/tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php b/tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php new file mode 100644 index 00000000..0ce7ce49 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php @@ -0,0 +1,224 @@ +getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->once()) + ->method('flush'); + + $em + ->expects($this->exactly(2)) + ->method('clear'); + + $body = <<<'JSON' +{ + "item_id": "1402935436", + "resolved_id": "1402935436", + "given_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", + "given_title": "Leslie Jones is back on Twitter and her comeback tweet rules", + "favorite": "0", + "status": "0", + "time_added": "1473020899", + "time_updated": "1473020899", + "time_read": "0", + "time_favorited": "0", + "sort_id": 0, + "resolved_title": "Leslie Jones is back on Twitter and her comeback tweet rules", + "resolved_url": "http://mashable.com/2016/09/04/leslie-jones-back-on-twitter-after-hack/?utm_campaign=Mash-Prod-RSS-Feedburner-All-Partial&utm_cid=Mash-Prod-RSS-Feedburner-All-Partial", + "excerpt": "Leslie Jones is back to communicating with her adoring public on Twitter after cowardly hacker-trolls drove her away, probably to compensate for their own failings. It all started with a mic drop ...", + "is_article": "1", + "is_index": "0", + "has_video": "0", + "has_image": "1", + "word_count": "200", + "tags": { + "ifttt": { + "item_id": "1402935436", + "tag": "ifttt" + }, + "mashable": { + "item_id": "1402935436", + "tag": "mashable" + } + }, + "authors": { + "2484273": { + "item_id": "1402935436", + "author_id": "2484273", + "name": "Adam Rosenberg", + "url": "http://mashable.com/author/adam-rosenberg/" + } + }, + "image": { + "item_id": "1402935436", + "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", + "width": "0", + "height": "0" + }, + "images": { + "1": { + "item_id": "1402935436", + "image_id": "1", + "src": "http://i.amz.mshcdn.com/i-V5cS6_sDqFABaVR0hVSBJqG_w=/950x534/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F199899%2Fleslie_jones_war_dogs.jpg", + "width": "0", + "height": "0", + "credit": "Image: Steve Eichner/NameFace/Sipa USA", + "caption": "" + } + }, + "userId": 1 +} +JSON; + + $user = new User(); + $entry = new Entry($user); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(1) + ->willReturn($user); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $import + ->expects($this->once()) + ->method('setUser') + ->with($user); + + $import + ->expects($this->once()) + ->method('parseEntry') + ->with(json_decode($body, true)) + ->willReturn($entry); + + $consumer = new RedisEntryConsumer( + $em, + $userRepository, + $import + ); + + $res = $consumer->manage($body); + + $this->assertTrue($res); + } + + public function testMessageWithBadUser() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->never()) + ->method('flush'); + + $em + ->expects($this->never()) + ->method('clear'); + + $body = '{ "userId": 123 }'; + + $user = new User(); + $entry = new Entry($user); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(123) + ->willReturn(null); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $consumer = new RedisEntryConsumer( + $em, + $userRepository, + $import + ); + + $res = $consumer->manage($body); + + $this->assertFalse($res); + } + + public function testMessageWithEntryProcessed() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $em + ->expects($this->never()) + ->method('flush'); + + $em + ->expects($this->never()) + ->method('clear'); + + $body = '{ "userId": 123 }'; + + $user = new User(); + + $userRepository = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository') + ->disableOriginalConstructor() + ->getMock(); + + $userRepository + ->expects($this->once()) + ->method('find') + // userId from the body json above + ->with(123) + ->willReturn($user); + + $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\AbstractImport') + ->disableOriginalConstructor() + ->getMock(); + + $import + ->expects($this->once()) + ->method('setUser') + ->with($user); + + $import + ->expects($this->once()) + ->method('parseEntry') + ->with(json_decode($body, true)) + ->willReturn(null); + + $consumer = new RedisEntryConsumer( + $em, + $userRepository, + $import + ); + + $res = $consumer->manage($body); + + $this->assertFalse($res); + } +} diff --git a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php index 098cf356..35673261 100644 --- a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php @@ -22,14 +22,29 @@ class PocketControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $client->getContainer()->get('craue_config')->set('rabbitmq', 1); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1); $crawler = $client->request('GET', '/import/pocket'); $this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(1, $crawler->filter('button[type=submit]')->count()); - $client->getContainer()->get('craue_config')->set('rabbitmq', 0); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0); + } + + public function testImportPocketWithRedisEnabled() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 1); + + $crawler = $client->request('GET', '/import/pocket'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertEquals(1, $crawler->filter('button[type=submit]')->count()); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 0); } public function testImportPocketAuthBadToken() diff --git a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php index e12a723d..69635382 100644 --- a/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php @@ -24,7 +24,7 @@ class ReadabilityControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $client->getContainer()->get('craue_config')->set('rabbitmq', 1); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1); $crawler = $client->request('GET', '/import/readability'); @@ -32,7 +32,23 @@ class ReadabilityControllerTest extends WallabagCoreTestCase $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); - $client->getContainer()->get('craue_config')->set('rabbitmq', 0); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0); + } + + public function testImportReadabilityWithRedisEnabled() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 1); + + $crawler = $client->request('GET', '/import/readability'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); + $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 0); } public function testImportReadabilityWithFile() diff --git a/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php b/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php index 96556717..933ddd6c 100644 --- a/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php @@ -24,7 +24,7 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $client->getContainer()->get('craue_config')->set('rabbitmq', 1); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1); $crawler = $client->request('GET', '/import/wallabag-v1'); @@ -32,7 +32,23 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); - $client->getContainer()->get('craue_config')->set('rabbitmq', 0); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0); + } + + public function testImportWallabagWithRedisEnabled() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 1); + + $crawler = $client->request('GET', '/import/wallabag-v1'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); + $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 0); } public function testImportWallabagWithFile() diff --git a/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php b/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php index 250d0d3e..36e5221d 100644 --- a/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php @@ -24,7 +24,7 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $client->getContainer()->get('craue_config')->set('rabbitmq', 1); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 1); $crawler = $client->request('GET', '/import/wallabag-v2'); @@ -32,7 +32,23 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); - $client->getContainer()->get('craue_config')->set('rabbitmq', 0); + $client->getContainer()->get('craue_config')->set('import_with_rabbitmq', 0); + } + + public function testImportWallabagWithRedisEnabled() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 1); + + $crawler = $client->request('GET', '/import/wallabag-v2'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertEquals(1, $crawler->filter('form[name=upload_import_file] > button[type=submit]')->count()); + $this->assertEquals(1, $crawler->filter('input[type=file]')->count()); + + $client->getContainer()->get('craue_config')->set('import_with_redis', 0); } public function testImportWallabagWithFile() diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php index 1750e3a1..425fa321 100644 --- a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php @@ -9,8 +9,11 @@ use GuzzleHttp\Client; use GuzzleHttp\Subscriber\Mock; use GuzzleHttp\Message\Response; use GuzzleHttp\Stream\Stream; +use Wallabag\ImportBundle\Redis\Producer; use Monolog\Logger; use Monolog\Handler\TestHandler; +use Simpleue\Queue\RedisQueue; +use M6Web\Component\RedisMock\RedisMockFactory; class PocketImportTest extends \PHPUnit_Framework_TestCase { @@ -442,7 +445,7 @@ JSON; ->with(json_encode($bodyAsArray)); $pocketImport->setClient($client); - $pocketImport->setRabbitmqProducer($producer); + $pocketImport->setProducer($producer); $pocketImport->authorize('wunderbar_code'); $res = $pocketImport->setMarkAsRead(true)->import(); @@ -451,6 +454,87 @@ JSON; $this->assertEquals(['skipped' => 0, 'imported' => 1], $pocketImport->getSummary()); } + /** + * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. + */ + public function testImportWithRedis() + { + $client = new Client(); + + $body = <<<'JSON' +{ + "item_id": "229279689", + "resolved_id": "229279689", + "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", + "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", + "favorite": "1", + "status": "1", + "time_added": "1473020899", + "time_updated": "1473020899", + "time_read": "0", + "time_favorited": "0", + "sort_id": 0, + "resolved_title": "The Massive Ryder Cup Preview", + "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", + "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.", + "is_article": "1", + "has_video": "0", + "has_image": "0", + "word_count": "3197" +} +JSON; + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' + { + "status": 1, + "list": { + "229279690": '.$body.' + } + } + ')), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->never()) + ->method('findByUrlAndUserId'); + + $this->em + ->expects($this->never()) + ->method('getRepository'); + + $entry = new Entry($this->user); + + $this->contentProxy + ->expects($this->never()) + ->method('updateEntry'); + + $factory = new RedisMockFactory(); + $redisMock = $factory->getAdapter('Predis\Client', true); + + $queue = new RedisQueue($redisMock, 'pocket'); + $producer = new Producer($queue); + + $pocketImport->setClient($client); + $pocketImport->setProducer($producer); + $pocketImport->authorize('wunderbar_code'); + + $res = $pocketImport->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 0, 'imported' => 1], $pocketImport->getSummary()); + + $this->assertNotEmpty($redisMock->lpop('pocket')); + } + public function testImportBadResponse() { $client = new Client(); diff --git a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php index 69a66d6a..0981eedb 100644 --- a/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php @@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; use Wallabag\ImportBundle\Import\ReadabilityImport; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\ImportBundle\Redis\Producer; use Monolog\Logger; use Monolog\Handler\TestHandler; +use Simpleue\Queue\RedisQueue; +use M6Web\Component\RedisMock\RedisMockFactory; class ReadabilityImportTest extends \PHPUnit_Framework_TestCase { @@ -152,7 +155,7 @@ class ReadabilityImportTest extends \PHPUnit_Framework_TestCase ->expects($this->exactly(2)) ->method('publish'); - $readabilityImport->setRabbitmqProducer($producer); + $readabilityImport->setProducer($producer); $res = $readabilityImport->setMarkAsRead(true)->import(); @@ -160,6 +163,46 @@ class ReadabilityImportTest extends \PHPUnit_Framework_TestCase $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary()); } + public function testImportWithRedis() + { + $readabilityImport = $this->getReadabilityImport(); + $readabilityImport->setFilepath(__DIR__.'/../fixtures/readability.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->never()) + ->method('findByUrlAndUserId'); + + $this->em + ->expects($this->never()) + ->method('getRepository'); + + $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy + ->expects($this->never()) + ->method('updateEntry'); + + $factory = new RedisMockFactory(); + $redisMock = $factory->getAdapter('Predis\Client', true); + + $queue = new RedisQueue($redisMock, 'readability'); + $producer = new Producer($queue); + + $readabilityImport->setProducer($producer); + + $res = $readabilityImport->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary()); + + $this->assertNotEmpty($redisMock->lpop('readability')); + } + public function testImportBadFile() { $readabilityImport = $this->getReadabilityImport(); diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php index ada5493e..b43682cd 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php @@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; use Wallabag\ImportBundle\Import\WallabagV1Import; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\ImportBundle\Redis\Producer; use Monolog\Logger; use Monolog\Handler\TestHandler; +use Simpleue\Queue\RedisQueue; +use M6Web\Component\RedisMock\RedisMockFactory; class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase { @@ -152,7 +155,7 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase ->expects($this->exactly(4)) ->method('publish'); - $wallabagV1Import->setRabbitmqProducer($producer); + $wallabagV1Import->setProducer($producer); $res = $wallabagV1Import->setMarkAsRead(true)->import(); @@ -160,6 +163,46 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase $this->assertEquals(['skipped' => 0, 'imported' => 4], $wallabagV1Import->getSummary()); } + public function testImportWithRedis() + { + $wallabagV1Import = $this->getWallabagV1Import(); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->never()) + ->method('findByUrlAndUserId'); + + $this->em + ->expects($this->never()) + ->method('getRepository'); + + $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy + ->expects($this->never()) + ->method('updateEntry'); + + $factory = new RedisMockFactory(); + $redisMock = $factory->getAdapter('Predis\Client', true); + + $queue = new RedisQueue($redisMock, 'wallabag_v1'); + $producer = new Producer($queue); + + $wallabagV1Import->setProducer($producer); + + $res = $wallabagV1Import->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 0, 'imported' => 4], $wallabagV1Import->getSummary()); + + $this->assertNotEmpty($redisMock->lpop('wallabag_v1')); + } + public function testImportBadFile() { $wallabagV1Import = $this->getWallabagV1Import(); diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php index 51f0aada..18998b35 100644 --- a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php +++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php @@ -5,8 +5,11 @@ namespace Tests\Wallabag\ImportBundle\Import; use Wallabag\ImportBundle\Import\WallabagV2Import; use Wallabag\UserBundle\Entity\User; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\ImportBundle\Redis\Producer; use Monolog\Logger; use Monolog\Handler\TestHandler; +use Simpleue\Queue\RedisQueue; +use M6Web\Component\RedisMock\RedisMockFactory; class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase { @@ -144,7 +147,7 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase ->expects($this->exactly(24)) ->method('publish'); - $wallabagV2Import->setRabbitmqProducer($producer); + $wallabagV2Import->setProducer($producer); $res = $wallabagV2Import->setMarkAsRead(true)->import(); @@ -152,6 +155,42 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary()); } + public function testImportWithRedis() + { + $wallabagV2Import = $this->getWallabagV2Import(); + $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->never()) + ->method('findByUrlAndUserId'); + + $this->em + ->expects($this->never()) + ->method('getRepository'); + + $this->contentProxy + ->expects($this->never()) + ->method('updateEntry'); + + $factory = new RedisMockFactory(); + $redisMock = $factory->getAdapter('Predis\Client', true); + + $queue = new RedisQueue($redisMock, 'wallabag_v2'); + $producer = new Producer($queue); + + $wallabagV2Import->setProducer($producer); + + $res = $wallabagV2Import->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary()); + + $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); + } + public function testImportBadFile() { $wallabagV1Import = $this->getWallabagV2Import(); -- cgit v1.2.3