From 6785f4aa749e381081b93e3db46424cc7475eab8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 20 Jan 2016 14:37:01 +0100 Subject: [#1590] Add JSON import from wallabag v2 --- .../Tests/Import/WallabagV2ImportTest.php | 97 ++++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php (limited to 'src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php') diff --git a/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php new file mode 100644 index 00000000..4ebe93bf --- /dev/null +++ b/src/Wallabag/ImportBundle/Tests/Import/WallabagV2ImportTest.php @@ -0,0 +1,97 @@ +user = new User(); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $wallabag = new WallabagV2Import($this->em); + + $this->logHandler = new TestHandler(); + $logger = new Logger('test', array($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->assertContains('This importer will import all your wallabag v2 articles.', $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(2)) + ->method('findByUrlAndUserId') + ->will($this->onConsecutiveCalls(false, true, false)); + + $this->em + ->expects($this->any()) + ->method('getRepository') + ->willReturn($entryRepo); + + $res = $wallabagV2Import->import(); + + $this->assertTrue($res); + $this->assertEquals(['skipped' => 1, 'imported' => 1], $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