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)
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 $wallabag = new WallabagV1Import($this->em
, $this->contentProxy
);
49 $this->logHandler
= new TestHandler();
50 $logger = new Logger('test', [$this->logHandler
]);
51 $wallabag->setLogger($logger);
53 if (false === $unsetUser) {
54 $wallabag->setUser($this->user
);
60 public function testInit()
62 $wallabagV1Import = $this->getWallabagV1Import();
64 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
65 $this->assertNotEmpty($wallabagV1Import->getUrl());
66 $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription());
69 public function testImport()
71 $wallabagV1Import = $this->getWallabagV1Import();
72 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
74 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
75 ->disableOriginalConstructor()
78 $entryRepo->expects($this->exactly(4))
79 ->method('findByUrlAndUserId')
80 ->will($this->onConsecutiveCalls(false, true, false, false));
83 ->expects($this->any())
84 ->method('getRepository')
85 ->willReturn($entryRepo);
87 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
88 ->disableOriginalConstructor()
92 ->expects($this->exactly(3))
93 ->method('updateEntry')
96 $res = $wallabagV1Import->import();
98 $this->assertTrue($res);
99 $this->assertEquals(['skipped' => 1, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
102 public function testImportAndMarkAllAsRead()
104 $wallabagV1Import = $this->getWallabagV1Import();
105 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1-read.json');
107 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
108 ->disableOriginalConstructor()
111 $entryRepo->expects($this->exactly(3))
112 ->method('findByUrlAndUserId')
113 ->will($this->onConsecutiveCalls(false, false, false));
116 ->expects($this->any())
117 ->method('getRepository')
118 ->willReturn($entryRepo);
121 ->expects($this->exactly(3))
122 ->method('updateEntry')
123 ->willReturn(new Entry($this->user
));
125 // check that every entry persisted are archived
127 ->expects($this->any())
129 ->with($this->callback(function ($persistedEntry) {
130 return $persistedEntry->isArchived();
133 $res = $wallabagV1Import->setMarkAsRead(true)->import();
135 $this->assertTrue($res);
137 $this->assertEquals(['skipped' => 0, 'imported' => 3, 'queued' => 0], $wallabagV1Import->getSummary());
140 public function testImportWithRabbit()
142 $wallabagV1Import = $this->getWallabagV1Import();
143 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
145 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
146 ->disableOriginalConstructor()
149 $entryRepo->expects($this->never())
150 ->method('findByUrlAndUserId');
153 ->expects($this->never())
154 ->method('getRepository');
156 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
157 ->disableOriginalConstructor()
161 ->expects($this->never())
162 ->method('updateEntry');
164 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
165 ->disableOriginalConstructor()
169 ->expects($this->exactly(4))
172 $wallabagV1Import->setProducer($producer);
174 $res = $wallabagV1Import->setMarkAsRead(true)->import();
176 $this->assertTrue($res);
177 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
180 public function testImportWithRedis()
182 $wallabagV1Import = $this->getWallabagV1Import();
183 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.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');
196 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
197 ->disableOriginalConstructor()
201 ->expects($this->never())
202 ->method('updateEntry');
204 $factory = new RedisMockFactory();
205 $redisMock = $factory->getAdapter('Predis\Client', true);
207 $queue = new RedisQueue($redisMock, 'wallabag_v1');
208 $producer = new Producer($queue);
210 $wallabagV1Import->setProducer($producer);
212 $res = $wallabagV1Import->setMarkAsRead(true)->import();
214 $this->assertTrue($res);
215 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $wallabagV1Import->getSummary());
217 $this->assertNotEmpty($redisMock->lpop('wallabag_v1'));
220 public function testImportBadFile()
222 $wallabagV1Import = $this->getWallabagV1Import();
223 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.jsonx');
225 $res = $wallabagV1Import->import();
227 $this->assertFalse($res);
229 $records = $this->logHandler
->getRecords();
230 $this->assertContains('WallabagImport: unable to read file', $records[0]['message']);
231 $this->assertEquals('ERROR', $records[0]['level_name']);
234 public function testImportUserNotDefined()
236 $wallabagV1Import = $this->getWallabagV1Import(true);
237 $wallabagV1Import->setFilepath(__DIR__
.'/../fixtures/wallabag-v1.json');
239 $res = $wallabagV1Import->import();
241 $this->assertFalse($res);
243 $records = $this->logHandler
->getRecords();
244 $this->assertContains('WallabagImport: user is not defined', $records[0]['message']);
245 $this->assertEquals('ERROR', $records[0]['level_name']);