3 namespace Tests\Wallabag\ImportBundle\Import
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\CoreBundle\Entity\Entry
;
7 use Wallabag\ImportBundle\Import\PocketImport
;
9 use GuzzleHttp\Subscriber\Mock
;
10 use GuzzleHttp\Message\Response
;
11 use GuzzleHttp\Stream\Stream
;
13 use Monolog\Handler\TestHandler
;
15 class PocketImportTest
extends \PHPUnit_Framework_TestCase
20 protected $contentProxy;
21 protected $logHandler;
23 private function getPocketImport($consumerKey = 'ConsumerKey')
25 $this->user
= new User();
27 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
28 ->disableOriginalConstructor()
31 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
32 ->disableOriginalConstructor()
35 $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config')
36 ->disableOriginalConstructor()
39 $config->expects($this->any())
41 ->with('pocket_consumer_key')
42 ->willReturn($consumerKey);
44 $pocket = new PocketImport(
49 $pocket->setUser($this->user
);
51 $this->logHandler
= new TestHandler();
52 $logger = new Logger('test', [$this->logHandler
]);
53 $pocket->setLogger($logger);
58 public function testInit()
60 $pocketImport = $this->getPocketImport();
62 $this->assertEquals('Pocket', $pocketImport->getName());
63 $this->assertNotEmpty($pocketImport->getUrl());
64 $this->assertEquals('import.pocket.description', $pocketImport->getDescription());
67 public function testOAuthRequest()
69 $client = new Client();
72 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['code' => 'wunderbar_code']))),
75 $client->getEmitter()->attach($mock);
77 $pocketImport = $this->getPocketImport();
78 $pocketImport->setClient($client);
80 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
82 $this->assertEquals('wunderbar_code', $code);
85 public function testOAuthRequestBadResponse()
87 $client = new Client();
93 $client->getEmitter()->attach($mock);
95 $pocketImport = $this->getPocketImport();
96 $pocketImport->setClient($client);
98 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
100 $this->assertFalse($code);
102 $records = $this->logHandler
->getRecords();
103 $this->assertContains('PocketImport: Failed to request token', $records[0]['message']);
104 $this->assertEquals('ERROR', $records[0]['level_name']);
107 public function testOAuthAuthorize()
109 $client = new Client();
112 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
115 $client->getEmitter()->attach($mock);
117 $pocketImport = $this->getPocketImport();
118 $pocketImport->setClient($client);
120 $res = $pocketImport->authorize('wunderbar_code');
122 $this->assertTrue($res);
123 $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken());
126 public function testOAuthAuthorizeBadResponse()
128 $client = new Client();
134 $client->getEmitter()->attach($mock);
136 $pocketImport = $this->getPocketImport();
137 $pocketImport->setClient($client);
139 $res = $pocketImport->authorize('wunderbar_code');
141 $this->assertFalse($res);
143 $records = $this->logHandler
->getRecords();
144 $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']);
145 $this->assertEquals('ERROR', $records[0]['level_name']);
149 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
151 public function testImport()
153 $client = new Client();
156 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
157 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory('
162 "item_id": "229279689",
163 "resolved_id": "229279689",
164 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
165 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
168 "resolved_title": "The Massive Ryder Cup Preview",
169 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
170 "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.",
174 "word_count": "3197",
177 "item_id": "229279689",
179 "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
182 "credit": "Jamie Squire/Getty Images",
188 "item_id": "229279689",
190 "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
199 "item_id": "1147652870",
203 "item_id": "1147652870",
209 "item_id": "229279689",
210 "resolved_id": "229279689",
211 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
212 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
215 "resolved_title": "The Massive Ryder Cup Preview",
216 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
217 "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.",
228 $client->getEmitter()->attach($mock);
230 $pocketImport = $this->getPocketImport();
232 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
233 ->disableOriginalConstructor()
236 $entryRepo->expects($this->exactly(2))
237 ->method('findByUrlAndUserId')
238 ->will($this->onConsecutiveCalls(false, true));
241 ->expects($this->exactly(2))
242 ->method('getRepository')
243 ->willReturn($entryRepo);
245 $entry = new Entry($this->user
);
248 ->expects($this->once())
249 ->method('updateEntry')
250 ->willReturn($entry);
252 $pocketImport->setClient($client);
253 $pocketImport->authorize('wunderbar_code');
255 $res = $pocketImport->import();
257 $this->assertTrue($res);
258 $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary());
262 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
264 public function testImportAndMarkAllAsRead()
266 $client = new Client();
269 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
270 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory('
275 "item_id": "229279689",
276 "resolved_id": "229279689",
277 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
278 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
281 "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.",
288 "item_id": "229279689",
289 "resolved_id": "229279689",
290 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview/2",
291 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
294 "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.",
305 $client->getEmitter()->attach($mock);
307 $pocketImport = $this->getPocketImport();
309 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
310 ->disableOriginalConstructor()
313 $entryRepo->expects($this->exactly(2))
314 ->method('findByUrlAndUserId')
315 ->will($this->onConsecutiveCalls(false, false));
318 ->expects($this->exactly(2))
319 ->method('getRepository')
320 ->willReturn($entryRepo);
322 // check that every entry persisted are archived
324 ->expects($this->any())
326 ->with($this->callback(function ($persistedEntry) {
327 return $persistedEntry->isArchived();
330 $entry = new Entry($this->user
);
333 ->expects($this->exactly(2))
334 ->method('updateEntry')
335 ->willReturn($entry);
337 $pocketImport->setClient($client);
338 $pocketImport->authorize('wunderbar_code');
340 $res = $pocketImport->setMarkAsRead(true)->import();
342 $this->assertTrue($res);
343 $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary());
346 public function testImportBadResponse()
348 $client = new Client();
351 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
355 $client->getEmitter()->attach($mock);
357 $pocketImport = $this->getPocketImport();
358 $pocketImport->setClient($client);
359 $pocketImport->authorize('wunderbar_code');
361 $res = $pocketImport->import();
363 $this->assertFalse($res);
365 $records = $this->logHandler
->getRecords();
366 $this->assertContains('PocketImport: Failed to import', $records[0]['message']);
367 $this->assertEquals('ERROR', $records[0]['level_name']);
370 public function testImportWithExceptionFromGraby()
372 $client = new Client();
375 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
376 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory('
381 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview"
388 $client->getEmitter()->attach($mock);
390 $pocketImport = $this->getPocketImport();
392 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
393 ->disableOriginalConstructor()
396 $entryRepo->expects($this->once())
397 ->method('findByUrlAndUserId')
398 ->will($this->onConsecutiveCalls(false, true));
401 ->expects($this->once())
402 ->method('getRepository')
403 ->willReturn($entryRepo);
405 $entry = new Entry($this->user
);
408 ->expects($this->once())
409 ->method('updateEntry')
410 ->will($this->throwException(new \
Exception()));
412 $pocketImport->setClient($client);
413 $pocketImport->authorize('wunderbar_code');
415 $res = $pocketImport->import();
417 $this->assertTrue($res);
418 $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary());