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 PocketImportMock
extends PocketImport
17 public function getAccessToken()
19 return $this->accessToken
;
23 class PocketImportTest
extends \PHPUnit_Framework_TestCase
28 protected $contentProxy;
29 protected $logHandler;
31 private function getPocketImport($consumerKey = 'ConsumerKey')
33 $this->user
= new User();
35 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
36 ->disableOriginalConstructor()
39 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
40 ->disableOriginalConstructor()
43 $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config')
44 ->disableOriginalConstructor()
47 $config->expects($this->any())
49 ->with('pocket_consumer_key')
50 ->willReturn($consumerKey);
52 $pocket = new PocketImportMock(
57 $pocket->setUser($this->user
);
59 $this->logHandler
= new TestHandler();
60 $logger = new Logger('test', [$this->logHandler
]);
61 $pocket->setLogger($logger);
66 public function testInit()
68 $pocketImport = $this->getPocketImport();
70 $this->assertEquals('Pocket', $pocketImport->getName());
71 $this->assertNotEmpty($pocketImport->getUrl());
72 $this->assertEquals('import.pocket.description', $pocketImport->getDescription());
75 public function testOAuthRequest()
77 $client = new Client();
80 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['code' => 'wunderbar_code']))),
83 $client->getEmitter()->attach($mock);
85 $pocketImport = $this->getPocketImport();
86 $pocketImport->setClient($client);
88 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
90 $this->assertEquals('wunderbar_code', $code);
93 public function testOAuthRequestBadResponse()
95 $client = new Client();
101 $client->getEmitter()->attach($mock);
103 $pocketImport = $this->getPocketImport();
104 $pocketImport->setClient($client);
106 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
108 $this->assertFalse($code);
110 $records = $this->logHandler
->getRecords();
111 $this->assertContains('PocketImport: Failed to request token', $records[0]['message']);
112 $this->assertEquals('ERROR', $records[0]['level_name']);
115 public function testOAuthAuthorize()
117 $client = new Client();
120 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
123 $client->getEmitter()->attach($mock);
125 $pocketImport = $this->getPocketImport();
126 $pocketImport->setClient($client);
128 $res = $pocketImport->authorize('wunderbar_code');
130 $this->assertTrue($res);
131 $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken());
134 public function testOAuthAuthorizeBadResponse()
136 $client = new Client();
142 $client->getEmitter()->attach($mock);
144 $pocketImport = $this->getPocketImport();
145 $pocketImport->setClient($client);
147 $res = $pocketImport->authorize('wunderbar_code');
149 $this->assertFalse($res);
151 $records = $this->logHandler
->getRecords();
152 $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']);
153 $this->assertEquals('ERROR', $records[0]['level_name']);
157 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
159 public function testImport()
161 $client = new Client();
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('
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",
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.",
182 "word_count": "3197",
185 "item_id": "229279689",
187 "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
190 "credit": "Jamie Squire/Getty Images",
196 "item_id": "229279689",
198 "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
207 "item_id": "1147652870",
211 "item_id": "1147652870",
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",
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.",
236 $client->getEmitter()->attach($mock);
238 $pocketImport = $this->getPocketImport();
240 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
241 ->disableOriginalConstructor()
244 $entryRepo->expects($this->exactly(2))
245 ->method('findByUrlAndUserId')
246 ->will($this->onConsecutiveCalls(false, true));
249 ->expects($this->exactly(2))
250 ->method('getRepository')
251 ->willReturn($entryRepo);
253 $entry = new Entry($this->user
);
256 ->expects($this->once())
257 ->method('updateEntry')
258 ->willReturn($entry);
260 $pocketImport->setClient($client);
261 $pocketImport->authorize('wunderbar_code');
263 $res = $pocketImport->import();
265 $this->assertTrue($res);
266 $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary());
270 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
272 public function testImportAndMarkAllAsRead()
274 $client = new Client();
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('
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",
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.",
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",
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.",
313 $client->getEmitter()->attach($mock);
315 $pocketImport = $this->getPocketImport();
317 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
318 ->disableOriginalConstructor()
321 $entryRepo->expects($this->exactly(2))
322 ->method('findByUrlAndUserId')
323 ->will($this->onConsecutiveCalls(false, false));
326 ->expects($this->exactly(2))
327 ->method('getRepository')
328 ->willReturn($entryRepo);
330 // check that every entry persisted are archived
332 ->expects($this->any())
334 ->with($this->callback(function ($persistedEntry) {
335 return $persistedEntry->isArchived();
338 $entry = new Entry($this->user
);
341 ->expects($this->exactly(2))
342 ->method('updateEntry')
343 ->willReturn($entry);
345 $pocketImport->setClient($client);
346 $pocketImport->authorize('wunderbar_code');
348 $res = $pocketImport->setMarkAsRead(true)->import();
350 $this->assertTrue($res);
351 $this->assertEquals(['skipped' => 0, 'imported' => 2], $pocketImport->getSummary());
354 public function testImportBadResponse()
356 $client = new Client();
359 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
363 $client->getEmitter()->attach($mock);
365 $pocketImport = $this->getPocketImport();
366 $pocketImport->setClient($client);
367 $pocketImport->authorize('wunderbar_code');
369 $res = $pocketImport->import();
371 $this->assertFalse($res);
373 $records = $this->logHandler
->getRecords();
374 $this->assertContains('PocketImport: Failed to import', $records[0]['message']);
375 $this->assertEquals('ERROR', $records[0]['level_name']);
378 public function testImportWithExceptionFromGraby()
380 $client = new Client();
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('
389 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview"
396 $client->getEmitter()->attach($mock);
398 $pocketImport = $this->getPocketImport();
400 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
401 ->disableOriginalConstructor()
404 $entryRepo->expects($this->once())
405 ->method('findByUrlAndUserId')
406 ->will($this->onConsecutiveCalls(false, true));
409 ->expects($this->once())
410 ->method('getRepository')
411 ->willReturn($entryRepo);
413 $entry = new Entry($this->user
);
416 ->expects($this->once())
417 ->method('updateEntry')
418 ->will($this->throwException(new \
Exception()));
420 $pocketImport->setClient($client);
421 $pocketImport->authorize('wunderbar_code');
423 $res = $pocketImport->import();
425 $this->assertTrue($res);
426 $this->assertEquals(['skipped' => 1, 'imported' => 0], $pocketImport->getSummary());