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