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