From 79d0e38e7ff975b2e0306d3dd96f57509fd84aef Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 4 Mar 2016 10:04:51 +0100 Subject: [PATCH] Adding test Reformat json file (thanks pro.jsonlint.com) --- .../Controller/PocketController.php | 12 +-- .../Controller/WallabagV1Controller.php | 1 + .../Form/Type/UploadImportType.php | 6 +- .../ImportBundle/Import/PocketImport.php | 2 +- .../Controller/WallabagV1ControllerTest.php | 3 +- .../Tests/Import/PocketImportTest.php | 94 ++++++++++++++++++- .../Tests/Import/WallabagV1ImportTest.php | 33 +++++++ .../Tests/Import/WallabagV2ImportTest.php | 13 ++- .../Tests/fixtures/wallabag-v1-read.json | 65 +++++++++---- .../Tests/fixtures/wallabag-v2-read.json | 53 ++++++----- 10 files changed, 219 insertions(+), 63 deletions(-) diff --git a/src/Wallabag/ImportBundle/Controller/PocketController.php b/src/Wallabag/ImportBundle/Controller/PocketController.php index ef0407e6..c88e115e 100644 --- a/src/Wallabag/ImportBundle/Controller/PocketController.php +++ b/src/Wallabag/ImportBundle/Controller/PocketController.php @@ -18,9 +18,9 @@ class PocketController extends Controller $pocket = $this->get('wallabag_import.pocket.import'); $form = $this->createFormBuilder($pocket) ->add('read', CheckboxType::class, array( - 'label' => 'Mark all as read', - 'required' => false, - )) + 'label' => 'Mark all as read', + 'required' => false, + )) ->getForm(); return $this->render('WallabagImportBundle:Pocket:index.html.twig', [ @@ -39,8 +39,7 @@ class PocketController extends Controller ->getRequestToken($this->generateUrl('import', array(), UrlGeneratorInterface::ABSOLUTE_URL)); $this->get('session')->set('import.pocket.code', $requestToken); - $markAsRead = $request->request->get('form')['read']; - $this->get('session')->set('read', $markAsRead); + $this->get('session')->set('read', $request->request->get('form')['read']); return $this->redirect( 'https://getpocket.com/auth/authorize?request_token='.$requestToken.'&redirect_uri='.$this->generateUrl('import_pocket_callback', array(), UrlGeneratorInterface::ABSOLUTE_URL), @@ -56,6 +55,7 @@ class PocketController extends Controller $message = 'Import failed, please try again.'; $pocket = $this->get('wallabag_import.pocket.import'); $markAsRead = $this->get('session')->get('read'); + $this->get('session')->remove('read'); // something bad happend on pocket side if (false === $pocket->authorize($this->get('session')->get('import.pocket.code'))) { @@ -72,8 +72,6 @@ class PocketController extends Controller $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; } - $this->get('session')->remove('read'); - $this->get('session')->getFlashBag()->add( 'notice', $message diff --git a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php index 8b27144b..154a0769 100644 --- a/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php +++ b/src/Wallabag/ImportBundle/Controller/WallabagV1Controller.php @@ -32,6 +32,7 @@ class WallabagV1Controller extends Controller ->import(); $message = 'Import failed, please try again.'; + if (true === $res) { $summary = $wallabag->getSummary(); $message = 'Import summary: '.$summary['imported'].' imported, '.$summary['skipped'].' already saved.'; diff --git a/src/Wallabag/ImportBundle/Form/Type/UploadImportType.php b/src/Wallabag/ImportBundle/Form/Type/UploadImportType.php index bbc3661a..38670379 100644 --- a/src/Wallabag/ImportBundle/Form/Type/UploadImportType.php +++ b/src/Wallabag/ImportBundle/Form/Type/UploadImportType.php @@ -15,9 +15,9 @@ class UploadImportType extends AbstractType $builder ->add('file', FileType::class) ->add('mark_as_read', CheckboxType::class, array( - 'label' => 'Mark all as read', - 'required' => false, - )) + 'label' => 'Mark all as read', + 'required' => false, + )) ->add('save', SubmitType::class) ; } diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 1ec82a41..238ddbd1 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -222,7 +222,7 @@ class PocketImport implements ImportInterface $entry = $this->contentProxy->updateEntry($entry, $url); // 0, 1, 2 - 1 if the item is archived - 2 if the item should be deleted - if ($pocketEntry['status'] == 1 | $this->markAsRead) { + if ($pocketEntry['status'] == 1 || $this->markAsRead) { $entry->setArchived(true); } diff --git a/src/Wallabag/ImportBundle/Tests/Controller/WallabagV1ControllerTest.php b/src/Wallabag/ImportBundle/Tests/Controller/WallabagV1ControllerTest.php index c10d0d18..7f97b0f5 100644 --- a/src/Wallabag/ImportBundle/Tests/Controller/WallabagV1ControllerTest.php +++ b/src/Wallabag/ImportBundle/Tests/Controller/WallabagV1ControllerTest.php @@ -99,8 +99,7 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase $this->assertTrue($content2->isArchived()); - $this->assertGreaterThan(1, $alert = $crawler->filter('div.messages.success')->extract(array('_text'))); - $this->assertContains('Import summary', $alert[0]); + $this->assertContains('Import summary', $client->getResponse()->getContent()); } public function testImportWallabagWithEmptyFile() diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php index f44786b1..bc9e2f42 100644 --- a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php +++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php @@ -3,6 +3,7 @@ namespace Wallabag\ImportBundle\Tests\Import; use Wallabag\UserBundle\Entity\User; +use Wallabag\CoreBundle\Entity\Entry; use Wallabag\ImportBundle\Import\PocketImport; use GuzzleHttp\Client; use GuzzleHttp\Subscriber\Mock; @@ -265,9 +266,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase ->method('getRepository') ->willReturn($entryRepo); - $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') - ->disableOriginalConstructor() - ->getMock(); + $entry = new Entry($this->user); $this->contentProxy ->expects($this->once()) @@ -283,6 +282,95 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase $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", + "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" + }, + "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", + "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, 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(); diff --git a/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php index 9a563a11..fbcd270d 100644 --- a/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php +++ b/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php @@ -81,6 +81,39 @@ class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase $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); + + // 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(); diff --git a/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php index 8728364b..c461168c 100644 --- a/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php +++ b/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php @@ -90,15 +90,18 @@ class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase ->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(); + })); + $res = $wallabagV2Import->setMarkAsRead(true)->import(); $this->assertTrue($res); - $this->em - ->expects($this->any()) - ->method('getBuilderForArchiveByUser') - ->willReturn($entryRepo); - $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); } diff --git a/src/Wallabag/ImportBundle/Tests/fixtures/wallabag-v1-read.json b/src/Wallabag/ImportBundle/Tests/fixtures/wallabag-v1-read.json index 73c6bebb..c4d1cf58 100644 --- a/src/Wallabag/ImportBundle/Tests/fixtures/wallabag-v1-read.json +++ b/src/Wallabag/ImportBundle/Tests/fixtures/wallabag-v1-read.json @@ -1,22 +1,53 @@ [ { - "id":"3","0":"3", - "title":"Features - wallabag","1":"Features - wallabag", - "url":"https:\/\/www.wallabag.org\/features\/","2":"https:\/\/www.wallabag.org\/features\/", - "is_read":"1","3":"1", - "is_fav":"1","4":"1", - "content":"\n\t\t

Here are some features. If one is missing, you can open a new issue<\/a>.<\/p>\n