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, $dispatched = 0)
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 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
48 ->disableOriginalConstructor()
52 ->expects($this->exactly($dispatched))
55 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
, $dispatcher);
57 $this->logHandler
= new TestHandler();
58 $logger = new Logger('test', [$this->logHandler
]);
59 $wallabag->setLogger($logger);
61 if (false === $unsetUser) {
62 $wallabag->setUser($this->user
);
68 public function testInit()
70 $wallabagV2Import = $this->getWallabagV2Import();
72 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
73 $this->assertNotEmpty($wallabagV2Import->getUrl());
74 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
77 public function testImport()
79 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
80 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
82 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
83 ->disableOriginalConstructor()
86 $entryRepo->expects($this->exactly(24))
87 ->method('findByUrlAndUserId')
88 ->will($this->onConsecutiveCalls(false, true, false));
91 ->expects($this->any())
92 ->method('getRepository')
93 ->willReturn($entryRepo);
96 ->expects($this->exactly(2))
97 ->method('updateEntry')
98 ->willReturn(new Entry($this->user
));
100 $res = $wallabagV2Import->import();
102 $this->assertTrue($res);
103 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
106 public function testImportAndMarkAllAsRead()
108 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
109 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
111 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
112 ->disableOriginalConstructor()
115 $entryRepo->expects($this->exactly(2))
116 ->method('findByUrlAndUserId')
117 ->will($this->onConsecutiveCalls(false, false));
120 ->expects($this->any())
121 ->method('getRepository')
122 ->willReturn($entryRepo);
125 ->expects($this->exactly(2))
126 ->method('updateEntry')
127 ->willReturn(new Entry($this->user
));
129 // check that every entry persisted are archived
131 ->expects($this->any())
133 ->with($this->callback(function ($persistedEntry) {
134 return $persistedEntry->isArchived();
137 $res = $wallabagV2Import->setMarkAsRead(true)->import();
139 $this->assertTrue($res);
141 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
144 public function testImportWithRabbit()
146 $wallabagV2Import = $this->getWallabagV2Import();
147 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
149 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
150 ->disableOriginalConstructor()
153 $entryRepo->expects($this->never())
154 ->method('findByUrlAndUserId');
157 ->expects($this->never())
158 ->method('getRepository');
161 ->expects($this->never())
162 ->method('updateEntry');
164 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
165 ->disableOriginalConstructor()
169 ->expects($this->exactly(24))
172 $wallabagV2Import->setProducer($producer);
174 $res = $wallabagV2Import->setMarkAsRead(true)->import();
176 $this->assertTrue($res);
177 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
180 public function testImportWithRedis()
182 $wallabagV2Import = $this->getWallabagV2Import();
183 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
185 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
186 ->disableOriginalConstructor()
189 $entryRepo->expects($this->never())
190 ->method('findByUrlAndUserId');
193 ->expects($this->never())
194 ->method('getRepository');
197 ->expects($this->never())
198 ->method('updateEntry');
200 $factory = new RedisMockFactory();
201 $redisMock = $factory->getAdapter('Predis\Client', true);
203 $queue = new RedisQueue($redisMock, 'wallabag_v2');
204 $producer = new Producer($queue);
206 $wallabagV2Import->setProducer($producer);
208 $res = $wallabagV2Import->setMarkAsRead(true)->import();
210 $this->assertTrue($res);
211 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
213 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
216 public function testImportBadFile()
218 $wallabagV1Import = $this->getWallabagV2Import();
219 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
221 $res = $wallabagV1Import->import();
223 $this->assertFalse($res);
225 $records = $this->logHandler
->getRecords();
226 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
227 $this->assertEquals('ERROR', $records[0]['level_name']);
230 public function testImportUserNotDefined()
232 $wallabagV1Import = $this->getWallabagV2Import(true);
233 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
235 $res = $wallabagV1Import->import();
237 $this->assertFalse($res);
239 $records = $this->logHandler
->getRecords();
240 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
241 $this->assertEquals('ERROR', $records[0]['level_name']);
244 public function testImportEmptyFile()
246 $wallabagV2Import = $this->getWallabagV2Import();
247 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-empty.json');
249 $res = $wallabagV2Import->import();
251 $this->assertFalse($res);
252 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
255 public function testImportWithExceptionFromGraby()
257 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
258 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
260 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
261 ->disableOriginalConstructor()
264 $entryRepo->expects($this->exactly(24))
265 ->method('findByUrlAndUserId')
266 ->will($this->onConsecutiveCalls(false, true, false));
269 ->expects($this->any())
270 ->method('getRepository')
271 ->willReturn($entryRepo);
274 ->expects($this->exactly(2))
275 ->method('updateEntry')
276 ->will($this->throwException(new \
Exception()));
278 $res = $wallabagV2Import->import();
280 $this->assertTrue($res);
281 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());