diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-01 21:27:35 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-22 17:59:35 +0200 |
commit | 23634d5d842dabcf5d7475e2becb7e127824239e (patch) | |
tree | b91688722a996c46f27e8fe0542c356424483da3 /tests/Wallabag/ImportBundle/Import | |
parent | 891a026e31ad54ca90b70f6026f23260cfadb7fd (diff) | |
download | wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.gz wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.tar.zst wallabag-23634d5d842dabcf5d7475e2becb7e127824239e.zip |
Jump to Symfony 3.1
Diffstat (limited to 'tests/Wallabag/ImportBundle/Import')
5 files changed, 757 insertions, 0 deletions
diff --git a/tests/Wallabag/ImportBundle/Import/ImportChainTest.php b/tests/Wallabag/ImportBundle/Import/ImportChainTest.php new file mode 100644 index 00000000..32568ce5 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ImportChainTest.php | |||
@@ -0,0 +1,21 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\ImportChain; | ||
6 | |||
7 | class ImportChainTest extends \PHPUnit_Framework_TestCase | ||
8 | { | ||
9 | public function testGetAll() | ||
10 | { | ||
11 | $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\ImportInterface') | ||
12 | ->disableOriginalConstructor() | ||
13 | ->getMock(); | ||
14 | |||
15 | $importChain = new ImportChain(); | ||
16 | $importChain->addImport($import, 'alias'); | ||
17 | |||
18 | $this->assertCount(1, $importChain->getAll()); | ||
19 | $this->assertEquals($import, $importChain->getAll()['alias']); | ||
20 | } | ||
21 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php b/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php new file mode 100644 index 00000000..71a007a9 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/ImportCompilerPassTest.php | |||
@@ -0,0 +1,47 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
6 | use Wallabag\ImportBundle\Import\ImportCompilerPass; | ||
7 | |||
8 | class ImportCompilerPassTest extends \PHPUnit_Framework_TestCase | ||
9 | { | ||
10 | public function testProcessNoDefinition() | ||
11 | { | ||
12 | $container = new ContainerBuilder(); | ||
13 | $res = $this->process($container); | ||
14 | |||
15 | $this->assertNull($res); | ||
16 | } | ||
17 | |||
18 | public function testProcess() | ||
19 | { | ||
20 | $container = new ContainerBuilder(); | ||
21 | $container | ||
22 | ->register('wallabag_import.chain') | ||
23 | ->setPublic(false) | ||
24 | ; | ||
25 | |||
26 | $container | ||
27 | ->register('foo') | ||
28 | ->addTag('wallabag_import.import', ['alias' => 'pocket']) | ||
29 | ; | ||
30 | |||
31 | $this->process($container); | ||
32 | |||
33 | $this->assertTrue($container->hasDefinition('wallabag_import.chain')); | ||
34 | |||
35 | $definition = $container->getDefinition('wallabag_import.chain'); | ||
36 | $this->assertTrue($definition->hasMethodCall('addImport')); | ||
37 | |||
38 | $calls = $definition->getMethodCalls(); | ||
39 | $this->assertEquals('pocket', $calls[0][1][1]); | ||
40 | } | ||
41 | |||
42 | protected function process(ContainerBuilder $container) | ||
43 | { | ||
44 | $repeatedPass = new ImportCompilerPass(); | ||
45 | $repeatedPass->process($container); | ||
46 | } | ||
47 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/PocketImportTest.php b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php new file mode 100644 index 00000000..41f9b51f --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/PocketImportTest.php | |||
@@ -0,0 +1,393 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\UserBundle\Entity\User; | ||
6 | use Wallabag\CoreBundle\Entity\Entry; | ||
7 | use Wallabag\ImportBundle\Import\PocketImport; | ||
8 | use GuzzleHttp\Client; | ||
9 | use GuzzleHttp\Subscriber\Mock; | ||
10 | use GuzzleHttp\Message\Response; | ||
11 | use GuzzleHttp\Stream\Stream; | ||
12 | use Monolog\Logger; | ||
13 | use Monolog\Handler\TestHandler; | ||
14 | |||
15 | class PocketImportMock extends PocketImport | ||
16 | { | ||
17 | public function getAccessToken() | ||
18 | { | ||
19 | return $this->accessToken; | ||
20 | } | ||
21 | } | ||
22 | |||
23 | class PocketImportTest extends \PHPUnit_Framework_TestCase | ||
24 | { | ||
25 | protected $token; | ||
26 | protected $user; | ||
27 | protected $em; | ||
28 | protected $contentProxy; | ||
29 | protected $logHandler; | ||
30 | |||
31 | private function getPocketImport($consumerKey = 'ConsumerKey') | ||
32 | { | ||
33 | $this->user = new User(); | ||
34 | |||
35 | $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') | ||
36 | ->disableOriginalConstructor() | ||
37 | ->getMock(); | ||
38 | |||
39 | $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') | ||
40 | ->disableOriginalConstructor() | ||
41 | ->getMock(); | ||
42 | |||
43 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
44 | ->disableOriginalConstructor() | ||
45 | ->getMock(); | ||
46 | |||
47 | $token->expects($this->once()) | ||
48 | ->method('getUser') | ||
49 | ->willReturn($this->user); | ||
50 | |||
51 | $this->tokenStorage->expects($this->once()) | ||
52 | ->method('getToken') | ||
53 | ->willReturn($token); | ||
54 | |||
55 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
56 | ->disableOriginalConstructor() | ||
57 | ->getMock(); | ||
58 | |||
59 | $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config') | ||
60 | ->disableOriginalConstructor() | ||
61 | ->getMock(); | ||
62 | |||
63 | $config->expects($this->any()) | ||
64 | ->method('get') | ||
65 | ->with('pocket_consumer_key') | ||
66 | ->willReturn($consumerKey); | ||
67 | |||
68 | $pocket = new PocketImportMock( | ||
69 | $this->tokenStorage, | ||
70 | $this->em, | ||
71 | $this->contentProxy, | ||
72 | $config | ||
73 | ); | ||
74 | |||
75 | $this->logHandler = new TestHandler(); | ||
76 | $logger = new Logger('test', [$this->logHandler]); | ||
77 | $pocket->setLogger($logger); | ||
78 | |||
79 | return $pocket; | ||
80 | } | ||
81 | |||
82 | public function testInit() | ||
83 | { | ||
84 | $pocketImport = $this->getPocketImport(); | ||
85 | |||
86 | $this->assertEquals('Pocket', $pocketImport->getName()); | ||
87 | $this->assertNotEmpty($pocketImport->getUrl()); | ||
88 | $this->assertEquals('import.pocket.description', $pocketImport->getDescription()); | ||
89 | } | ||
90 | |||
91 | public function testOAuthRequest() | ||
92 | { | ||
93 | $client = new Client(); | ||
94 | |||
95 | $mock = new Mock([ | ||
96 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), | ||
97 | ]); | ||
98 | |||
99 | $client->getEmitter()->attach($mock); | ||
100 | |||
101 | $pocketImport = $this->getPocketImport(); | ||
102 | $pocketImport->setClient($client); | ||
103 | |||
104 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | ||
105 | |||
106 | $this->assertEquals('wunderbar_code', $code); | ||
107 | } | ||
108 | |||
109 | public function testOAuthRequestBadResponse() | ||
110 | { | ||
111 | $client = new Client(); | ||
112 | |||
113 | $mock = new Mock([ | ||
114 | new Response(403), | ||
115 | ]); | ||
116 | |||
117 | $client->getEmitter()->attach($mock); | ||
118 | |||
119 | $pocketImport = $this->getPocketImport(); | ||
120 | $pocketImport->setClient($client); | ||
121 | |||
122 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | ||
123 | |||
124 | $this->assertFalse($code); | ||
125 | |||
126 | $records = $this->logHandler->getRecords(); | ||
127 | $this->assertContains('PocketImport: Failed to request token', $records[0]['message']); | ||
128 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
129 | } | ||
130 | |||
131 | public function testOAuthAuthorize() | ||
132 | { | ||
133 | $client = new Client(); | ||
134 | |||
135 | $mock = new Mock([ | ||
136 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
137 | ]); | ||
138 | |||
139 | $client->getEmitter()->attach($mock); | ||
140 | |||
141 | $pocketImport = $this->getPocketImport(); | ||
142 | $pocketImport->setClient($client); | ||
143 | |||
144 | $res = $pocketImport->authorize('wunderbar_code'); | ||
145 | |||
146 | $this->assertTrue($res); | ||
147 | $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken()); | ||
148 | } | ||
149 | |||
150 | public function testOAuthAuthorizeBadResponse() | ||
151 | { | ||
152 | $client = new Client(); | ||
153 | |||
154 | $mock = new Mock([ | ||
155 | new Response(403), | ||
156 | ]); | ||
157 | |||
158 | $client->getEmitter()->attach($mock); | ||
159 | |||
160 | $pocketImport = $this->getPocketImport(); | ||
161 | $pocketImport->setClient($client); | ||
162 | |||
163 | $res = $pocketImport->authorize('wunderbar_code'); | ||
164 | |||
165 | $this->assertFalse($res); | ||
166 | |||
167 | $records = $this->logHandler->getRecords(); | ||
168 | $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']); | ||
169 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
170 | } | ||
171 | |||
172 | /** | ||
173 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
174 | */ | ||
175 | public function testImport() | ||
176 | { | ||
177 | $client = new Client(); | ||
178 | |||
179 | $mock = new Mock([ | ||
180 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
181 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
182 | { | ||
183 | "status": 1, | ||
184 | "list": { | ||
185 | "229279689": { | ||
186 | "item_id": "229279689", | ||
187 | "resolved_id": "229279689", | ||
188 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
189 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
190 | "favorite": "1", | ||
191 | "status": "1", | ||
192 | "resolved_title": "The Massive Ryder Cup Preview", | ||
193 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
194 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
195 | "is_article": "1", | ||
196 | "has_video": "1", | ||
197 | "has_image": "1", | ||
198 | "word_count": "3197", | ||
199 | "images": { | ||
200 | "1": { | ||
201 | "item_id": "229279689", | ||
202 | "image_id": "1", | ||
203 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", | ||
204 | "width": "0", | ||
205 | "height": "0", | ||
206 | "credit": "Jamie Squire/Getty Images", | ||
207 | "caption": "" | ||
208 | } | ||
209 | }, | ||
210 | "videos": { | ||
211 | "1": { | ||
212 | "item_id": "229279689", | ||
213 | "video_id": "1", | ||
214 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", | ||
215 | "width": "420", | ||
216 | "height": "315", | ||
217 | "type": "1", | ||
218 | "vid": "Er34PbFkVGk" | ||
219 | } | ||
220 | }, | ||
221 | "tags": { | ||
222 | "grantland": { | ||
223 | "item_id": "1147652870", | ||
224 | "tag": "grantland" | ||
225 | }, | ||
226 | "Ryder Cup": { | ||
227 | "item_id": "1147652870", | ||
228 | "tag": "Ryder Cup" | ||
229 | } | ||
230 | } | ||
231 | }, | ||
232 | "229279690": { | ||
233 | "item_id": "229279689", | ||
234 | "resolved_id": "229279689", | ||
235 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
236 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
237 | "favorite": "1", | ||
238 | "status": "1", | ||
239 | "resolved_title": "The Massive Ryder Cup Preview", | ||
240 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
241 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
242 | "is_article": "1", | ||
243 | "has_video": "0", | ||
244 | "has_image": "0", | ||
245 | "word_count": "3197" | ||
246 | } | ||
247 | } | ||
248 | } | ||
249 | ')), | ||
250 | ]); | ||
251 | |||
252 | $client->getEmitter()->attach($mock); | ||
253 | |||
254 | $pocketImport = $this->getPocketImport(); | ||
255 | |||
256 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
257 | ->disableOriginalConstructor() | ||
258 | ->getMock(); | ||
259 | |||
260 | $entryRepo->expects($this->exactly(2)) | ||
261 | ->method('findByUrlAndUserId') | ||
262 | ->will($this->onConsecutiveCalls(false, true)); | ||
263 | |||
264 | $this->em | ||
265 | ->expects($this->exactly(2)) | ||
266 | ->method('getRepository') | ||
267 | ->willReturn($entryRepo); | ||
268 | |||
269 | $entry = new Entry($this->user); | ||
270 | |||
271 | $this->contentProxy | ||
272 | ->expects($this->once()) | ||
273 | ->method('updateEntry') | ||
274 | ->willReturn($entry); | ||
275 | |||
276 | $pocketImport->setClient($client); | ||
277 | $pocketImport->authorize('wunderbar_code'); | ||
278 | |||
279 | $res = $pocketImport->import(); | ||
280 | |||
281 | $this->assertTrue($res); | ||
282 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
287 | */ | ||
288 | public function testImportAndMarkAllAsRead() | ||
289 | { | ||
290 | $client = new Client(); | ||
291 | |||
292 | $mock = new Mock([ | ||
293 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
294 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
295 | { | ||
296 | "status": 1, | ||
297 | "list": { | ||
298 | "229279689": { | ||
299 | "item_id": "229279689", | ||
300 | "resolved_id": "229279689", | ||
301 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
302 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
303 | "favorite": "1", | ||
304 | "status": "1", | ||
305 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
306 | "is_article": "1", | ||
307 | "has_video": "1", | ||
308 | "has_image": "1", | ||
309 | "word_count": "3197" | ||
310 | }, | ||
311 | "229279690": { | ||
312 | "item_id": "229279689", | ||
313 | "resolved_id": "229279689", | ||
314 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2", | ||
315 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
316 | "favorite": "1", | ||
317 | "status": "0", | ||
318 | "excerpt": "The list of things I love about the Ryder Cup is so long that it could fill a (tedious) novel, and golf fans can probably guess most of them.", | ||
319 | "is_article": "1", | ||
320 | "has_video": "0", | ||
321 | "has_image": "0", | ||
322 | "word_count": "3197" | ||
323 | } | ||
324 | } | ||
325 | } | ||
326 | ')), | ||
327 | ]); | ||
328 | |||
329 | $client->getEmitter()->attach($mock); | ||
330 | |||
331 | $pocketImport = $this->getPocketImport(); | ||
332 | |||
333 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
334 | ->disableOriginalConstructor() | ||
335 | ->getMock(); | ||
336 | |||
337 | $entryRepo->expects($this->exactly(2)) | ||
338 | ->method('findByUrlAndUserId') | ||
339 | ->will($this->onConsecutiveCalls(false, false)); | ||
340 | |||
341 | $this->em | ||
342 | ->expects($this->exactly(2)) | ||
343 | ->method('getRepository') | ||
344 | ->willReturn($entryRepo); | ||
345 | |||
346 | // check that every entry persisted are archived | ||
347 | $this->em | ||
348 | ->expects($this->any()) | ||
349 | ->method('persist') | ||
350 | ->with($this->callback(function ($persistedEntry) { | ||
351 | return $persistedEntry->isArchived(); | ||
352 | })); | ||
353 | |||
354 | $entry = new Entry($this->user); | ||
355 | |||
356 | $this->contentProxy | ||
357 | ->expects($this->exactly(2)) | ||
358 | ->method('updateEntry') | ||
359 | ->willReturn($entry); | ||
360 | |||
361 | $pocketImport->setClient($client); | ||
362 | $pocketImport->authorize('wunderbar_code'); | ||
363 | |||
364 | $res = $pocketImport->setMarkAsRead(true)->import(); | ||
365 | |||
366 | $this->assertTrue($res); | ||
367 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary()); | ||
368 | } | ||
369 | |||
370 | public function testImportBadResponse() | ||
371 | { | ||
372 | $client = new Client(); | ||
373 | |||
374 | $mock = new Mock([ | ||
375 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
376 | new Response(403), | ||
377 | ]); | ||
378 | |||
379 | $client->getEmitter()->attach($mock); | ||
380 | |||
381 | $pocketImport = $this->getPocketImport(); | ||
382 | $pocketImport->setClient($client); | ||
383 | $pocketImport->authorize('wunderbar_code'); | ||
384 | |||
385 | $res = $pocketImport->import(); | ||
386 | |||
387 | $this->assertFalse($res); | ||
388 | |||
389 | $records = $this->logHandler->getRecords(); | ||
390 | $this->assertContains('PocketImport: Failed to import', $records[0]['message']); | ||
391 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
392 | } | ||
393 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php new file mode 100644 index 00000000..bdc47dac --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/WallabagV1ImportTest.php | |||
@@ -0,0 +1,150 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\WallabagV1Import; | ||
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Monolog\Logger; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | |||
11 | class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | protected $user; | ||
14 | protected $em; | ||
15 | protected $logHandler; | ||
16 | protected $contentProxy; | ||
17 | |||
18 | private function getWallabagV1Import($unsetUser = false) | ||
19 | { | ||
20 | $this->user = new User(); | ||
21 | |||
22 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
23 | ->disableOriginalConstructor() | ||
24 | ->getMock(); | ||
25 | |||
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
27 | ->disableOriginalConstructor() | ||
28 | ->getMock(); | ||
29 | |||
30 | $wallabag = new WallabagV1Import($this->em, $this->contentProxy); | ||
31 | |||
32 | $this->logHandler = new TestHandler(); | ||
33 | $logger = new Logger('test', [$this->logHandler]); | ||
34 | $wallabag->setLogger($logger); | ||
35 | |||
36 | if (false === $unsetUser) { | ||
37 | $wallabag->setUser($this->user); | ||
38 | } | ||
39 | |||
40 | return $wallabag; | ||
41 | } | ||
42 | |||
43 | public function testInit() | ||
44 | { | ||
45 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
46 | |||
47 | $this->assertEquals('wallabag v1', $wallabagV1Import->getName()); | ||
48 | $this->assertNotEmpty($wallabagV1Import->getUrl()); | ||
49 | $this->assertEquals('import.wallabag_v1.description', $wallabagV1Import->getDescription()); | ||
50 | } | ||
51 | |||
52 | public function testImport() | ||
53 | { | ||
54 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
55 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
56 | |||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
58 | ->disableOriginalConstructor() | ||
59 | ->getMock(); | ||
60 | |||
61 | $entryRepo->expects($this->exactly(4)) | ||
62 | ->method('findByUrlAndUserId') | ||
63 | ->will($this->onConsecutiveCalls(false, true, false, false)); | ||
64 | |||
65 | $this->em | ||
66 | ->expects($this->any()) | ||
67 | ->method('getRepository') | ||
68 | ->willReturn($entryRepo); | ||
69 | |||
70 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
71 | ->disableOriginalConstructor() | ||
72 | ->getMock(); | ||
73 | |||
74 | $this->contentProxy | ||
75 | ->expects($this->exactly(3)) | ||
76 | ->method('updateEntry') | ||
77 | ->willReturn($entry); | ||
78 | |||
79 | $res = $wallabagV1Import->import(); | ||
80 | |||
81 | $this->assertTrue($res); | ||
82 | $this->assertEquals(['skipped' => 1, 'imported' => 3], $wallabagV1Import->getSummary()); | ||
83 | } | ||
84 | |||
85 | public function testImportAndMarkAllAsRead() | ||
86 | { | ||
87 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
88 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1-read.json'); | ||
89 | |||
90 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
91 | ->disableOriginalConstructor() | ||
92 | ->getMock(); | ||
93 | |||
94 | $entryRepo->expects($this->exactly(3)) | ||
95 | ->method('findByUrlAndUserId') | ||
96 | ->will($this->onConsecutiveCalls(false, false, false)); | ||
97 | |||
98 | $this->em | ||
99 | ->expects($this->any()) | ||
100 | ->method('getRepository') | ||
101 | ->willReturn($entryRepo); | ||
102 | |||
103 | $this->contentProxy | ||
104 | ->expects($this->exactly(3)) | ||
105 | ->method('updateEntry') | ||
106 | ->willReturn(new Entry($this->user)); | ||
107 | |||
108 | // check that every entry persisted are archived | ||
109 | $this->em | ||
110 | ->expects($this->any()) | ||
111 | ->method('persist') | ||
112 | ->with($this->callback(function ($persistedEntry) { | ||
113 | return $persistedEntry->isArchived(); | ||
114 | })); | ||
115 | |||
116 | $res = $wallabagV1Import->setMarkAsRead(true)->import(); | ||
117 | |||
118 | $this->assertTrue($res); | ||
119 | |||
120 | $this->assertEquals(['skipped' => 0, 'imported' => 3], $wallabagV1Import->getSummary()); | ||
121 | } | ||
122 | |||
123 | public function testImportBadFile() | ||
124 | { | ||
125 | $wallabagV1Import = $this->getWallabagV1Import(); | ||
126 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx'); | ||
127 | |||
128 | $res = $wallabagV1Import->import(); | ||
129 | |||
130 | $this->assertFalse($res); | ||
131 | |||
132 | $records = $this->logHandler->getRecords(); | ||
133 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); | ||
134 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
135 | } | ||
136 | |||
137 | public function testImportUserNotDefined() | ||
138 | { | ||
139 | $wallabagV1Import = $this->getWallabagV1Import(true); | ||
140 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json'); | ||
141 | |||
142 | $res = $wallabagV1Import->import(); | ||
143 | |||
144 | $this->assertFalse($res); | ||
145 | |||
146 | $records = $this->logHandler->getRecords(); | ||
147 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); | ||
148 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
149 | } | ||
150 | } | ||
diff --git a/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php new file mode 100644 index 00000000..8ec66b12 --- /dev/null +++ b/tests/Wallabag/ImportBundle/Import/WallabagV2ImportTest.php | |||
@@ -0,0 +1,146 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Tests\Wallabag\ImportBundle\Import; | ||
4 | |||
5 | use Wallabag\ImportBundle\Import\WallabagV2Import; | ||
6 | use Wallabag\UserBundle\Entity\User; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Monolog\Logger; | ||
9 | use Monolog\Handler\TestHandler; | ||
10 | |||
11 | class WallabagV2ImportTest extends \PHPUnit_Framework_TestCase | ||
12 | { | ||
13 | protected $user; | ||
14 | protected $em; | ||
15 | protected $logHandler; | ||
16 | protected $contentProxy; | ||
17 | |||
18 | private function getWallabagV2Import($unsetUser = false) | ||
19 | { | ||
20 | $this->user = new User(); | ||
21 | |||
22 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | ||
23 | ->disableOriginalConstructor() | ||
24 | ->getMock(); | ||
25 | |||
26 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
27 | ->disableOriginalConstructor() | ||
28 | ->getMock(); | ||
29 | |||
30 | $wallabag = new WallabagV2Import($this->em, $this->contentProxy); | ||
31 | |||
32 | $this->logHandler = new TestHandler(); | ||
33 | $logger = new Logger('test', [$this->logHandler]); | ||
34 | $wallabag->setLogger($logger); | ||
35 | |||
36 | if (false === $unsetUser) { | ||
37 | $wallabag->setUser($this->user); | ||
38 | } | ||
39 | |||
40 | return $wallabag; | ||
41 | } | ||
42 | |||
43 | public function testInit() | ||
44 | { | ||
45 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
46 | |||
47 | $this->assertEquals('wallabag v2', $wallabagV2Import->getName()); | ||
48 | $this->assertNotEmpty($wallabagV2Import->getUrl()); | ||
49 | $this->assertEquals('import.wallabag_v2.description', $wallabagV2Import->getDescription()); | ||
50 | } | ||
51 | |||
52 | public function testImport() | ||
53 | { | ||
54 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
55 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
56 | |||
57 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
58 | ->disableOriginalConstructor() | ||
59 | ->getMock(); | ||
60 | |||
61 | $entryRepo->expects($this->exactly(24)) | ||
62 | ->method('findByUrlAndUserId') | ||
63 | ->will($this->onConsecutiveCalls(false, true, false)); | ||
64 | |||
65 | $this->em | ||
66 | ->expects($this->any()) | ||
67 | ->method('getRepository') | ||
68 | ->willReturn($entryRepo); | ||
69 | |||
70 | $this->contentProxy | ||
71 | ->expects($this->exactly(2)) | ||
72 | ->method('updateEntry') | ||
73 | ->willReturn(new Entry($this->user)); | ||
74 | |||
75 | $res = $wallabagV2Import->import(); | ||
76 | |||
77 | $this->assertTrue($res); | ||
78 | $this->assertEquals(['skipped' => 22, 'imported' => 2], $wallabagV2Import->getSummary()); | ||
79 | } | ||
80 | |||
81 | public function testImportAndMarkAllAsRead() | ||
82 | { | ||
83 | $wallabagV2Import = $this->getWallabagV2Import(); | ||
84 | $wallabagV2Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2-read.json'); | ||
85 | |||
86 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
87 | ->disableOriginalConstructor() | ||
88 | ->getMock(); | ||
89 | |||
90 | $entryRepo->expects($this->exactly(2)) | ||
91 | ->method('findByUrlAndUserId') | ||
92 | ->will($this->onConsecutiveCalls(false, false)); | ||
93 | |||
94 | $this->em | ||
95 | ->expects($this->any()) | ||
96 | ->method('getRepository') | ||
97 | ->willReturn($entryRepo); | ||
98 | |||
99 | $this->contentProxy | ||
100 | ->expects($this->exactly(2)) | ||
101 | ->method('updateEntry') | ||
102 | ->willReturn(new Entry($this->user)); | ||
103 | |||
104 | // check that every entry persisted are archived | ||
105 | $this->em | ||
106 | ->expects($this->any()) | ||
107 | ->method('persist') | ||
108 | ->with($this->callback(function ($persistedEntry) { | ||
109 | return $persistedEntry->isArchived(); | ||
110 | })); | ||
111 | |||
112 | $res = $wallabagV2Import->setMarkAsRead(true)->import(); | ||
113 | |||
114 | $this->assertTrue($res); | ||
115 | |||
116 | $this->assertEquals(['skipped' => 0, 'imported' => 2], $wallabagV2Import->getSummary()); | ||
117 | } | ||
118 | |||
119 | public function testImportBadFile() | ||
120 | { | ||
121 | $wallabagV1Import = $this->getWallabagV2Import(); | ||
122 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.jsonx'); | ||
123 | |||
124 | $res = $wallabagV1Import->import(); | ||
125 | |||
126 | $this->assertFalse($res); | ||
127 | |||
128 | $records = $this->logHandler->getRecords(); | ||
129 | $this->assertContains('WallabagImport: unable to read file', $records[0]['message']); | ||
130 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
131 | } | ||
132 | |||
133 | public function testImportUserNotDefined() | ||
134 | { | ||
135 | $wallabagV1Import = $this->getWallabagV2Import(true); | ||
136 | $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v2.json'); | ||
137 | |||
138 | $res = $wallabagV1Import->import(); | ||
139 | |||
140 | $this->assertFalse($res); | ||
141 | |||
142 | $records = $this->logHandler->getRecords(); | ||
143 | $this->assertContains('WallabagImport: user is not defined', $records[0]['message']); | ||
144 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
145 | } | ||
146 | } | ||