Add test for RabbitMQ

Also update Symfony deps
This commit is contained in:
Jeremy Benoist 2016-09-09 18:02:29 +02:00
parent 8664069e1a
commit 13470c3596
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
11 changed files with 487 additions and 2 deletions

View 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'

View file

@ -265,7 +265,7 @@ class PocketImport extends AbstractImport
*/
protected function setEntryAsRead(array $importedEntry)
{
$importedEntry['status'] = 1;
$importedEntry['status'] = '1';
return $importedEntry;
}

View file

@ -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);
}
}

View 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');

View 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');

View 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');

View 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');

View 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();

View 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();

View 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();

View 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();