]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add test for RabbitMQ
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 9 Sep 2016 16:02:29 +0000 (18:02 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 11 Sep 2016 19:58:56 +0000 (21:58 +0200)
Also update Symfony deps

app/config/parameters_test.yml
src/Wallabag/ImportBundle/Import/PocketImport.php
tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php [new file with mode: 0644]
tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php
tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php
tests/Wallabag/ImportBundle/Import/PocketImportTest.php
tests/Wallabag/ImportBundle/Import/ReadabilityImportTest.php
tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php
tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php

index 696c95ef7b1487e8191b8b54ad96e4f278667edd..2943b27a75338643eac98b59b1e48d21146475ee 100644 (file)
@@ -5,4 +5,4 @@ parameters:
     test_database_name: null
     test_database_user: null
     test_database_password: null
-    test_database_path: '%kernel.root_dir%/../data/db/wallabag_testYO.sqlite'
+    test_database_path: '%kernel.root_dir%/../data/db/wallabag_test.sqlite'
index 92dcdd409b25d4099914153f4a042d15ed0f7dae..d76a3a08fa20b7bbe91d8db4e9f41081af64d240 100644 (file)
@@ -265,7 +265,7 @@ class PocketImport extends AbstractImport
      */
     protected function setEntryAsRead(array $importedEntry)
     {
-        $importedEntry['status'] = 1;
+        $importedEntry['status'] = '1';
 
         return $importedEntry;
     }
diff --git a/tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php b/tests/Wallabag/ImportBundle/Consumer/AMPQ/EntryConsumerTest.php
new file mode 100644 (file)
index 0000000..7141874
--- /dev/null
@@ -0,0 +1,225 @@
+<?php
+
+namespace Tests\Wallabag\ImportBundle\Consumer\AMQP;
+
+use Wallabag\ImportBundle\Consumer\AMPQ\EntryConsumer;
+use PhpAmqpLib\Message\AMQPMessage;
+use Wallabag\UserBundle\Entity\User;
+use Wallabag\CoreBundle\Entity\Entry;
+
+class EntryConsumerTest extends \PHPUnit_Framework_TestCase
+{
+    public function testMessageOk()
+    {
+        $em = $this->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);
+    }
+}
index e0e61df88c43d5d5161128a261738bf9385c0e69..098cf3566bb0f7496cea93af4967e634e9496ff7 100644 (file)
@@ -17,6 +17,21 @@ class PocketControllerTest extends WallabagCoreTestCase
         $this->assertEquals(1, $crawler->filter('button[type=submit]')->count());
     }
 
+    public function testImportPocketWithRabbitEnabled()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->getContainer()->get('craue_config')->set('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);
+    }
+
     public function testImportPocketAuthBadToken()
     {
         $this->logInAs('admin');
index fb39356a140c1b128566c35e0e011629e4a7f7d9..e12a723d3510a11889fa2fe8f7f5efcd1e199b8b 100644 (file)
@@ -19,6 +19,22 @@ class ReadabilityControllerTest extends WallabagCoreTestCase
         $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
     }
 
+    public function testImportReadabilityWithRabbitEnabled()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->getContainer()->get('craue_config')->set('rabbitmq', 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('rabbitmq', 0);
+    }
+
     public function testImportReadabilityWithFile()
     {
         $this->logInAs('admin');
index ff1bf6f050580809414af7e42db25abac861c4d5..96556717933447e343210ae770cc84b7a84ec9d2 100644 (file)
@@ -19,6 +19,22 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase
         $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
     }
 
+    public function testImportWallabagWithRabbitEnabled()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->getContainer()->get('craue_config')->set('rabbitmq', 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('rabbitmq', 0);
+    }
+
     public function testImportWallabagWithFile()
     {
         $this->logInAs('admin');
index 149e88bbbf3c3727447a8139f5ede31115de542a..250d0d3eddfaf391d549ad21c9ed3f116211f8bc 100644 (file)
@@ -19,6 +19,22 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase
         $this->assertEquals(1, $crawler->filter('input[type=file]')->count());
     }
 
