3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use M6Web\Component\RedisMock\RedisMockFactory
;
6 use Monolog\Handler\TestHandler
;
8 use Simpleue\Queue\RedisQueue
;
9 use Wallabag\CoreBundle\Entity\Entry
;
10 use Wallabag\ImportBundle\Import\WallabagV2Import
;
11 use Wallabag\ImportBundle\Redis\Producer
;
12 use Wallabag\UserBundle\Entity\User
;
14 class WallabagV2ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
23 public function testInit()
25 $wallabagV2Import = $this->getWallabagV2Import();
27 $this->assertSame('wallabag v2', $wallabagV2Import->getName());
28 $this->assertNotEmpty($wallabagV2Import->getUrl());
29 $this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription());
32 public function testImport()
34 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
35 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
37 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
38 ->disableOriginalConstructor()
41 $entryRepo->expects($this->exactly(6))
42 ->method('findByUrlAndUserId')
43 ->will($this->onConsecutiveCalls(false, true, false));
46 ->expects($this->any())
47 ->method('getRepository')
48 ->willReturn($entryRepo);
51 ->expects($this->exactly(2))
52 ->method('updateEntry')
53 ->willReturn(new Entry($this->user
));
55 $res = $wallabagV2Import->import();
57 $this->assertTrue($res);
58 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
61 public function testImportAndMarkAllAsRead()
63 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
64 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2-read.json');
66 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
67 ->disableOriginalConstructor()
70 $entryRepo->expects($this->exactly(2))
71 ->method('findByUrlAndUserId')
72 ->will($this->onConsecutiveCalls(false, false));
75 ->expects($this->any())
76 ->method('getRepository')
77 ->willReturn($entryRepo);
80 ->expects($this->exactly(2))
81 ->method('updateEntry')
82 ->willReturn(new Entry($this->user
));
84 // check that every entry persisted are archived
86 ->expects($this->any())
88 ->with($this->callback(function ($persistedEntry) {
89 return $persistedEntry->isArchived();
92 $res = $wallabagV2Import->setMarkAsRead(true)->import();
94 $this->assertTrue($res);
96 $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
99 public function testImportWithRabbit()
101 $wallabagV2Import = $this->getWallabagV2Import();
102 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
104 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
105 ->disableOriginalConstructor()
108 $entryRepo->expects($this->never())
109 ->method('findByUrlAndUserId');
112 ->expects($this->never())
113 ->method('getRepository');
116 ->expects($this->never())
117 ->method('updateEntry');
119 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
120 ->disableOriginalConstructor()
124 ->expects($this->exactly(6))
127 $wallabagV2Import->setProducer($producer);
129 $res = $wallabagV2Import->setMarkAsRead(true)->import();
131 $this->assertTrue($res);
132 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
135 public function testImportWithRedis()
137 $wallabagV2Import = $this->getWallabagV2Import();
138 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
140 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
141 ->disableOriginalConstructor()
144 $entryRepo->expects($this->never())
145 ->method('findByUrlAndUserId');
148 ->expects($this->never())
149 ->method('getRepository');
152 ->expects($this->never())
153 ->method('updateEntry');
155 $factory = new RedisMockFactory();
156 $redisMock = $factory->getAdapter('Predis\Client', true);
158 $queue = new RedisQueue($redisMock, 'wallabag_v2');
159 $producer = new Producer($queue);
161 $wallabagV2Import->setProducer($producer);
163 $res = $wallabagV2Import->setMarkAsRead(true)->import();
165 $this->assertTrue($res);
166 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
168 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
171 public function testImportBadFile()
173 $wallabagV1Import = $this->getWallabagV2Import();
174 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.jsonx');
176 $res = $wallabagV1Import->import();
178 $this->assertFalse($res);
180 $records = $this->logHandler
->getRecords();
181 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
182 $this->assertSame('ERROR', $records[0]['level_name']);
185 public function testImportUserNotDefined()
187 $wallabagV1Import = $this->getWallabagV2Import(true);
188 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
190 $res = $wallabagV1Import->import();
192 $this->assertFalse($res);
194 $records = $this->logHandler
->getRecords();
195 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
196 $this->assertSame('ERROR', $records[0]['level_name']);
199 public function testImportEmptyFile()
201 $wallabagV2Import = $this->getWallabagV2Import();
202 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2-empty.json');
204 $res = $wallabagV2Import->import();
206 $this->assertFalse($res);
207 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
210 public function testImportWithExceptionFromGraby()
212 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
213 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
215 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
216 ->disableOriginalConstructor()
219 $entryRepo->expects($this->exactly(6))
220 ->method('findByUrlAndUserId')
221 ->will($this->onConsecutiveCalls(false, true, false));
224 ->expects($this->any())
225 ->method('getRepository')
226 ->willReturn($entryRepo);
229 ->expects($this->exactly(2))
230 ->method('updateEntry')
231 ->will($this->throwException(new \
Exception()));
233 $res = $wallabagV2Import->import();
235 $this->assertTrue($res);
236 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
239 private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
241 $this->user
= new User();
243 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
244 ->disableOriginalConstructor()
247 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
248 ->disableOriginalConstructor()
252 ->expects($this->any())
253 ->method('getUnitOfWork')
254 ->willReturn($this->uow
);
257 ->expects($this->any())
258 ->method('getScheduledEntityInsertions')
261 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
262 ->disableOriginalConstructor()
265 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
266 ->disableOriginalConstructor()
269 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
270 ->disableOriginalConstructor()
274 ->expects($this->exactly($dispatched))
275 ->method('dispatch');
277 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
279 $this->logHandler
= new TestHandler();
280 $logger = new Logger('test', [$this->logHandler
]);
281 $wallabag->setLogger($logger);
283 if (false === $unsetUser) {
284 $wallabag->setUser($this->user
);