3 namespace Wallabag\ImportBundle\Tests\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV2Import
;
6 use Wallabag\UserBundle\Entity\User
;
8 use Monolog\Handler\TestHandler
;
10 class WallabagV2ImportTest
extends \PHPUnit_Framework_TestCase
14 protected $logHandler;
15 protected $contentProxy;
17 private function getWallabagV2Import($unsetUser = false)
19 $this->user
= new User();
21 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
22 ->disableOriginalConstructor()
25 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
26 ->disableOriginalConstructor()
29 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
);
31 $this->logHandler
= new TestHandler();
32 $logger = new Logger('test', array($this->logHandler
));
33 $wallabag->setLogger($logger);
35 if (false === $unsetUser) {
36 $wallabag->setUser($this->user
);
42 public function testInit()
44 $wallabagV2Import = $this->getWallabagV2Import();
46 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
47 $this->assertNotEmpty($wallabagV2Import->getUrl());
48 $this->assertContains('This importer will import all your wallabag v2 articles.', $wallabagV2Import->getDescription());
51 public function testImport()
53 $wallabagV2Import = $this->getWallabagV2Import();
54 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
56 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
57 ->disableOriginalConstructor()
60 $entryRepo->expects($this->exactly(3))
61 ->method('findByUrlAndUserId')
62 ->will($this->onConsecutiveCalls(false, true, false));
65 ->expects($this->any())
66 ->method('getRepository')
67 ->willReturn($entryRepo);
69 $res = $wallabagV2Import->import();
71 $this->assertTrue($res);
72 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV2Import->getSummary());
75 public function testImportAndMarkAllAsRead()
77 $wallabagV2Import = $this->getWallabagV2Import();
78 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
80 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
81 ->disableOriginalConstructor()
84 $entryRepo->expects($this->exactly(2))
85 ->method('findByUrlAndUserId')
86 ->will($this->onConsecutiveCalls(false, false));
89 ->expects($this->any())
90 ->method('getRepository')
91 ->willReturn($entryRepo);
93 $res = $wallabagV2Import->setMarkAsRead(true)->import();
95 $this->assertTrue($res);
98 ->expects($this->any())
99 ->method('getBuilderForArchiveByUser')
100 ->willReturn($entryRepo);
102 $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary());
105 public function testImportBadFile()
107 $wallabagV1Import = $this->getWallabagV2Import();
108 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
110 $res = $wallabagV1Import->import();
112 $this->assertFalse($res);
114 $records = $this->logHandler
->getRecords();
115 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
116 $this->assertEquals('ERROR', $records[0]['level_name']);
119 public function testImportUserNotDefined()
121 $wallabagV1Import = $this->getWallabagV2Import(true);
122 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
124 $res = $wallabagV1Import->import();
126 $this->assertFalse($res);
128 $records = $this->logHandler
->getRecords();
129 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
130 $this->assertEquals('ERROR', $records[0]['level_name']);