]>
Commit | Line | Data |
---|---|---|
ff1a5362 JB |
1 | <?php |
2 | ||
3 | namespace Tests\Wallabag\ImportBundle\Import; | |
4 | ||
f808b016 | 5 | use M6Web\Component\RedisMock\RedisMockFactory; |
ff1a5362 | 6 | use Monolog\Handler\TestHandler; |
f808b016 | 7 | use Monolog\Logger; |
bd91bd5c | 8 | use PHPUnit\Framework\TestCase; |
ff1a5362 | 9 | use Simpleue\Queue\RedisQueue; |
f808b016 JB |
10 | use Wallabag\CoreBundle\Entity\Entry; |
11 | use Wallabag\ImportBundle\Import\InstapaperImport; | |
12 | use Wallabag\ImportBundle\Redis\Producer; | |
13 | use Wallabag\UserBundle\Entity\User; | |
ff1a5362 | 14 | |
bd91bd5c | 15 | class InstapaperImportTest extends TestCase |
ff1a5362 JB |
16 | { |
17 | protected $user; | |
18 | protected $em; | |
19 | protected $logHandler; | |
20 | protected $contentProxy; | |
6bc6fb1f | 21 | protected $tagsAssigner; |
7a8ed3ce | 22 | protected $uow; |
ff1a5362 | 23 | |
ff1a5362 JB |
24 | public function testInit() |
25 | { | |
26 | $instapaperImport = $this->getInstapaperImport(); | |
27 | ||
f808b016 | 28 | $this->assertSame('Instapaper', $instapaperImport->getName()); |
ff1a5362 | 29 | $this->assertNotEmpty($instapaperImport->getUrl()); |
f808b016 | 30 | $this->assertSame('import.instapaper.description', $instapaperImport->getDescription()); |
ff1a5362 JB |
31 | } |
32 | ||
33 | public function testImport() | |
34 | { | |
7a8ed3ce | 35 | $instapaperImport = $this->getInstapaperImport(false, 4); |
f808b016 | 36 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
ff1a5362 JB |
37 | |
38 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
39 | ->disableOriginalConstructor() | |
40 | ->getMock(); | |
41 | ||
7a8ed3ce | 42 | $entryRepo->expects($this->exactly(4)) |
ff1a5362 JB |
43 | ->method('findByUrlAndUserId') |
44 | ->willReturn(false); | |
45 | ||
46 | $this->em | |
47 | ->expects($this->any()) | |
48 | ->method('getRepository') | |
49 | ->willReturn($entryRepo); | |
50 | ||
51 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
52 | ->disableOriginalConstructor() | |
53 | ->getMock(); | |
54 | ||
55 | $this->contentProxy | |
7a8ed3ce | 56 | ->expects($this->exactly(4)) |
ff1a5362 JB |
57 | ->method('updateEntry') |
58 | ->willReturn($entry); | |
59 | ||
60 | $res = $instapaperImport->import(); | |
61 | ||
62 | $this->assertTrue($res); | |
f808b016 | 63 | $this->assertSame(['skipped' => 0, 'imported' => 4, 'queued' => 0], $instapaperImport->getSummary()); |
ff1a5362 JB |
64 | } |
65 | ||
66 | public function testImportAndMarkAllAsRead() | |
67 | { | |
7816eb62 | 68 | $instapaperImport = $this->getInstapaperImport(false, 1); |
f808b016 | 69 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
ff1a5362 JB |
70 | |
71 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
72 | ->disableOriginalConstructor() | |
73 | ->getMock(); | |
74 | ||
7a8ed3ce | 75 | $entryRepo->expects($this->exactly(4)) |
ff1a5362 | 76 | ->method('findByUrlAndUserId') |
7a8ed3ce | 77 | ->will($this->onConsecutiveCalls(false, true, true, true)); |
ff1a5362 JB |
78 | |
79 | $this->em | |
80 | ->expects($this->any()) | |
81 | ->method('getRepository') | |
82 | ->willReturn($entryRepo); | |
83 | ||
84 | $this->contentProxy | |
85 | ->expects($this->once()) | |
86 | ->method('updateEntry') | |
87 | ->willReturn(new Entry($this->user)); | |
88 | ||
89 | // check that every entry persisted are archived | |
90 | $this->em | |
91 | ->expects($this->once()) | |
92 | ->method('persist') | |
93 | ->with($this->callback(function ($persistedEntry) { | |
94 | return $persistedEntry->isArchived(); | |
95 | })); | |
96 | ||
97 | $res = $instapaperImport->setMarkAsRead(true)->import(); | |
98 | ||
99 | $this->assertTrue($res); | |
100 | ||
f808b016 | 101 | $this->assertSame(['skipped' => 3, 'imported' => 1, 'queued' => 0], $instapaperImport->getSummary()); |
ff1a5362 JB |
102 | } |
103 | ||
104 | public function testImportWithRabbit() | |
105 | { | |
106 | $instapaperImport = $this->getInstapaperImport(); | |
f808b016 | 107 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
ff1a5362 JB |
108 | |
109 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | |
110 | ->disableOriginalConstructor() | |
111 | ->getMock(); | |
112 | ||
113 | $entryRepo->expects($this->never()) | |
114 | ->method('findByUrlAndUserId'); | |
115 | ||
116 | $this->em | |
117 | ->expects($this->never()) | |
118 | ->method('getRepository'); | |
119 | ||
120 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
121 | ->disableOriginalConstructor() | |
122 | ->getMock(); | |
123 | ||
124 | $this->contentProxy | |
125 | ->expects($this->never()) | |
126 | ->method('updateEntry'); | |
127 | ||
128 | $producer = $this->getMockBuilder('OldSound\RabbitMqBundle\RabbitMq\Producer') | |
129 | ->disableOriginalConstructor() | |
130 | ->getMock(); | |
131 | ||
132 | $producer | |
7a8ed3ce | 133 | ->expects($this->exactly(4)) |
ff1a5362 JB |
134 | ->method('publish'); |
135 | ||
136 | $instapaperImport->setProducer($producer); | |
137 | ||
138 | $res = $instapaperImport->setMarkAsRead(true)->import(); | |
139 | ||
140 | $this->assertTrue($res); | |
f808b016 | 141 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary()); |
ff1a5362 JB |
142 | } |
143 | ||
144 | public function testImportWithRedis() | |
145 | { | |
146 | $instapaperImport = $this->getInstapaperImport(); | |
f808b016 | 147 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
ff1a5362 JB |
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 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | |
161 | ->disableOriginalConstructor() | |
162 | ->getMock(); | |
163 | ||
164 | $this->contentProxy | |
165 | ->expects($this->never()) | |
166 | ->method('updateEntry'); | |
167 | ||
168 | $factory = new RedisMockFactory(); | |
169 | $redisMock = $factory->getAdapter('Predis\Client', true); | |
170 | ||
171 | $queue = new RedisQueue($redisMock, 'instapaper'); | |
172 | $producer = new Producer($queue); | |
173 | ||
174 | $instapaperImport->setProducer($producer); | |
175 | ||
176 | $res = $instapaperImport->setMarkAsRead(true)->import(); | |
177 | ||
178 | $this->assertTrue($res); | |
f808b016 | 179 | $this->assertSame(['skipped' => 0, 'imported' => 0, 'queued' => 4], $instapaperImport->getSummary()); |
ff1a5362 JB |
180 | |
181 | $this->assertNotEmpty($redisMock->lpop('instapaper')); | |
182 | } | |
183 | ||
184 | public function testImportBadFile() | |
185 | { | |
186 | $instapaperImport = $this->getInstapaperImport(); | |
f808b016 | 187 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/wallabag-v1.jsonx'); |
ff1a5362 JB |
188 | |
189 | $res = $instapaperImport->import(); | |
190 | ||
191 | $this->assertFalse($res); | |
192 | ||
193 | $records = $this->logHandler->getRecords(); | |
194 | $this->assertContains('InstapaperImport: unable to read file', $records[0]['message']); | |
f808b016 | 195 | $this->assertSame('ERROR', $records[0]['level_name']); |
ff1a5362 JB |
196 | } |
197 | ||
198 | public function testImportUserNotDefined() | |
199 | { | |
200 | $instapaperImport = $this->getInstapaperImport(true); | |
f808b016 | 201 | $instapaperImport->setFilepath(__DIR__ . '/../fixtures/instapaper-export.csv'); |
ff1a5362 JB |
202 | |
203 | $res = $instapaperImport->import(); | |
204 | ||
205 | $this->assertFalse($res); | |
206 | ||
207 | $records = $this->logHandler->getRecords(); | |
208 | $this->assertContains('InstapaperImport: user is not defined', $records[0]['message']); | |
f808b016 JB |
209 | $this->assertSame('ERROR', $records[0]['level_name']); |
210 | } | |
211 | ||
212 | private function getInstapaperImport($unsetUser = false, $dispatched = 0) | |
213 | { | |
214 | $this->user = new User(); | |
215 | ||
216 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | |
217 | ->disableOriginalConstructor() | |
218 | ->getMock(); | |
219 | ||
220 | $this->uow = $this->getMockBuilder('Doctrine\ORM\UnitOfWork') | |
221 | ->disableOriginalConstructor() | |
222 | ->getMock(); | |
223 | ||
224 | $this->em | |
225 | ->expects($this->any()) | |
226 | ->method('getUnitOfWork') | |
227 | ->willReturn($this->uow); | |
228 | ||
229 | $this->uow | |
230 | ->expects($this->any()) | |
231 | ->method('getScheduledEntityInsertions') | |
232 | ->willReturn([]); | |
233 | ||
234 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | |
235 | ->disableOriginalConstructor() | |
236 | ->getMock(); | |
237 | ||
238 | $this->tagsAssigner = $this->getMockBuilder('Wallabag\CoreBundle\Helper\TagsAssigner') | |
239 | ->disableOriginalConstructor() | |
240 | ->getMock(); | |
241 | ||
242 | $dispatcher = $this->getMockBuilder('Symfony\Component\EventDispatcher\EventDispatcher') | |
243 | ->disableOriginalConstructor() | |
244 | ->getMock(); | |
245 | ||
246 | $dispatcher | |
247 | ->expects($this->exactly($dispatched)) | |
248 | ->method('dispatch'); | |
249 | ||
250 | $import = new InstapaperImport($this->em, $this->contentProxy, $this->tagsAssigner, $dispatcher); | |
251 | ||
252 | $this->logHandler = new TestHandler(); | |
253 | $logger = new Logger('test', [$this->logHandler]); | |
254 | $import->setLogger($logger); | |
255 | ||
256 | if (false === $unsetUser) { | |
257 | $import->setUser($this->user); | |
258 | } | |
259 | ||
260 | return $import; | |
ff1a5362 JB |
261 | } |
262 | } |