]> git.immae.eu Git - github/wallabag/wallabag.git/blame_incremental - tests/Wallabag/ImportBundle/Import/InstapaperImportTest.php
Merge remote-tracking branch 'origin/master' into 2.3
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Import / InstapaperImportTest.php
... / ...
CommitLineData
1<?php
2
3namespace Tests\Wallabag\ImportBundle\Import;
4
5use Wallabag\ImportBundle\Import\InstapaperImport;
6use Wallabag\UserBundle\Entity\User;
7use Wallabag\CoreBundle\Entity\Entry;
8use Wallabag\ImportBundle\Redis\Producer;
9use Monolog\Logger;
10use Monolog\Handler\TestHandler;
11use Simpleue\Queue\RedisQueue;
12use M6Web\Component\RedisMock\RedisMockFactory;
13
14class InstapaperImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $user;
17 protected $em;
18 protected $logHandler;
19 protected $contentProxy;
20 protected $tagsAssigner;
21 protected $uow;
22
23 private function getInstapaperImport($unsetUser = false, $dispatched = 0)
24 {
25 $this->user = new User();
26
27 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
28 ->disableOriginalConstructor()
29 ->getMock();
30
31 $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork')
32 ->disableOriginalConstructor()
33 ->getMock();
34
35 $this->em
36 ->expects($this->any())
37 ->method('getUnitOfWork')
38 ->willReturn($this->uow);
39
40 $this->uow
41 ->expects($this->any())
42 ->method('getScheduledEntityInsertions')
43 ->willReturn([]);
44
45 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
46 ->disableOriginalConstructor()
47 ->getMock();
48
49 $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner')
50 ->disableOriginalConstructor()
51 ->getMock();
52
53 $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher')
54 ->disableOriginalConstructor()
55 ->getMock();
56
57 $dispatcher
58 ->expects($this->exactly($dispatched))
59 ->method('dispatch');
60
61 $import = new InstapaperImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher);
62
63 $this->logHandler = new TestHandler();
64 $logger = new Logger('test', [$this->logHandler]);
65 $import->setLogger($logger);
66
67 if (false === $unsetUser) {
68 $import->setUser($this->user);
69 }
70
71 return $import;
72 }
73
74 public function testInit()
75 {
76 $instapaperImport = $this->getInstapaperImport();
77
78 $this->assertEquals('Instapaper', $instapaperImport->getName());
79 $this->assertNotEmpty($instapaperImport->getUrl());
80 $this->assertEquals('import.instapaper.description', $instapaperImport->getDescription());
81 }
82
83 public function testImport()
84 {
85 $instapaperImport = $this->getInstapaperImport(false, 4);
86 $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
87
88 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
89 ->disableOriginalConstructor()
90 ->getMock();
91
92 $entryRepo->expects($this->exactly(4))
93 ->method('findByUrlAndUserId')
94 ->willReturn(false);
95
96 $this->em
97 ->expects($this->any())
98 ->method('getRepository')
99 ->willReturn($entryRepo);
100
101 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
102 ->disableOriginalConstructor()
103 ->getMock();
104
105 $this->contentProxy
106 ->expects($this->exactly(4))
107 ->method('updateEntry')
108 ->willReturn($entry);
109
110 $res = $instapaperImport->import();
111
112 $this->assertTrue($res);
113 $this->assertEquals(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary());
114 }
115
116 public function testImportAndMarkAllAsRead()
117 {
118 $instapaperImport = $this->getInstapaperImport(false, 1);
119 $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
120
121 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
122 ->disableOriginalConstructor()
123 ->getMock();
124
125 $entryRepo->expects($this->exactly(4))
126 ->method('findByUrlAndUserId')
127 ->will($this->onConsecutiveCalls(false, true, true, true));
128
129 $this->em
130 ->expects($this->any())
131 ->method('getRepository')
132 ->willReturn($entryRepo);
133
134 $this->contentProxy
135 ->expects($this->once())
136 ->method('updateEntry')
137 ->willReturn(new Entry($this->user));
138
139 // check that every entry persisted are archived
140 $this->em
141 ->expects($this->once())
142 ->method('persist')
143 ->with($this->callback(function ($persistedEntry) {
144 return $persistedEntry->isArchived();
145 }));
146
147 $res = $instapaperImport->setMarkAsRead(true)->import();
148
149 $this->assertTrue($res);
150
151 $this->assertEquals(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary());
152 }
153
154 public function testImportWithRabbit()
155 {
156 $instapaperImport = $this->getInstapaperImport();
157 $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
158
159 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
160 ->disableOriginalConstructor()
161 ->getMock();
162
163 $entryRepo->expects($this->never())
164 ->method('findByUrlAndUserId');
165
166 $this->em
167 ->expects($this->never())
168 ->method('getRepository');
169
170 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
171 ->disableOriginalConstructor()
172 ->getMock();
173
174 $this->contentProxy
175 ->expects($this->never())
176 ->method('updateEntry');
177
178 $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer')
179 ->disableOriginalConstructor()
180 ->getMock();
181
182 $producer
183 ->expects($this->exactly(4))
184 ->method('publish');
185
186 $instapaperImport->setProducer($producer);
187
188 $res = $instapaperImport->setMarkAsRead(true)->import();
189
190 $this->assertTrue($res);
191 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
192 }
193
194 public function testImportWithRedis()
195 {
196 $instapaperImport = $this->getInstapaperImport();
197 $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
198
199 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
200 ->disableOriginalConstructor()
201 ->getMock();
202
203 $entryRepo->expects($this->never())
204 ->method('findByUrlAndUserId');
205
206 $this->em
207 ->expects($this->never())
208 ->method('getRepository');
209
210 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
211 ->disableOriginalConstructor()
212 ->getMock();
213
214 $this->contentProxy
215 ->expects($this->never())
216 ->method('updateEntry');
217
218 $factory = new RedisMockFactory();
219 $redisMock = $factory->getAdapter('Predis\Client', true);
220
221 $queue = new RedisQueue($redisMock, 'instapaper');
222 $producer = new Producer($queue);
223
224 $instapaperImport->setProducer($producer);
225
226 $res = $instapaperImport->setMarkAsRead(true)->import();
227
228 $this->assertTrue($res);
229 $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary());
230
231 $this->assertNotEmpty($redisMock->lpop('instapaper'));
232 }
233
234 public function testImportBadFile()
235 {
236 $instapaperImport = $this->getInstapaperImport();
237 $instapaperImport->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
238
239 $res = $instapaperImport->import();
240
241 $this->assertFalse($res);
242
243 $records = $this->logHandler->getRecords();
244 $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']);
245 $this->assertEquals('ERROR', $records[0]['level_name']);
246 }
247
248 public function testImportUserNotDefined()
249 {
250 $instapaperImport = $this->getInstapaperImport(true);
251 $instapaperImport->setFilepath(__DIR__.'/../fixtures/instapaper-export.csv');
252
253 $res = $instapaperImport->import();
254
255 $this->assertFalse($res);
256
257 $records = $this->logHandler->getRecords();
258 $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']);
259 $this->assertEquals('ERROR', $records[0]['level_name']);
260 }
261}