3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV1Import
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Entity\Entry
;
9 use Monolog\Handler\TestHandler
;
11 class WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
15 protected $logHandler;
16 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', [$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->assertEquals('import.wallabag_v1.description', $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(4))
62 ->method('findByUrlAndUserId')
63 ->will($this->onConsecutiveCalls(false, true, false, false));
66 ->expects($this->any())
67 ->method('getRepository')
68 ->willReturn($entryRepo);
70 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
71 ->disableOriginalConstructor()
75 ->expects($this->exactly(3))
76 ->method('updateEntry')
79 $res = $wallabagV1Import->import();
81 $this->assertTrue($res);
82 $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary());
85 public function testImportAndMarkAllAsRead()
87 $wallabagV1Import = $this->getWallabagV1Import();
88 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
90 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
91 ->disableOriginalConstructor()
94 $entryRepo->expects($this->exactly(3))
95 ->method('findByUrlAndUserId')
96 ->will($this->onConsecutiveCalls(false, false, false));
99 ->expects($this->any())
100 ->method('getRepository')
101 ->willReturn($entryRepo);
104 ->expects($this->exactly(3))
105 ->method('updateEntry')
106 ->willReturn(new Entry($this->user
));
108 // check that every entry persisted are archived
110 ->expects($this->any())
112 ->with($this->callback(function ($persistedEntry) {
113 return $persistedEntry->isArchived();
116 $res = $wallabagV1Import->setMarkAsRead(true)->import();
118 $this->assertTrue($res);
120 $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary());
123 public function testImportWithRabbit()
125 $wallabagV1Import = $this->getWallabagV1Import();
126 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
128 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
129 ->disableOriginalConstructor()
132 $entryRepo->expects($this->never())
133 ->method('findByUrlAndUserId');
136 ->expects($this->never())
137 ->method('getRepository');
139 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
140 ->disableOriginalConstructor()
144 ->expects($this->never())
145 ->method('updateEntry');
147 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
148 ->disableOriginalConstructor()
152 ->expects($this->exactly(4))
155 $wallabagV1Import->setRabbitmqProducer($producer);
157 $res = $wallabagV1Import->setMarkAsRead(true)->import();
159 $this->assertTrue($res);
160 $this->assertEquals(['skipped' => 0, 'imported' => 4], $wallabagV1Import->getSummary());
163 public function testImportBadFile()
165 $wallabagV1Import = $this->getWallabagV1Import();
166 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
168 $res = $wallabagV1Import->import();
170 $this->assertFalse($res);
172 $records = $this->logHandler
->getRecords();
173 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
174 $this->assertEquals('ERROR', $records[0]['level_name']);
177 public function testImportUserNotDefined()
179 $wallabagV1Import = $this->getWallabagV1Import(true);
180 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
182 $res = $wallabagV1Import->import();
184 $this->assertFalse($res);
186 $records = $this->logHandler
->getRecords();
187 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
188 $this->assertEquals('ERROR', $records[0]['level_name']);