]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ImportBundle/Import/PocketImportTest.php
Send every imported item to the queue
[github/wallabag/wallabag.git] / tests / Wallabag / ImportBundle / Import / PocketImportTest.php
CommitLineData
7ec2897e
JB
1<?php
2
23634d5d 3namespace Tests\Wallabag\ImportBundle\Import;
7ec2897e
JB
4
5use Wallabag\UserBundle\Entity\User;
79d0e38e 6use Wallabag\CoreBundle\Entity\Entry;
7ec2897e 7use Wallabag\ImportBundle\Import\PocketImport;
7ec2897e
JB
8use GuzzleHttp\Client;
9use GuzzleHttp\Subscriber\Mock;
10use GuzzleHttp\Message\Response;
11use GuzzleHttp\Stream\Stream;
252ebd60
JB
12use Monolog\Logger;
13use Monolog\Handler\TestHandler;
14
15class PocketImportMock extends PocketImport
16{
17 public function getAccessToken()
18 {
19 return $this->accessToken;
20 }
21}
7ec2897e
JB
22
23class PocketImportTest extends \PHPUnit_Framework_TestCase
24{
25 protected $token;
26 protected $user;
7ec2897e 27 protected $em;
252ebd60
JB
28 protected $contentProxy;
29 protected $logHandler;
7ec2897e 30
ef75e122 31 private function getPocketImport($consumerKey = 'ConsumerKey')
7ec2897e
JB
32 {
33 $this->user = new User();
34
252ebd60
JB
35 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
36 ->disableOriginalConstructor()
37 ->getMock();
38
7ec2897e
JB
39 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
40 ->disableOriginalConstructor()
41 ->getMock();
42
63e40f2d
JB
43 $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config')
44 ->disableOriginalConstructor()
45 ->getMock();
46
47 $config->expects($this->any())
48 ->method('get')
49 ->with('pocket_consumer_key')
50 ->willReturn($consumerKey);
51
252ebd60 52 $pocket = new PocketImportMock(
7ec2897e 53 $this->em,
252ebd60 54 $this->contentProxy,
ef75e122 55 $config
7ec2897e 56 );
ef75e122 57 $pocket->setUser($this->user);
252ebd60
JB
58
59 $this->logHandler = new TestHandler();
4094ea47 60 $logger = new Logger('test', [$this->logHandler]);
252ebd60
JB
61 $pocket->setLogger($logger);
62
63 return $pocket;
7ec2897e
JB
64 }
65
66 public function testInit()
67 {
68 $pocketImport = $this->getPocketImport();
69
70 $this->assertEquals('Pocket', $pocketImport->getName());
7019c7cf 71 $this->assertNotEmpty($pocketImport->getUrl());
0d42217e 72 $this->assertEquals('import.pocket.description', $pocketImport->getDescription());
7ec2897e
JB
73 }
74
75 public function testOAuthRequest()
76 {
77 $client = new Client();
78
79 $mock = new Mock([
252ebd60 80 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))),
7ec2897e
JB
81 ]);
82
83 $client->getEmitter()->attach($mock);
84
85 $pocketImport = $this->getPocketImport();
86 $pocketImport->setClient($client);
87
252ebd60 88 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
7ec2897e 89
252ebd60
JB
90 $this->assertEquals('wunderbar_code', $code);
91 }
92
93 public function testOAuthRequestBadResponse()
94 {
95 $client = new Client();
96
97 $mock = new Mock([
98 new Response(403),
99 ]);
100
101 $client->getEmitter()->attach($mock);
102
103 $pocketImport = $this->getPocketImport();
104 $pocketImport->setClient($client);
105
106 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
107
108 $this->assertFalse($code);
109
110 $records = $this->logHandler->getRecords();
111 $this->assertContains('PocketImport: Failed to request token', $records[0]['message']);
112 $this->assertEquals('ERROR', $records[0]['level_name']);
7ec2897e
JB
113 }
114
115 public function testOAuthAuthorize()
116 {
117 $client = new Client();
118
119 $mock = new Mock([
252ebd60 120 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
7ec2897e
JB
121 ]);
122
123 $client->getEmitter()->attach($mock);
124
125 $pocketImport = $this->getPocketImport();
126 $pocketImport->setClient($client);
127
252ebd60 128 $res = $pocketImport->authorize('wunderbar_code');
7ec2897e 129
252ebd60
JB
130 $this->assertTrue($res);
131 $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken());
7ec2897e
JB
132 }
133
252ebd60
JB
134 public function testOAuthAuthorizeBadResponse()
135 {
136 $client = new Client();
137
138 $mock = new Mock([
139 new Response(403),
140 ]);
141
142 $client->getEmitter()->attach($mock);
143
144 $pocketImport = $this->getPocketImport();
145 $pocketImport->setClient($client);
146
147 $res = $pocketImport->authorize('wunderbar_code');
148
149 $this->assertFalse($res);
150
151 $records = $this->logHandler->getRecords();
152 $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']);
153 $this->assertEquals('ERROR', $records[0]['level_name']);
154 }
155
156 /**
157 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
158 */
7ec2897e
JB
159 public function testImport()
160 {
161 $client = new Client();
162
163 $mock = new Mock([
252ebd60
JB
164 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
165 new Response(200, ['Content-Type' => 'application/json'], Stream::factory('
166 {
167 "status": 1,
168 "list": {
169 "229279689": {
170 "item_id": "229279689",
171 "resolved_id": "229279689",
172 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
173 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
174 "favorite": "1",
175 "status": "1",
176 "resolved_title": "The Massive Ryder Cup Preview",
177 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
178 "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.",
179 "is_article": "1",
180 "has_video": "1",
181 "has_image": "1",
182 "word_count": "3197",
183 "images": {
184 "1": {
185 "item_id": "229279689",
186 "image_id": "1",
187 "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
188 "width": "0",
189 "height": "0",
190 "credit": "Jamie Squire/Getty Images",
191 "caption": ""
192 }
193 },
194 "videos": {
195 "1": {
196 "item_id": "229279689",
197 "video_id": "1",
198 "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
199 "width": "420",
200 "height": "315",
201 "type": "1",
202 "vid": "Er34PbFkVGk"
203 }
204 },
205 "tags": {
206 "grantland": {
207 "item_id": "1147652870",
208 "tag": "grantland"
209 },
210 "Ryder Cup": {
211 "item_id": "1147652870",
212 "tag": "Ryder Cup"
213 }
214 }
215 },
216 "229279690": {
217 "item_id": "229279689",
218 "resolved_id": "229279689",
219 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
220 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
221 "favorite": "1",
222 "status": "1",
223 "resolved_title": "The Massive Ryder Cup Preview",
224 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
225 "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.",
226 "is_article": "1",
227 "has_video": "0",
228 "has_image": "0",
229 "word_count": "3197"
230 }
231 }
232 }
233 ')),
234 ]);
235
236 $client->getEmitter()->attach($mock);
237
238 $pocketImport = $this->getPocketImport();
239
240 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
241 ->disableOriginalConstructor()
242 ->getMock();
243
244 $entryRepo->expects($this->exactly(2))
78833672 245 ->method('findByUrlAndUserId')
252ebd60
JB
246 ->will($this->onConsecutiveCalls(false, true));
247
252ebd60 248 $this->em
c2656f96 249 ->expects($this->exactly(2))
252ebd60 250 ->method('getRepository')
c2656f96 251 ->willReturn($entryRepo);
252ebd60 252
79d0e38e 253 $entry = new Entry($this->user);
252ebd60
JB
254
255 $this->contentProxy
256 ->expects($this->once())
257 ->method('updateEntry')
258 ->willReturn($entry);
259
260 $pocketImport->setClient($client);
261 $pocketImport->authorize('wunderbar_code');
262
263 $res = $pocketImport->import();
264
265 $this->assertTrue($res);
266 $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary());
267 }
268
79d0e38e
JB
269 /**
270 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
271 */
272 public function testImportAndMarkAllAsRead()
273 {
274 $client = new Client();
275
276 $mock = new Mock([
277 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
278 new Response(200, ['Content-Type' => 'application/json'], Stream::factory('
279 {
280 "status": 1,
281 "list": {
282 "229279689": {
283 "item_id": "229279689",
284 "resolved_id": "229279689",
285 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
286 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
287 "favorite": "1",
288 "status": "1",
79d0e38e
JB
289 "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.",
290 "is_article": "1",
291 "has_video": "1",
292 "has_image": "1",
293 "word_count": "3197"
294 },
295 "229279690": {
296 "item_id": "229279689",
297 "resolved_id": "229279689",
298 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2",
299 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
300 "favorite": "1",
301 "status": "0",
79d0e38e
JB
302 "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.",
303 "is_article": "1",
304 "has_video": "0",
305 "has_image": "0",
306 "word_count": "3197"
307 }
308 }
309 }
310 ')),
311 ]);
312
313 $client->getEmitter()->attach($mock);
314
315 $pocketImport = $this->getPocketImport();
316
317 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
318 ->disableOriginalConstructor()
319 ->getMock();
320
321 $entryRepo->expects($this->exactly(2))
322 ->method('findByUrlAndUserId')
323 ->will($this->onConsecutiveCalls(false, false));
324
325 $this->em
326 ->expects($this->exactly(2))
327 ->method('getRepository')
328 ->willReturn($entryRepo);
329
330 // check that every entry persisted are archived
331 $this->em
332 ->expects($this->any())
333 ->method('persist')
cebb4223 334 ->with($this->callback(function ($persistedEntry) {
79d0e38e
JB
335 return $persistedEntry->isArchived();
336 }));
337
338 $entry = new Entry($this->user);
339
340 $this->contentProxy
341 ->expects($this->exactly(2))
342 ->method('updateEntry')
343 ->willReturn($entry);
344
345 $pocketImport->setClient($client);
346 $pocketImport->authorize('wunderbar_code');
347
348 $res = $pocketImport->setMarkAsRead(true)->import();
349
350 $this->assertTrue($res);
351 $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary());
352 }
353
252ebd60
JB
354 public function testImportBadResponse()
355 {
356 $client = new Client();
357
358 $mock = new Mock([
359 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
360 new Response(403),
7ec2897e
JB
361 ]);
362
363 $client->getEmitter()->attach($mock);
364
365 $pocketImport = $this->getPocketImport();
366 $pocketImport->setClient($client);
252ebd60
JB
367 $pocketImport->authorize('wunderbar_code');
368
369 $res = $pocketImport->import();
7ec2897e 370
252ebd60 371 $this->assertFalse($res);
7ec2897e 372
252ebd60
JB
373 $records = $this->logHandler->getRecords();
374 $this->assertContains('PocketImport: Failed to import', $records[0]['message']);
375 $this->assertEquals('ERROR', $records[0]['level_name']);
7ec2897e 376 }
19d9efab
JB
377
378 public function testImportWithExceptionFromGraby()
379 {
380 $client = new Client();
381
382 $mock = new Mock([
383 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
384 new Response(200, ['Content-Type' => 'application/json'], Stream::factory('
385 {
386 "status": 1,
387 "list": {
388 "229279689": {
389 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview"
390 }
391 }
392 }
393 ')),
394 ]);
395
396 $client->getEmitter()->attach($mock);
397
398 $pocketImport = $this->getPocketImport();
399
400 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
401 ->disableOriginalConstructor()
402 ->getMock();
403
404 $entryRepo->expects($this->once())
405 ->method('findByUrlAndUserId')
406 ->will($this->onConsecutiveCalls(false, true));
407
408 $this->em
409 ->expects($this->once())
410 ->method('getRepository')
411 ->willReturn($entryRepo);
412
413 $entry = new Entry($this->user);
414
415 $this->contentProxy
416 ->expects($this->once())
417 ->method('updateEntry')
418 ->will($this->throwException(new \Exception()));
419
420 $pocketImport->setClient($client);
421 $pocketImport->authorize('wunderbar_code');
422
423 $res = $pocketImport->import();
424
425 $this->assertTrue($res);
426 $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary());
427 }
7ec2897e 428}