+    public function testImportWallabagWithRabbitEnabled()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->getContainer()->get('craue_config')->set('rabbitmq', 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('rabbitmq', 0);
+    }
+
     public function testImportWallabagWithFile()
     {
         $this->logInAs('admin');
index 26c6b8255068700a31efac6bdef83a9f052feda7..5ad3e4351b5b2a1c2f95f71daebdbb80dea21816 100644 (file)
@@ -343,6 +343,87 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary());
     }
 
+    /**
+     * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
+     */
+    public function testImportWithRabbit()
+    {
+        $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",
+    "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');
+
+        $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $bodyAsArray = json_decode($body, true);
+        // because with just use `new User()` so it doesn't have an id
+        $bodyAsArray['userId'] = null;
+
+        $producer
+            ->expects($this->once())
+            ->method('publish')
+            ->with(json_encode($bodyAsArray));
+
+        $pocketImport->setClient($client);
+        $pocketImport->setRabbitmqProducer($producer);
+        $pocketImport->authorize('wunderbar_code');
+
+        $res = $pocketImport->setMarkAsRead(true)->import();
+
+        $this->assertTrue($res);
+        $this->assertEquals(['skipped' => 0, 'imported' => 1], $pocketImport->getSummary());
+    }
+
     public function testImportBadResponse()
     {
         $client = new Client();
index 706d707b2819db7113eac6b99cbeebf6186e213b..69a66d6a9dffaf24a9f5b04aa62052f731f3baf4 100644 (file)
@@ -120,6 +120,46 @@ class ReadabilityImportTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary());
     }
 
+    public function testImportWithRabbit()
+    {
+        $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');
+
+        $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $producer
+            ->expects($this->exactly(2))
+            ->method('publish');
+
+        $readabilityImport->setRabbitmqProducer($producer);
+
+        $res = $readabilityImport->setMarkAsRead(true)->import();
+
+        $this->assertTrue($res);
+        $this->assertEquals(['skipped' => 0, 'imported' => 2], $readabilityImport->getSummary());
+    }
+
     public function testImportBadFile()
     {
         $readabilityImport = $this->getReadabilityImport();
index bdc47dac14227b631e8e685b6d9f460000537336..ada5493e8a40dfb5953f6b0fa181d05fad1d3b98 100644 (file)
@@ -120,6 +120,46 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary());
     }
 
+    public function testImportWithRabbit()
+    {
+        $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');
+
+        $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $producer
+            ->expects($this->exactly(4))
+            ->method('publish');
+
+        $wallabagV1Import->setRabbitmqProducer($producer);
+
+        $res = $wallabagV1Import->setMarkAsRead(true)->import();
+
+        $this->assertTrue($res);
+        $this->assertEquals(['skipped' => 0, 'imported' => 4], $wallabagV1Import->getSummary());
+    }
+
     public function testImportBadFile()
     {
         $wallabagV1Import = $this->getWallabagV1Import();
index 4a45e0f0a6779ab750f710ce157e85b235aa8c9e..51f0aada5994f83c858a4bcaa9d3240d16e3cbd0 100644 (file)
@@ -116,6 +116,42 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary());
     }
 
+    public function testImportWithRabbit()
+    {
+        $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');
+
+        $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
+            ->disableOriginalConstructor()
+            ->getMock();
+
+        $producer
+            ->expects($this->exactly(24))
+            ->method('publish');
+
+        $wallabagV2Import->setRabbitmqProducer($producer);
+
+        $res = $wallabagV2Import->setMarkAsRead(true)->import();
+
+        $this->assertTrue($res);
+        $this->assertEquals(['skipped' => 0, 'imported' => 24], $wallabagV2Import->getSummary());
+    }
+
     public function testImportBadFile()
     {
         $wallabagV1Import = $this->getWallabagV2Import();