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