3 namespace Wallabag\ImportBundle\Tests\Import
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\ImportBundle\Import\WallabagV1Import
;
8 use Monolog\Handler\TestHandler
;
10 class WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
14 protected $logHandler;
16 private function getWallabagV1Import($unsetUser = false)
18 $this->user
= new User();
20 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
21 ->disableOriginalConstructor()
24 $pocket = new WallabagV1Import($this->em
);
26 $this->logHandler
= new TestHandler();
27 $logger = new Logger('test', array($this->logHandler
));
28 $pocket->setLogger($logger);
30 if (false === $unsetUser) {
31 $pocket->setUser($this->user
);
37 public function testInit()
39 $wallabagV1Import = $this->getWallabagV1Import();
41 $this->assertEquals('Wallabag v1', $wallabagV1Import->getName());
42 $this->assertNotEmpty($wallabagV1Import->getUrl());
43 $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
46 public function testImport()
48 $wallabagV1Import = $this->getWallabagV1Import();
49 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
51 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
52 ->disableOriginalConstructor()
55 $entryRepo->expects($this->exactly(3))
56 ->method('existByUrlAndUserId')
57 ->will($this->onConsecutiveCalls(false, true, false));
60 ->expects($this->any())
61 ->method('getRepository')
62 ->willReturn($entryRepo);
64 $res = $wallabagV1Import->import();
66 $this->assertTrue($res);
67 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary());
70 public function testImportBadFile()
72 $wallabagV1Import = $this->getWallabagV1Import();
73 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
75 $res = $wallabagV1Import->import();
77 $this->assertFalse($res);
79 $records = $this->logHandler
->getRecords();
80 $this->assertContains('WallabagV1Import: unable to read file', $records[0]['message']);
81 $this->assertEquals('ERROR', $records[0]['level_name']);
84 public function testImportUserNotDefined()
86 $wallabagV1Import = $this->getWallabagV1Import(true);
87 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
89 $res = $wallabagV1Import->import();
91 $this->assertFalse($res);
93 $records = $this->logHandler
->getRecords();
94 $this->assertContains('WallabagV1Import: user is not defined', $records[0]['message']);
95 $this->assertEquals('ERROR', $records[0]['level_name']);