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;
20 protected $tagsAssigner;
23 private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
25 $this->user
= new User();
27 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
28 ->disableOriginalConstructor()
31 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
32 ->disableOriginalConstructor()
36 ->expects($this->any())
37 ->method('getUnitOfWork')
38 ->willReturn($this->uow
);
41 ->expects($this->any())
42 ->method('getScheduledEntityInsertions')
45 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
46 ->disableOriginalConstructor()
49 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
50 ->disableOriginalConstructor()
53 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
54 ->disableOriginalConstructor()
58 ->expects($this->exactly($dispatched))
61 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
63 $this->logHandler
= new TestHandler();
64 $logger = new Logger('test', [$this->logHandler
]);
65 $wallabag->setLogger($logger);
67 if (false === $unsetUser) {
68 $wallabag->setUser($this->user
);
74 public function testInit()
76 $wallabagV2Import = $this->getWallabagV2Import();
78 $this->assertEquals('wallabag v2', $wallabagV2Import->getName());
79 $this->assertNotEmpty($wallabagV2Import->getUrl());
80 $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription());
83 public function testImport()
85 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
86 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
88 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
89 ->disableOriginalConstructor()
92 $entryRepo->expects($this->exactly(24))
93 ->method('findByUrlAndUserId')
94 ->will($this->onConsecutiveCalls(false, true, false));
97 ->expects($this->any())
98 ->method('getRepository')
99 ->willReturn($entryRepo);
102 ->expects($this->exactly(2))
103 ->method('updateEntry')
104 ->willReturn(new Entry($this->user
));
106 $res = $wallabagV2Import->import();
108 $this->assertTrue($res);
109 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
112 public function testImportAndMarkAllAsRead()
114 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
115 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-read.json');
117 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
118 ->disableOriginalConstructor()
121 $entryRepo->expects($this->exactly(2))
122 ->method('findByUrlAndUserId')
123 ->will($this->onConsecutiveCalls(false, false));
126 ->expects($this->any())
127 ->method('getRepository')
128 ->willReturn($entryRepo);
131 ->expects($this->exactly(2))
132 ->method('updateEntry')
133 ->willReturn(new Entry($this->user
));
135 // check that every entry persisted are archived
137 ->expects($this->any())
139 ->with($this->callback(function ($persistedEntry) {
140 return $persistedEntry->isArchived();
143 $res = $wallabagV2Import->setMarkAsRead(true)->import();
145 $this->assertTrue($res);
147 $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
150 public function testImportWithRabbit()
152 $wallabagV2Import = $this->getWallabagV2Import();
153 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
155 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
156 ->disableOriginalConstructor()
159 $entryRepo->expects($this->never())
160 ->method('findByUrlAndUserId');
163 ->expects($this->never())
164 ->method('getRepository');
167 ->expects($this->never())
168 ->method('updateEntry');
170 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
171 ->disableOriginalConstructor()
175 ->expects($this->exactly(24))
178 $wallabagV2Import->setProducer($producer);
180 $res = $wallabagV2Import->setMarkAsRead(true)->import();
182 $this->assertTrue($res);
183 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
186 public function testImportWithRedis()
188 $wallabagV2Import = $this->getWallabagV2Import();
189 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
191 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
192 ->disableOriginalConstructor()
195 $entryRepo->expects($this->never())
196 ->method('findByUrlAndUserId');
199 ->expects($this->never())
200 ->method('getRepository');
203 ->expects($this->never())
204 ->method('updateEntry');
206 $factory = new RedisMockFactory();
207 $redisMock = $factory->getAdapter('Predis\Client', true);
209 $queue = new RedisQueue($redisMock, 'wallabag_v2');
210 $producer = new Producer($queue);
212 $wallabagV2Import->setProducer($producer);
214 $res = $wallabagV2Import->setMarkAsRead(true)->import();
216 $this->assertTrue($res);
217 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 24], $wallabagV2Import->getSummary());
219 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
222 public function testImportBadFile()
224 $wallabagV1Import = $this->getWallabagV2Import();
225 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.jsonx');
227 $res = $wallabagV1Import->import();
229 $this->assertFalse($res);
231 $records = $this->logHandler
->getRecords();
232 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
233 $this->assertEquals('ERROR', $records[0]['level_name']);
236 public function testImportUserNotDefined()
238 $wallabagV1Import = $this->getWallabagV2Import(true);
239 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
241 $res = $wallabagV1Import->import();
243 $this->assertFalse($res);
245 $records = $this->logHandler
->getRecords();
246 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
247 $this->assertEquals('ERROR', $records[0]['level_name']);
250 public function testImportEmptyFile()
252 $wallabagV2Import = $this->getWallabagV2Import();
253 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2-empty.json');
255 $res = $wallabagV2Import->import();
257 $this->assertFalse($res);
258 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
261 public function testImportWithExceptionFromGraby()
263 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
264 $wallabagV2Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v2.json');
266 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
267 ->disableOriginalConstructor()
270 $entryRepo->expects($this->exactly(24))
271 ->method('findByUrlAndUserId')
272 ->will($this->onConsecutiveCalls(false, true, false));
275 ->expects($this->any())
276 ->method('getRepository')
277 ->willReturn($entryRepo);
280 ->expects($this->exactly(2))
281 ->method('updateEntry')
282 ->will($this->throwException(new \
Exception()));
284 $res = $wallabagV2Import->import();
286 $this->assertTrue($res);
287 $this->assertEquals(['skipped' => 22, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());