3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\ImportBundle\Import\WallabagV1Import
;
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 WallabagV1ImportTest
extends \PHPUnit_Framework_TestCase
18 protected $logHandler;
19 protected $contentProxy;
21 private function getWallabagV1Import($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 WallabagV1Import($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 $wallabagV1Import = $this->getWallabagV1Import();
72 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
73 $this->assertNotEmpty($wallabagV1Import->getUrl());
74 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
77 public function testImport()
79 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
80 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
82 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
83 ->disableOriginalConstructor()
86 $entryRepo->expects($this->exactly(4))
87 ->method('findByUrlAndUserId')
88 ->will($this->onConsecutiveCalls(false, true, false, false));
91 ->expects($this->any())
92 ->method('getRepository')
93 ->willReturn($entryRepo);
95 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
96 ->disableOriginalConstructor()
100 ->expects($this->exactly(3))
101 ->method('updateEntry')
102 ->willReturn($entry);
104 $res = $wallabagV1Import->import();
106 $this->assertTrue($res);
107 $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
110 public function testImportAndMarkAllAsRead()
112 $wallabagV1Import = $this->getWallabagV1Import(false, 3);
113 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
115 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
116 ->disableOriginalConstructor()
119 $entryRepo->expects($this->exactly(3))
120 ->method('findByUrlAndUserId')
121 ->will($this->onConsecutiveCalls(false, false, false));
124 ->expects($this->any())
125 ->method('getRepository')
126 ->willReturn($entryRepo);
129 ->expects($this->exactly(3))
130 ->method('updateEntry')
131 ->willReturn(new Entry($this->user
));
133 // check that every entry persisted are archived
135 ->expects($this->any())
137 ->with($this->callback(function ($persistedEntry) {
138 return $persistedEntry->isArchived();
141 $res = $wallabagV1Import->setMarkAsRead(true)->import();
143 $this->assertTrue($res);
145 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
148 public function testImportWithRabbit()
150 $wallabagV1Import = $this->getWallabagV1Import();
151 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
153 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
154 ->disableOriginalConstructor()
157 $entryRepo->expects($this->never())
158 ->method('findByUrlAndUserId');
161 ->expects($this->never())
162 ->method('getRepository');
164 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
165 ->disableOriginalConstructor()
169 ->expects($this->never())
170 ->method('updateEntry');
172 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
173 ->disableOriginalConstructor()
177 ->expects($this->exactly(4))
180 $wallabagV1Import->setProducer($producer);
182 $res = $wallabagV1Import->setMarkAsRead(true)->import();
184 $this->assertTrue($res);
185 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
188 public function testImportWithRedis()
190 $wallabagV1Import = $this->getWallabagV1Import();
191 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
193 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
194 ->disableOriginalConstructor()
197 $entryRepo->expects($this->never())
198 ->method('findByUrlAndUserId');
201 ->expects($this->never())
202 ->method('getRepository');
204 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
205 ->disableOriginalConstructor()
209 ->expects($this->never())
210 ->method('updateEntry');
212 $factory = new RedisMockFactory();
213 $redisMock = $factory->getAdapter('Predis\Client', true);
215 $queue = new RedisQueue($redisMock, 'wallabag_v1');
216 $producer = new Producer($queue);
218 $wallabagV1Import->setProducer($producer);
220 $res = $wallabagV1Import->setMarkAsRead(true)->import();
222 $this->assertTrue($res);
223 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
225 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
228 public function testImportBadFile()
230 $wallabagV1Import = $this->getWallabagV1Import();
231 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
233 $res = $wallabagV1Import->import();
235 $this->assertFalse($res);
237 $records = $this->logHandler
->getRecords();
238 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
239 $this->assertEquals('ERROR', $records[0]['level_name']);
242 public function testImportUserNotDefined()
244 $wallabagV1Import = $this->getWallabagV1Import(true);
245 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
247 $res = $wallabagV1Import->import();
249 $this->assertFalse($res);
251 $records = $this->logHandler
->getRecords();
252 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
253 $this->assertEquals('ERROR', $records[0]['level_name']);