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