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