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