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