]>
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; |
6bc6fb1f TC |
20 | protected $tagsAssigner; |
21 | protected $uow; | |
6785f4aa | 22 | |
7816eb62 | 23 | private function getWallabagV2Import($unsetUser = false, $dispatched = 0) |
6785f4aa NL |
24 | { |
25 | $this->user = new User(); | |
26 | ||
27 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
28 | ->disableOriginalConstructor() | |
29 | ->getMock(); | |
30 | ||
40113585 JB |
31 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') |
32 | ->disableOriginalConstructor() | |
33 | ->getMock(); | |
34 | ||
35 | $this->em | |
36 | ->expects($this->any()) | |
37 | ->method('getUnitOfWork') | |
38 | ->willReturn($this->uow); | |
39 | ||
40 | $this->uow | |
41 | ->expects($this->any()) | |
42 | ->method('getScheduledEntityInsertions') | |
43 | ->willReturn([]); | |
44 | ||
0783c99a TC |
45 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') |
46 | ->disableOriginalConstructor() | |
47 | ->getMock(); | |
48 | ||
6bc6fb1f TC |
49 | $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner') |
50 | ->disableOriginalConstructor() | |
51 | ->getMock(); | |
52 | ||
7816eb62 JB |
53 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') |
54 | ->disableOriginalConstructor() | |
55 | ->getMock(); | |
56 | ||
57 | $dispatcher | |
58 | ->expects($this->exactly($dispatched)) | |
59 | ->method('dispatch'); | |
60 | ||
6bc6fb1f | 61 | $wallabag = new WallabagV2Import($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher); |
6785f4aa NL |
62 | |
63 | $this->logHandler = new TestHandler(); | |
4094ea47 | 64 | $logger = new Logger('test', [$this->logHandler]); |
6785f4aa NL |
65 | $wallabag->setLogger($logger); |
66 | ||
67 | if (false === $unsetUser) { | |
68 | $wallabag->setUser($this->user); | |
69 | } | |
70 | ||
71 | return $wallabag; | |
72 | } | |
73 | ||
74 | public function testInit() | |
75 | { | |
76 | $wallabagV2Import = $this->getWallabagV2Import(); | |
77 | ||
78 | $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); | |
79 | $this->assertNotEmpty($wallabagV2Import->getUrl()); | |
0d42217e | 80 | $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); |
6785f4aa NL |
81 | } |
82 | ||
83 | public function testImport() | |
84 | { | |
7816eb62 | 85 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
6785f4aa NL |
86 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); |
87 | ||
88 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
89 | ->disableOriginalConstructor() | |
90 | ->getMock(); | |
91 | ||
cf05a1ae | 92 | $entryRepo->expects($this->exactly(6)) |
6785f4aa NL |
93 | ->method('findByUrlAndUserId') |
94 | ->will($this->onConsecutiveCalls(false, true, false)); | |
95 | ||
96 | $this->em | |
97 | ->expects($this->any()) | |
98 | ->method('getRepository') | |
99 | ->willReturn($entryRepo); | |
100 | ||
8f336fda JB |
101 | $this->contentProxy |
102 | ->expects($this->exactly(2)) | |
103 | ->method('updateEntry') | |
104 | ->willReturn(new Entry($this->user)); | |
105 | ||
6785f4aa NL |
106 | $res = $wallabagV2Import->import(); |
107 | ||
108 | $this->assertTrue($res); | |
cf05a1ae | 109 | $this->assertEquals(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
c32ae320 TC |
110 | } |
111 | ||
112 | public function testImportAndMarkAllAsRead() | |
113 | { | |
7816eb62 | 114 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
c32ae320 TC |
115 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); |
116 | ||
117 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
118 | ->disableOriginalConstructor() | |
119 | ->getMock(); | |
120 | ||
121 | $entryRepo->expects($this->exactly(2)) | |
122 | ->method('findByUrlAndUserId') | |
123 | ->will($this->onConsecutiveCalls(false, false)); | |
124 | ||
125 | $this->em | |
126 | ->expects($this->any()) | |
127 | ->method('getRepository') | |
128 | ->willReturn($entryRepo); | |
129 | ||
8f336fda JB |
130 | $this->contentProxy |
131 | ->expects($this->exactly(2)) | |
132 | ->method('updateEntry') | |
133 | ->willReturn(new Entry($this->user)); | |
134 | ||
79d0e38e JB |
135 | // check that every entry persisted are archived |
136 | $this->em | |
137 | ->expects($this->any()) | |
138 | ->method('persist') | |
cebb4223 | 139 | ->with($this->callback(function ($persistedEntry) { |
79d0e38e JB |
140 | return $persistedEntry->isArchived(); |
141 | })); | |
142 | ||
c32ae320 TC |
143 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); |
144 | ||
145 | $this->assertTrue($res); | |
146 | ||
c80cc01a | 147 | $this->assertEquals(['skipped' => 0, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
6785f4aa NL |
148 | } |
149 | ||
13470c35 JB |
150 | public function testImportWithRabbit() |
151 | { | |
152 | $wallabagV2Import = $this->getWallabagV2Import(); | |
153 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
154 | ||
155 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
156 | ->disableOriginalConstructor() | |
157 | ->getMock(); | |
158 | ||
159 | $entryRepo->expects($this->never()) | |
160 | ->method('findByUrlAndUserId'); | |
161 | ||
162 | $this->em | |
163 | ->expects($this->never()) | |
164 | ->method('getRepository'); | |
165 | ||
166 | $this->contentProxy | |
167 | ->expects($this->never()) | |
168 | ->method('updateEntry'); | |
169 | ||
170 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
171 | ->disableOriginalConstructor() | |
172 | ->getMock(); | |
173 | ||
174 | $producer | |
cf05a1ae | 175 | ->expects($this->exactly(6)) |
13470c35 JB |
176 | ->method('publish'); |
177 | ||
b3437d58 | 178 | $wallabagV2Import->setProducer($producer); |
13470c35 JB |
179 | |
180 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | |
181 | ||
182 | $this->assertTrue($res); | |
cf05a1ae | 183 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary()); |
13470c35 JB |
184 | } |
185 | ||
b3437d58 JB |
186 | public function testImportWithRedis() |
187 | { | |
188 | $wallabagV2Import = $this->getWallabagV2Import(); | |
189 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
190 | ||
191 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
192 | ->disableOriginalConstructor() | |
193 | ->getMock(); | |
194 | ||
195 | $entryRepo->expects($this->never()) | |
196 | ->method('findByUrlAndUserId'); | |
197 | ||
198 | $this->em | |
199 | ->expects($this->never()) | |
200 | ->method('getRepository'); | |
201 | ||
202 | $this->contentProxy | |
203 | ->expects($this->never()) | |
204 | ->method('updateEntry'); | |
205 | ||
206 | $factory = new RedisMockFactory(); | |
207 | $redisMock = $factory->getAdapter('Predis\Client', true); | |
208 | ||
209 | $queue = new RedisQueue($redisMock, 'wallabag_v2'); | |
210 | $producer = new Producer($queue); | |
211 | ||
212 | $wallabagV2Import->setProducer($producer); | |
213 | ||
214 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | |
215 | ||
216 | $this->assertTrue($res); | |
cf05a1ae | 217 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 6], $wallabagV2Import->getSummary()); |
b3437d58 JB |
218 | |
219 | $this->assertNotEmpty($redisMock->lpop('wallabag_v2')); | |
220 | } | |
221 | ||
6785f4aa NL |
222 | public function testImportBadFile() |
223 | { | |
224 | $wallabagV1Import = $this->getWallabagV2Import(); | |
225 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); | |
226 | ||
227 | $res = $wallabagV1Import->import(); | |
228 | ||
229 | $this->assertFalse($res); | |
230 | ||
231 | $records = $this->logHandler->getRecords(); | |
232 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); | |
233 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
234 | } | |
235 | ||
236 | public function testImportUserNotDefined() | |
237 | { | |
238 | $wallabagV1Import = $this->getWallabagV2Import(true); | |
239 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | |
240 | ||
241 | $res = $wallabagV1Import->import(); | |
242 | ||
243 | $this->assertFalse($res); | |
244 | ||
245 | $records = $this->logHandler->getRecords(); | |
246 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); | |
247 | $this->assertEquals('ERROR', $records[0]['level_name']); | |
248 | } | |
19d9efab JB |
249 | |
250 | public function testImportEmptyFile() | |
251 | { | |
252 | $wallabagV2Import = $this->getWallabagV2Import(); | |
253 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-empty.json'); | |
254 | ||
255 | $res = $wallabagV2Import->import(); | |
256 | ||
257 | $this->assertFalse($res); | |
c80cc01a | 258 | $this->assertEquals(['skipped' => 0, 'imported' => 0, 'queued' => 0], $wallabagV2Import->getSummary()); |
19d9efab JB |
259 | } |
260 | ||
261 | public function testImportWithExceptionFromGraby() | |
262 | { | |
7816eb62 | 263 | $wallabagV2Import = $this->getWallabagV2Import(false, 2); |
19d9efab JB |
264 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); |
265 | ||
266 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
267 | ->disableOriginalConstructor() | |
268 | ->getMock(); | |
269 | ||
cf05a1ae | 270 | $entryRepo->expects($this->exactly(6)) |
19d9efab JB |
271 | ->method('findByUrlAndUserId') |
272 | ->will($this->onConsecutiveCalls(false, true, false)); | |
273 | ||
274 | $this->em | |
275 | ->expects($this->any()) | |
276 | ->method('getRepository') | |
277 | ->willReturn($entryRepo); | |
278 | ||
279 | $this->contentProxy | |
280 | ->expects($this->exactly(2)) | |
281 | ->method('updateEntry') | |
282 | ->will($this->throwException(new \Exception())); | |
283 | ||
284 | $res = $wallabagV2Import->import(); | |
285 | ||
286 | $this->assertTrue($res); | |
cf05a1ae | 287 | $this->assertEquals(['skipped' => 4, 'imported' => 2, 'queued' => 0], $wallabagV2Import->getSummary()); |
19d9efab | 288 | } |
6785f4aa | 289 | } |