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