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