From 23634d5d842dabcf5d7475e2becb7e127824239e Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 1 Jun 2016 21:27:35 +0200 Subject: Jump to Symfony 3.1 --- .../ImportBundle/Import/ImportChainTest.php | 21 ++ .../ImportBundle/Import/ImportCompilerPassTest.php | 47 +++ .../ImportBundle/Import/PocketImportTest.php | 393 +++++++++++++++++++++ .../ImportBundle/Import/WallabagV1ImportTest.php | 150 ++++++++ .../ImportBundle/Import/WallabagV2ImportTest.php | 146 ++++++++ 5 files changed, 757 insertions(+) create mode 100644 tests/Wallabag/ImportBundle/Import/ImportChainTest.php create mode 100644 tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php create mode 100644 tests/Wallabag/ImportBundle/Import/PocketImportTest.php create mode 100644 tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php create mode 100644 tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php (limited to 'tests/Wallabag/ImportBundle/Import') diff --git a/tests/Wallabag/ImportBundle/Import/ImportChainTest.php b/tests/Wallabag/ImportBundle/Import/ImportChainTest.php new file mode 100644 index 00000000..32568ce5 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ImportChainTest.php @@ -0,0 +1,21 @@ +getMockBuilder('Wallabag\ImportBundle\Import\ImportInterface') + ->disableOriginalConstructor() + ->getMock(); + + $importChain = new ImportChain(); + $importChain->addImport($import, 'alias'); + + $this->assertCount(1, $importChain->getAll()); + $this->assertEquals($import, $importChain->getAll()['alias']); + } +} diff --git a/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php b/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php new file mode 100644 index 00000000..71a007a9 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php @@ -0,0 +1,47 @@ +process($container); + + $this->assertNull($res); + } + + public function testProcess() + { + $container = new ContainerBuilder(); + $container + ->register('wallabag_import.chain') + ->setPublic(false) + ; + + $container + ->register('foo') + ->addTag('wallabag_import.import', ['alias' => 'pocket']) + ; + + $this->process($container); + + $this->assertTrue($container->hasDefinition('wallabag_import.chain')); + + $definition = $container->getDefinition('wallabag_import.chain'); + $this->assertTrue($definition->hasMethodCall('addImport')); + + $calls = $definition->getMethodCalls(); + $this->assertEquals('pocket', $calls[0][1][1]); + } + + protected function process(ContainerBuilder $container) + { + $repeatedPass = new ImportCompilerPass(); + $repeatedPass->process($container); + } +} diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php new file mode 100644 index 00000000..41f9b51f --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php @@ -0,0 +1,393 @@ +accessToken; + } +} + +class PocketImportTest extends \PHPUnit_Framework_TestCase +{ + protected $token; + protected $user; + protected $em; + protected $contentProxy; + protected $logHandler; + + private function getPocketImport($consumerKey = 'ConsumerKey') + { + $this->user = new User(); + + $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') + ->disableOriginalConstructor() + ->getMock(); + + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') + ->disableOriginalConstructor() + ->getMock(); + + $token->expects($this->once()) + ->method('getUser') + ->willReturn($this->user); + + $this->tokenStorage->expects($this->once()) + ->method('getToken') + ->willReturn($token); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') + ->disableOriginalConstructor() + ->getMock(); + + $config->expects($this->any()) + ->method('get') + ->with('pocket_consumer_key') + ->willReturn($consumerKey); + + $pocket = new PocketImportMock( + $this->tokenStorage, + $this->em, + $this->contentProxy, + $config + ); + + $this->logHandler = new TestHandler(); + $logger = new Logger('test', [$this->logHandler]); + $pocket->setLogger($logger); + + return $pocket; + } + + public function testInit() + { + $pocketImport = $this->getPocketImport(); + + $this->assertEquals('Pocket', $pocketImport->getName()); + $this->assertNotEmpty($pocketImport->getUrl()); + $this->assertEquals('import.pocket.description', $pocketImport->getDescription()); + } + + public function testOAuthRequest() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); + + $this->assertEquals('wunderbar_code', $code); + } + + public function testOAuthRequestBadResponse() + { + $client = new Client(); + + $mock = new Mock([ + new Response(403), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); + + $this->assertFalse($code); + + $records = $this->logHandler->getRecords(); + $this->assertContains('PocketImport: Failed to request token', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } + + public function testOAuthAuthorize() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $res = $pocketImport->authorize('wunderbar_code'); + + $this->assertTrue($res); + $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken()); + } + + public function testOAuthAuthorizeBadResponse() + { + $client = new Client(); + + $mock = new Mock([ + new Response(403), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $res = $pocketImport->authorize('wunderbar_code'); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } + + /** + * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. + */ + public function testImport() + { + $client = new Client(); + + $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": { + "229279689": { + "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": "1", + "has_image": "1", + "word_count": "3197", + "images": { + "1": { + "item_id": "229279689", + "image_id": "1", + "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", + "width": "0", + "height": "0", + "credit": "Jamie Squire/Getty Images", + "caption": "" + } + }, + "videos": { + "1": { + "item_id": "229279689", + "video_id": "1", + "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", + "width": "420", + "height": "315", + "type": "1", + "vid": "Er34PbFkVGk" + } + }, + "tags": { + "grantland": { + "item_id": "1147652870", + "tag": "grantland" + }, + "Ryder Cup": { + "item_id": "1147652870", + "tag": "Ryder Cup" + } + } + }, + "229279690": { + "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" + } + } + } + ')), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, true)); + + $this->em + ->expects($this->exactly(2)) + ->method('getRepository') + ->willReturn($entryRepo); + + $entry = new Entry($this->user); + + $this->contentProxy + ->expects($this->once()) + ->method('updateEntry') + ->willReturn($entry); + + $pocketImport->setClient($client); + $pocketImport->authorize('wunderbar_code'); + + $res = $pocketImport->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); + } + + /** + * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. + */ + public function testImportAndMarkAllAsRead() + { + $client = new Client(); + + $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": { + "229279689": { + "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", + "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": "1", + "has_image": "1", + "word_count": "3197" + }, + "229279690": { + "item_id": "229279689", + "resolved_id": "229279689", + "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", + "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", + "favorite": "1", + "status": "0", + "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" + } + } + } + ')), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, false)); + + $this->em + ->expects($this->exactly(2)) + ->method('getRepository') + ->willReturn($entryRepo); + + // check that every entry persisted are archived + $this->em + ->expects($this->any()) + ->method('persist') + ->with($this->callback(function ($persistedEntry) { + return $persistedEntry->isArchived(); + })); + + $entry = new Entry($this->user); + + $this->contentProxy + ->expects($this->exactly(2)) + ->method('updateEntry') + ->willReturn($entry); + + $pocketImport->setClient($client); + $pocketImport->authorize('wunderbar_code'); + + $res = $pocketImport->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); + } + + public function testImportBadResponse() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), + new Response(403), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + $pocketImport->authorize('wunderbar_code'); + + $res = $pocketImport->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('PocketImport: Failed to import', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } +} diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php new file mode 100644 index 00000000..bdc47dac --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php @@ -0,0 +1,150 @@ +user = new User(); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') + ->disableOriginalConstructor() + ->getMock(); + + $wallabag = new WallabagV1Import($this->em, $this->contentProxy); + + $this->logHandler = new TestHandler(); + $logger = new Logger('test', [$this->logHandler]); + $wallabag->setLogger($logger); + + if (false === $unsetUser) { + $wallabag->setUser($this->user); + } + + return $wallabag; + } + + public function testInit() + { + $wallabagV1Import = $this->getWallabagV1Import(); + + $this->assertEquals('wallabag v1', $wallabagV1Import->getName()); + $this->assertNotEmpty($wallabagV1Import->getUrl()); + $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription()); + } + + public function testImport() + { + $wallabagV1Import = $this->getWallabagV1Import(); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(4)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, true, false, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy + ->expects($this->exactly(3)) + ->method('updateEntry') + ->willReturn($entry); + + $res = $wallabagV1Import->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary()); + } + + public function testImportAndMarkAllAsRead() + { + $wallabagV1Import = $this->getWallabagV1Import(); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1-read.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(3)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, false, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $this->contentProxy + ->expects($this->exactly(3)) + ->method('updateEntry') + ->willReturn(new Entry($this->user)); + + // check that every entry persisted are archived + $this->em + ->expects($this->any()) + ->method('persist') + ->with($this->callback(function ($persistedEntry) { + return $persistedEntry->isArchived(); + })); + + $res = $wallabagV1Import->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + + $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary()); + } + + public function testImportBadFile() + { + $wallabagV1Import = $this->getWallabagV1Import(); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); + + $res = $wallabagV1Import->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } + + public function testImportUserNotDefined() + { + $wallabagV1Import = $this->getWallabagV1Import(true); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); + + $res = $wallabagV1Import->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } +} diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php new file mode 100644 index 00000000..8ec66b12 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php @@ -0,0 +1,146 @@ +user = new User(); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') + ->disableOriginalConstructor() + ->getMock(); + + $wallabag = new WallabagV2Import($this->em, $this->contentProxy); + + $this->logHandler = new TestHandler(); + $logger = new Logger('test', [$this->logHandler]); + $wallabag->setLogger($logger); + + if (false === $unsetUser) { + $wallabag->setUser($this->user); + } + + return $wallabag; + } + + public function testInit() + { + $wallabagV2Import = $this->getWallabagV2Import(); + + $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); + $this->assertNotEmpty($wallabagV2Import->getUrl()); + $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); + } + + public function testImport() + { + $wallabagV2Import = $this->getWallabagV2Import(); + $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(24)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, true, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $this->contentProxy + ->expects($this->exactly(2)) + ->method('updateEntry') + ->willReturn(new Entry($this->user)); + + $res = $wallabagV2Import->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); + } + + public function testImportAndMarkAllAsRead() + { + $wallabagV2Import = $this->getWallabagV2Import(); + $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); + + $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $entryRepo->expects($this->exactly(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $this->contentProxy + ->expects($this->exactly(2)) + ->method('updateEntry') + ->willReturn(new Entry($this->user)); + + // check that every entry persisted are archived + $this->em + ->expects($this->any()) + ->method('persist') + ->with($this->callback(function ($persistedEntry) { + return $persistedEntry->isArchived(); + })); + + $res = $wallabagV2Import->setMarkAsRead(true)->import(); + + $this->assertTrue($res); + + $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); + } + + public function testImportBadFile() + { + $wallabagV1Import = $this->getWallabagV2Import(); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); + + $res = $wallabagV1Import->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } + + public function testImportUserNotDefined() + { + $wallabagV1Import = $this->getWallabagV2Import(true); + $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); + + $res = $wallabagV1Import->import(); + + $this->assertFalse($res); + + $records = $this->logHandler->getRecords(); + $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); + $this->assertEquals('ERROR', $records[0]['level_name']); + } +} -- cgit v1.2.3