3 namespace Wallabag\ImportBundle\Tests\Import
;
5 use Wallabag\UserBundle\Entity\User
;
6 use Wallabag\ImportBundle\Import\PocketImport
;
8 use GuzzleHttp\Subscriber\Mock
;
9 use GuzzleHttp\Message\Response
;
10 use GuzzleHttp\Stream\Stream
;
12 use Monolog\Handler\TestHandler
;
14 class PocketImportMock
extends PocketImport
16 public function getAccessToken()
18 return $this->accessToken
;
22 class PocketImportTest
extends \PHPUnit_Framework_TestCase
27 protected $contentProxy;
28 protected $logHandler;
30 private function getPocketImport($consumerKey = 'ConsumerKey')
32 $this->user
= new User();
34 $this->tokenStorage
= $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
35 ->disableOriginalConstructor()
38 $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
39 ->disableOriginalConstructor()
42 $this->contentProxy
= $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
43 ->disableOriginalConstructor()
46 $token->expects($this->once())
48 ->willReturn($this->user
);
50 $this->tokenStorage
->expects($this->once())
54 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
55 ->disableOriginalConstructor()
58 $config = $this->getMockBuilder('Craue\ConfigBundle\Util\Config')
59 ->disableOriginalConstructor()
62 $config->expects($this->any())
64 ->with('pocket_consumer_key')
65 ->willReturn($consumerKey);
67 $pocket = new PocketImportMock(
74 $this->logHandler
= new TestHandler();
75 $logger = new Logger('test', array($this->logHandler
));
76 $pocket->setLogger($logger);
81 public function testInit()
83 $pocketImport = $this->getPocketImport();
85 $this->assertEquals('Pocket', $pocketImport->getName());
86 $this->assertNotEmpty($pocketImport->getUrl());
87 $this->assertContains('This importer will import all your Pocket data.', $pocketImport->getDescription());
90 public function testOAuthRequest()
92 $client = new Client();
95 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['code' => 'wunderbar_code']))),
98 $client->getEmitter()->attach($mock);
100 $pocketImport = $this->getPocketImport();
101 $pocketImport->setClient($client);
103 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
105 $this->assertEquals('wunderbar_code', $code);
108 public function testOAuthRequestBadResponse()
110 $client = new Client();
116 $client->getEmitter()->attach($mock);
118 $pocketImport = $this->getPocketImport();
119 $pocketImport->setClient($client);
121 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
123 $this->assertFalse($code);
125 $records = $this->logHandler
->getRecords();
126 $this->assertContains('PocketImport: Failed to request token', $records[0]['message']);
127 $this->assertEquals('ERROR', $records[0]['level_name']);
130 public function testOAuthAuthorize()
132 $client = new Client();
135 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
138 $client->getEmitter()->attach($mock);
140 $pocketImport = $this->getPocketImport();
141 $pocketImport->setClient($client);
143 $res = $pocketImport->authorize('wunderbar_code');
145 $this->assertTrue($res);
146 $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken());
149 public function testOAuthAuthorizeBadResponse()
151 $client = new Client();
157 $client->getEmitter()->attach($mock);
159 $pocketImport = $this->getPocketImport();
160 $pocketImport->setClient($client);
162 $res = $pocketImport->authorize('wunderbar_code');
164 $this->assertFalse($res);
166 $records = $this->logHandler
->getRecords();
167 $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']);
168 $this->assertEquals('ERROR', $records[0]['level_name']);
172 * Will sample results from https://getpocket.com/developer/docs/v3/retrieve.
174 public function testImport()
176 $client = new Client();
179 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
180 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory('
185 "item_id": "229279689",
186 "resolved_id": "229279689",
187 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
188 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
191 "resolved_title": "The Massive Ryder Cup Preview",
192 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
193 "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.",
197 "word_count": "3197",
200 "item_id": "229279689",
202 "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360",
205 "credit": "Jamie Squire/Getty Images",
211 "item_id": "229279689",
213 "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0",
222 "item_id": "1147652870",
226 "item_id": "1147652870",
232 "item_id": "229279689",
233 "resolved_id": "229279689",
234 "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
235 "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland",
238 "resolved_title": "The Massive Ryder Cup Preview",
239 "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview",
240 "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.",
251 $client->getEmitter()->attach($mock);
253 $pocketImport = $this->getPocketImport();
255 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
256 ->disableOriginalConstructor()
259 $entryRepo->expects($this->exactly(2))
260 ->method('findByUrlAndUserId')
261 ->will($this->onConsecutiveCalls(false, true));
263 $tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag')
264 ->disableOriginalConstructor()
267 $tagRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository')
268 ->disableOriginalConstructor()
271 $tagRepo->expects($this->exactly(2))
272 // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
273 // to magically call the `findOneBy` with ['label' => 'foo']
275 ->will($this->onConsecutiveCalls(false, $tag));
278 ->expects($this->any())
279 ->method('getRepository')
280 ->will($this->onConsecutiveCalls($entryRepo, $tagRepo, $tagRepo, $entryRepo));
282 $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry')
283 ->disableOriginalConstructor()
287 ->expects($this->once())
288 ->method('updateEntry')
289 ->willReturn($entry);
291 $pocketImport->setClient($client);
292 $pocketImport->authorize('wunderbar_code');
294 $res = $pocketImport->import();
296 $this->assertTrue($res);
297 $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary());
300 public function testImportBadResponse()
302 $client = new Client();
305 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar_token']))),
309 $client->getEmitter()->attach($mock);
311 $pocketImport = $this->getPocketImport();
312 $pocketImport->setClient($client);
313 $pocketImport->authorize('wunderbar_code');
315 $res = $pocketImport->import();
317 $this->assertFalse($res);
319 $records = $this->logHandler
->getRecords();
320 $this->assertContains('PocketImport: Failed to import', $records[0]['message']);
321 $this->assertEquals('ERROR', $records[0]['level_name']);