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\InstapaperImport
;
12 use Wallabag\ImportBundle\Redis\Producer
;
13 use Wallabag\UserBundle\Entity\User
;
15 class InstapaperImportTest
extends TestCase
19 protected $logHandler;
20 protected $contentProxy;
21 protected $tagsAssigner;
24 public function testInit()
26 $instapaperImport = $this->getInstapaperImport();
28 $this->assertSame('Instapaper', $instapaperImport->getName());
29 $this->assertNotEmpty($instapaperImport->getUrl());
30 $this->assertSame('import.instapaper.description', $instapaperImport->getDescription());
33 public function testImport()
35 $instapaperImport = $this->getInstapaperImport(false, 4);
36 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/instapaper-export.csv');
38 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
39 ->disableOriginalConstructor()
42 $entryRepo->expects($this->exactly(4))
43 ->method('findByUrlAndUserId')
47 ->expects($this->any())
48 ->method('getRepository')
49 ->willReturn($entryRepo);
51 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
52 ->disableOriginalConstructor()
56 ->expects($this->exactly(4))
57 ->method('updateEntry')
60 $res = $instapaperImport->import();
62 $this->assertTrue($res);
63 $this->assertSame(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary());
66 public function testImportAndMarkAllAsRead()
68 $instapaperImport = $this->getInstapaperImport(false, 1);
69 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/instapaper-export.csv');
71 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
72 ->disableOriginalConstructor()
75 $entryRepo->expects($this->exactly(4))
76 ->method('findByUrlAndUserId')
77 ->will($this->onConsecutiveCalls(false, true, true, true));
80 ->expects($this->any())
81 ->method('getRepository')
82 ->willReturn($entryRepo);
85 ->expects($this->once())
86 ->method('updateEntry')
87 ->willReturn(new Entry($this->user
));
89 // check that every entry persisted are archived
91 ->expects($this->once())
93 ->with($this->callback(function ($persistedEntry) {
94 return $persistedEntry->isArchived();
97 $res = $instapaperImport->setMarkAsRead(true)->import();
99 $this->assertTrue($res);
101 $this->assertSame(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary());
104 public function testImportWithRabbit()
106 $instapaperImport = $this->getInstapaperImport();
107 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/instapaper-export.csv');
109 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
110 ->disableOriginalConstructor()
113 $entryRepo->expects($this->never())
114 ->method('findByUrlAndUserId');
117 ->expects($this->never())
118 ->method('getRepository');
120 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
121 ->disableOriginalConstructor()
125 ->expects($this->never())
126 ->method('updateEntry');
128 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
129 ->disableOriginalConstructor()
133 ->expects($this->exactly(4))
136 $instapaperImport->setProducer($producer);
138 $res = $instapaperImport->setMarkAsRead(true)->import();
140 $this->assertTrue($res);
141 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
144 public function testImportWithRedis()
146 $instapaperImport = $this->getInstapaperImport();
147 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/instapaper-export.csv');
149 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
150 ->disableOriginalConstructor()
153 $entryRepo->expects($this->never())
154 ->method('findByUrlAndUserId');
157 ->expects($this->never())
158 ->method('getRepository');
160 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
161 ->disableOriginalConstructor()
165 ->expects($this->never())
166 ->method('updateEntry');
168 $factory = new RedisMockFactory();
169 $redisMock = $factory->getAdapter('Predis\Client', true);
171 $queue = new RedisQueue($redisMock, 'instapaper');
172 $producer = new Producer($queue);
174 $instapaperImport->setProducer($producer);
176 $res = $instapaperImport->setMarkAsRead(true)->import();
178 $this->assertTrue($res);
179 $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
181 $this->assertNotEmpty($redisMock->lpop('instapaper'));
184 public function testImportBadFile()
186 $instapaperImport = $this->getInstapaperImport();
187 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/wallabag-v1.jsonx');
189 $res = $instapaperImport->import();
191 $this->assertFalse($res);
193 $records = $this->logHandler
->getRecords();
194 $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']);
195 $this->assertSame('ERROR', $records[0]['level_name']);
198 public function testImportUserNotDefined()
200 $instapaperImport = $this->getInstapaperImport(true);
201 $instapaperImport->setFilepath(__DIR__
. '/../fixtures/instapaper-export.csv');
203 $res = $instapaperImport->import();
205 $this->assertFalse($res);
207 $records = $this->logHandler
->getRecords();
208 $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']);
209 $this->assertSame('ERROR', $records[0]['level_name']);
212 private function getInstapaperImport($unsetUser = false, $dispatched = 0)
214 $this->user
= new User();
216 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
217 ->disableOriginalConstructor()
220 $this->uow
= $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
221 ->disableOriginalConstructor()
225 ->expects($this->any())
226 ->method('getUnitOfWork')
227 ->willReturn($this->uow
);
230 ->expects($this->any())
231 ->method('getScheduledEntityInsertions')
234 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
235 ->disableOriginalConstructor()
238 $this->tagsAssigner
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
239 ->disableOriginalConstructor()
242 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
243 ->disableOriginalConstructor()
247 ->expects($this->exactly($dispatched))
248 ->method('dispatch');
250 $import = new InstapaperImport($this->em
, $this->contentProxy
, $this->tagsAssigner
, $dispatcher);
252 $this->logHandler
= new TestHandler();
253 $logger = new Logger('test', [$this->logHandler
]);
254 $import->setLogger($logger);
256 if (false === $unsetUser) {
257 $import->setUser($this->user
);