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