3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV2Import
;
6 use Wallabag\UserBundle\Entity\User
;
7 use Wallabag\CoreBundle\Entity\Entry
;
8 use Wallabag\ImportBundle\Redis\Producer
;
10 use Monolog\Handler\TestHandler
;
11 use Simpleue\Queue\RedisQueue
;
12 use M6Web\Component\RedisMock\RedisMockFactory
;
14 class WallabagV2ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
21 private function getWallabagV2Import($unsetUser = false)
23 $this->user
= new User();
25 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
26 ->disableOriginalConstructor()
29 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
30 ->disableOriginalConstructor()
34 ->expects($this->any())
35 ->method('getUnitOfWork')
36 ->willReturn($this->uow
);
39 ->expects($this->any())
40 ->method('getScheduledEntityInsertions')
43 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
44 ->disableOriginalConstructor()
47 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
);
49 $this->logHandler
= new TestHandler();
50 $logger = new Logger('test', [$this->logHandler
]);
51 $wallabag->setLogger($logger);
53 if (false === $unsetUser) {
54 $wallabag->setUser($this->user
);
60 public function testInit()
62 $wallabagV2Import = $this->getWallabagV2Import();
64 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
65 $this->assertNotEmpty($wallabagV2Import->getUrl());
66 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
69 public function testImport()
71 $wallabagV2Import = $this->getWallabagV2Import();
72 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
74 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
75 ->disableOriginalConstructor()
78 $entryRepo->expects($this->exactly(24))
79 ->method('findByUrlAndUserId')
80 ->will($this->onConsecutiveCalls(false, true, false));
83 ->expects($this->any())
84 ->method('getRepository')
85 ->willReturn($entryRepo);
88 ->expects($this->exactly(2))
89 ->method('updateEntry')
90 ->willReturn(new Entry($this->user
));
92 $res = $wallabagV2Import->import();
94 $this->assertTrue($res);
95 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
98 public function testImportAndMarkAllAsRead()
100 $wallabagV2Import = $this->getWallabagV2Import();
101 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
103 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
104 ->disableOriginalConstructor()
107 $entryRepo->expects($this->exactly(2))
108 ->method('findByUrlAndUserId')
109 ->will($this->onConsecutiveCalls(false, false));
112 ->expects($this->any())
113 ->method('getRepository')
114 ->willReturn($entryRepo);
117 ->expects($this->exactly(2))
118 ->method('updateEntry')
119 ->willReturn(new Entry($this->user
));
121 // check that every entry persisted are archived
123 ->expects($this->any())
125 ->with($this->callback(function ($persistedEntry) {
126 return $persistedEntry->isArchived();
129 $res = $wallabagV2Import->setMarkAsRead(true)->import();
131 $this->assertTrue($res);
133 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
136 public function testImportWithRabbit()
138 $wallabagV2Import = $this->getWallabagV2Import();
139 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
141 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
142 ->disableOriginalConstructor()
145 $entryRepo->expects($this->never())
146 ->method('findByUrlAndUserId');
149 ->expects($this->never())
150 ->method('getRepository');
153 ->expects($this->never())
154 ->method('updateEntry');
156 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
157 ->disableOriginalConstructor()
161 ->expects($this->exactly(24))
164 $wallabagV2Import->setProducer($producer);
166 $res = $wallabagV2Import->setMarkAsRead(true)->import();
168 $this->assertTrue($res);
169 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
172 public function testImportWithRedis()
174 $wallabagV2Import = $this->getWallabagV2Import();
175 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
177 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
178 ->disableOriginalConstructor()
181 $entryRepo->expects($this->never())
182 ->method('findByUrlAndUserId');
185 ->expects($this->never())
186 ->method('getRepository');
189 ->expects($this->never())
190 ->method('updateEntry');
192 $factory = new RedisMockFactory();
193 $redisMock = $factory->getAdapter('Predis\Client', true);
195 $queue = new RedisQueue($redisMock, 'wallabag_v2');
196 $producer = new Producer($queue);
198 $wallabagV2Import->setProducer($producer);
200 $res = $wallabagV2Import->setMarkAsRead(true)->import();
202 $this->assertTrue($res);
203 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
205 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
208 public function testImportBadFile()
210 $wallabagV1Import = $this->getWallabagV2Import();
211 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
213 $res = $wallabagV1Import->import();
215 $this->assertFalse($res);
217 $records = $this->logHandler
->getRecords();
218 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
219 $this->assertEquals('ERROR', $records[0]['level_name']);
222 public function testImportUserNotDefined()
224 $wallabagV1Import = $this->getWallabagV2Import(true);
225 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
227 $res = $wallabagV1Import->import();
229 $this->assertFalse($res);
231 $records = $this->logHandler
->getRecords();
232 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
233 $this->assertEquals('ERROR', $records[0]['level_name']);
236 public function testImportEmptyFile()
238 $wallabagV2Import = $this->getWallabagV2Import();
239 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-empty.json');
241 $res = $wallabagV2Import->import();
243 $this->assertFalse($res);
244 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
247 public function testImportWithExceptionFromGraby()
249 $wallabagV2Import = $this->getWallabagV2Import();
250 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
252 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
253 ->disableOriginalConstructor()
256 $entryRepo->expects($this->exactly(24))
257 ->method('findByUrlAndUserId')
258 ->will($this->onConsecutiveCalls(false, true, false));
261 ->expects($this->any())
262 ->method('getRepository')
263 ->willReturn($entryRepo);
266 ->expects($this->exactly(2))
267 ->method('updateEntry')
268 ->will($this->throwException(new \
Exception()));
270 $res = $wallabagV2Import->import();
272 $this->assertTrue($res);
273 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());