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;
17 private function getWallabagV1Import($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 WallabagV1Import($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 $wallabagV1Import = $this->getWallabagV1Import();
46 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
47 $this->assertNotEmpty($wallabagV1Import->getUrl());
48 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
51 public function testImport()
53 $wallabagV1Import = $this->getWallabagV1Import();
54 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
56 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
57 ->disableOriginalConstructor()
60 $entryRepo->expects($this->exactly(4))
61 ->method('findByUrlAndUserId')
62 ->will($this->onConsecutiveCalls(false, true, false, false));
65 ->expects($this->any())
66 ->method('getRepository')
67 ->willReturn($entryRepo);
69 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
70 ->disableOriginalConstructor()
74 ->expects($this->once())
75 ->method('updateEntry')
78 $res = $wallabagV1Import->import();
80 $this->assertTrue($res);
81 $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary());
84 public function testImportAndMarkAllAsRead()
86 $wallabagV1Import = $this->getWallabagV1Import();
87 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
89 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
90 ->disableOriginalConstructor()
93 $entryRepo->expects($this->exactly(3))
94 ->method('findByUrlAndUserId')
95 ->will($this->onConsecutiveCalls(false, false, false));
98 ->expects($this->any())
99 ->method('getRepository')
100 ->willReturn($entryRepo);
102 // check that every entry persisted are archived
104 ->expects($this->any())
106 ->with($this->callback(function ($persistedEntry) {
107 return $persistedEntry->isArchived();
110 $res = $wallabagV1Import->setMarkAsRead(true)->import();
112 $this->assertTrue($res);
114 $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary());
117 public function testImportBadFile()
119 $wallabagV1Import = $this->getWallabagV1Import();
120 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
122 $res = $wallabagV1Import->import();
124 $this->assertFalse($res);
126 $records = $this->logHandler
->getRecords();
127 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
128 $this->assertEquals('ERROR', $records[0]['level_name']);
131 public function testImportUserNotDefined()
133 $wallabagV1Import = $this->getWallabagV1Import(true);
134 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
136 $res = $wallabagV1Import->import();
138 $this->assertFalse($res);
140 $records = $this->logHandler
->getRecords();
141 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
142 $this->assertEquals('ERROR', $records[0]['level_name']);