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;
15 protected $contentProxy;
18 private function getWallabagV1Import($unsetUser = false)
20 $this->user
= new User();
22 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
23 ->disableOriginalConstructor()
26 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
27 ->disableOriginalConstructor()
30 $wallabag = new WallabagV1Import($this->em
,$this->contentProxy
);
32 $this->logHandler
= new TestHandler();
33 $logger = new Logger('test', array($this->logHandler
));
34 $wallabag->setLogger($logger);
36 if (false === $unsetUser) {
37 $wallabag->setUser($this->user
);
43 public function testInit()
45 $wallabagV1Import = $this->getWallabagV1Import();
47 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
48 $this->assertNotEmpty($wallabagV1Import->getUrl());
49 $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
52 public function testImport()
54 $wallabagV1Import = $this->getWallabagV1Import();
55 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
57 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
58 ->disableOriginalConstructor()
61 $entryRepo->expects($this->exactly(3))
62 ->method('findByUrlAndUserId')
63 ->will($this->onConsecutiveCalls(false, true, false));
66 ->expects($this->any())
67 ->method('getRepository')
68 ->willReturn($entryRepo);
70 $res = $wallabagV1Import->import();
72 $this->assertTrue($res);
73 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary());
76 public function testImportBadFile()
78 $wallabagV1Import = $this->getWallabagV1Import();
79 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
81 $res = $wallabagV1Import->import();
83 $this->assertFalse($res);
85 $records = $this->logHandler
->getRecords();
86 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
87 $this->assertEquals('ERROR', $records[0]['level_name']);
90 public function testImportUserNotDefined()
92 $wallabagV1Import = $this->getWallabagV1Import(true);
93 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
95 $res = $wallabagV1Import->import();
97 $this->assertFalse($res);
99 $records = $this->logHandler
->getRecords();
100 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
101 $this->assertEquals('ERROR', $records[0]['level_name']);