]>
Commit | Line | Data |
---|---|---|
6785f4aa NL |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\ImportBundle\Import; |
6785f4aa | 4 | |
f808b016 | 5 | use M6Web\Component\RedisMock\RedisMockFactory; |
6785f4aa | 6 | use Monolog\Handler\TestHandler; |
f808b016 | 7 | use Monolog\Logger; |
bd91bd5c | 8 | use PHPUnit\Framework\TestCase; |
b3437d58 | 9 | use Simpleue\Queue\RedisQueue; |
f808b016 JB |
10 | use Wallabag\CoreBundle\Entity\Entry; |
11 | use Wallabag\ImportBundle\Import\WallabagV2Import; | |
12 | use Wallabag\ImportBundle\Redis\Producer; | |
13 | use Wallabag\UserBundle\Entity\User; | |
6785f4aa | 14 | |
bd91bd5c | 15 | class WallabagV2ImportTest extends TestCase |
6785f4aa NL |
16 | { |
17 | protected $user; | |
18 | protected $em; | |
19 | protected $logHandler; | |
0783c99a | 20 | protected $contentProxy; |
6bc6fb1f TC |
21 | protected $tagsAssigner; |
22 | protected $uow; | |
6785f4aa | 23 | |
6785f4aa NL |
24 | public function testInit() |
25 | { | |
26 | $wallabagV2Import = $this->getWallabagV2Import(); | |
27 | ||
f808b016 | 28 | $this->assertSame('wallabag v2', $wallabagV2Import->getName()); |
6785f4aa | 29 | $this->assertNotEmpty($wallabagV2Import->getUrl()); |
f808b016 | 30 | $this->assertSame('import.wallabag_v2.description', $wallabagV2Import->getDescription()); |
6785f4aa NL |
31 | } |
32 | ||
33 | public function testImport() | |
34 | { | |
7816eb62 | 35 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
f808b016 | 36 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json'); |
6785f4aa NL |
37 | |
38 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
39 | ->disableOriginalConstructor() | |
40 | ->getMock(); | |
41 | ||
cf05a1ae | 42 | $entryRepo->expects($this->exactly(6)) |
6785f4aa NL |
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 | ||
8f336fda JB |
51 | $this->contentProxy |
52 | ->expects($this->exactly(2)) | |
53 | ->method('updateEntry') | |
54 | ->willReturn(new Entry($this->user)); | |
55 | ||
6785f4aa NL |
56 | $res = $wallabagV2Import->import(); |
57 | ||
58 | $this->assertTrue($res); | |
f808b016 | 59 | $this->assertSame(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
c32ae320 TC |
60 | } |
61 | ||
62 | public function testImportAndMarkAllAsRead() | |
63 | { | |
7816eb62 | 64 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
f808b016 | 65 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2-read.json'); |
c32ae320 TC |
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 | ||
8f336fda JB |
80 | $this->contentProxy |
81 | ->expects($this->exactly(2)) | |
82 | ->method('updateEntry') | |
83 | ->willReturn(new Entry($this->user)); | |
84 | ||
79d0e38e JB |
85 | // check that every entry persisted are archived |
86 | $this->em | |
87 | ->expects($this->any()) | |
88 | ->method('persist') | |
cebb4223 | 89 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
90 | return $persistedEntry->isArchived(); |
91 | })); | |
92 | ||
c32ae320 TC |
93 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); |
94 | ||
95 | $this->assertTrue($res); | |
96 | ||
f808b016 | 97 | $this->assertSame(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
6785f4aa NL |
98 | } |
99 | ||
13470c35 JB |
100 | public function testImportWithRabbit() |
101 | { | |
102 | $wallabagV2Import = $this->getWallabagV2Import(); | |
f808b016 | 103 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json'); |
13470c35 JB |
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 | |
cf05a1ae | 125 | ->expects($this->exactly(6)) |
13470c35 JB |
126 | ->method('publish'); |
127 | ||
b3437d58 | 128 | $wallabagV2Import->setProducer($producer); |
13470c35 JB |
129 | |
130 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | |
131 | ||
132 | $this->assertTrue($res); | |
f808b016 | 133 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary()); |
13470c35 JB |
134 | } |
135 | ||
b3437d58 JB |
136 | public function testImportWithRedis() |
137 | { | |
138 | $wallabagV2Import = $this->getWallabagV2Import(); | |
f808b016 | 139 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json'); |
b3437d58 JB |
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); | |
f808b016 | 167 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary()); |
b3437d58 JB |
168 | |
169 | $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); | |
170 | } | |
171 | ||
6785f4aa NL |
172 | public function testImportBadFile() |
173 | { | |
174 | $wallabagV1Import = $this->getWallabagV2Import(); | |
f808b016 | 175 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.jsonx'); |
6785f4aa NL |
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']); | |
f808b016 | 183 | $this->assertSame('ERROR', $records[0]['level_name']); |
6785f4aa NL |
184 | } |
185 | ||
186 | public function testImportUserNotDefined() | |
187 | { | |
188 | $wallabagV1Import = $this->getWallabagV2Import(true); | |
f808b016 | 189 | $wallabagV1Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json'); |
6785f4aa NL |
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']); | |
f808b016 | 197 | $this->assertSame('ERROR', $records[0]['level_name']); |
6785f4aa | 198 | } |
19d9efab JB |
199 | |
200 | public function testImportEmptyFile() | |
201 | { | |
202 | $wallabagV2Import = $this->getWallabagV2Import(); | |
f808b016 | 203 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2-empty.json'); |
19d9efab JB |
204 | |
205 | $res = $wallabagV2Import->import(); | |
206 | ||
207 | $this->assertFalse($res); | |
f808b016 | 208 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary()); |
19d9efab JB |
209 | } |
210 | ||
211 | public function testImportWithExceptionFromGraby() | |
212 | { | |
7816eb62 | 213 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
f808b016 | 214 | $wallabagV2Import->setFilepath(__DIR__ . '/../fixtures/wallabag-v2.json'); |
19d9efab JB |
215 | |
216 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
217 | ->disableOriginalConstructor() | |
218 | ->getMock(); | |
219 | ||
cf05a1ae | 220 | $entryRepo->expects($this->exactly(6)) |
19d9efab JB |
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); | |
f808b016 JB |
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; | |
19d9efab | 289 | } |
6785f4aa | 290 | } |