3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use M6Web\Component\RedisMock\RedisMockFactory
;
6 use Monolog\Handler\TestHandler
;
8 use PHPUnit\Framework\TestCase
;
9 use Simpleue\Queue\RedisQueue
;
10 use Wallabag\CoreBundle\Entity\Entry
;
11 use Wallabag\ImportBundle\Import\WallabagV2Import
;
12 use Wallabag\ImportBundle\Redis\Producer
;
13 use Wallabag\UserBundle\Entity\User
;
15 class WallabagV2ImportTest
extends TestCase
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
24 public function testInit()
26 $wallabagV2Import = $this->getWallabagV2Import();
28 $this->assertSame('wallabag v2', $wallabagV2Import->getName());
29 $this->assertNotEmpty($wallabagV2Import->getUrl());
30 $this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription());
33 public function testImport()
35 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
36 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
38 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
39 ->disableOriginalConstructor()
42 $entryRepo->expects($this->exactly(6))
43 ->method('findByUrlAndUserId')
44 ->will($this->onConsecutiveCalls(false, true, false));
47 ->expects($this->any())
48 ->method('getRepository')
49 ->willReturn($entryRepo);
52 ->expects($this->exactly(2))
53 ->method('updateEntry')
54 ->willReturn(new Entry($this->user
));
56 $res = $wallabagV2Import->import();
58 $this->assertTrue($res);
59 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
62 public function testImportAndMarkAllAsRead()
64 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
65 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2-read.json');
67 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
68 ->disableOriginalConstructor()
71 $entryRepo->expects($this->exactly(2))
72 ->method('findByUrlAndUserId')
73 ->will($this->onConsecutiveCalls(false, false));
76 ->expects($this->any())
77 ->method('getRepository')
78 ->willReturn($entryRepo);
81 ->expects($this->exactly(2))
82 ->method('updateEntry')
83 ->willReturn(new Entry($this->user
));
85 // check that every entry persisted are archived
87 ->expects($this->any())
89 ->with($this->callback(function ($persistedEntry) {
90 return $persistedEntry->isArchived();
93 $res = $wallabagV2Import->setMarkAsRead(true)->import();
95 $this->assertTrue($res);
97 $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
100 public function testImportWithRabbit()
102 $wallabagV2Import = $this->getWallabagV2Import();
103 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
105 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
106 ->disableOriginalConstructor()
109 $entryRepo->expects($this->never())
110 ->method('findByUrlAndUserId');
113 ->expects($this->never())
114 ->method('getRepository');
117 ->expects($this->never())
118 ->method('updateEntry');
120 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
121 ->disableOriginalConstructor()
125 ->expects($this->exactly(6))
128 $wallabagV2Import->setProducer($producer);
130 $res = $wallabagV2Import->setMarkAsRead(true)->import();
132 $this->assertTrue($res);
133 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
136 public function testImportWithRedis()
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 $factory = new RedisMockFactory();
157 $redisMock = $factory->getAdapter('Predis\Client', true);
159 $queue = new RedisQueue($redisMock, 'wallabag_v2');
160 $producer = new Producer($queue);
162 $wallabagV2Import->setProducer($producer);
164 $res = $wallabagV2Import->setMarkAsRead(true)->import();
166 $this->assertTrue($res);
167 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary());
169 $this->assertNotEmpty($redisMock->lpop('wallabag_v2'));
172 public function testImportBadFile()
174 $wallabagV1Import = $this->getWallabagV2Import();
175 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.jsonx');
177 $res = $wallabagV1Import->import();
179 $this->assertFalse($res);
181 $records = $this->logHandler
->getRecords();
182 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
183 $this->assertSame('ERROR', $records[0]['level_name']);
186 public function testImportUserNotDefined()
188 $wallabagV1Import = $this->getWallabagV2Import(true);
189 $wallabagV1Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
191 $res = $wallabagV1Import->import();
193 $this->assertFalse($res);
195 $records = $this->logHandler
->getRecords();
196 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
197 $this->assertSame('ERROR', $records[0]['level_name']);
200 public function testImportEmptyFile()
202 $wallabagV2Import = $this->getWallabagV2Import();
203 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2-empty.json');
205 $res = $wallabagV2Import->import();
207 $this->assertFalse($res);
208 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary());
211 public function testImportWithExceptionFromGraby()
213 $wallabagV2Import = $this->getWallabagV2Import(false, 2);
214 $wallabagV2Import->setFilepath(__DIR__
. '/../fixtures/wallabag-v2.json');
216 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
217 ->disableOriginalConstructor()
220 $entryRepo->expects($this->exactly(6))
221 ->method('findByUrlAndUserId')
222 ->will($this->onConsecutiveCalls(false, true, false));
225 ->expects($this->any())
226 ->method('getRepository')
227 ->willReturn($entryRepo);
230 ->expects($this->exactly(2))
231 ->method('updateEntry')
232 ->will($this->throwException(new \
Exception()));
234 $res = $wallabagV2Import->import();
236 $this->assertTrue($res);
237 $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary());
240 private function getWallabagV2Import($unsetUser = false, $dispatched = 0)
242 $this->user
= new User();
244 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
245 ->disableOriginalConstructor()
248 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
249 ->disableOriginalConstructor()
253 ->expects($this->any())
254 ->method('getUnitOfWork')
255 ->willReturn($this->uow
);
258 ->expects($this->any())
259 ->method('getScheduledEntityInsertions')
262 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
263 ->disableOriginalConstructor()
266 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
267 ->disableOriginalConstructor()
270 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
271 ->disableOriginalConstructor()
275 ->expects($this->exactly($dispatched))
276 ->method('dispatch');
278 $wallabag = new WallabagV2Import($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
280 $this->logHandler
= new TestHandler();
281 $logger = new Logger('test', [$this->logHandler
]);
282 $wallabag->setLogger($logger);
284 if (false === $unsetUser) {
285 $wallabag->setUser($this->user
);