From 7ec2897ee0ad190dcb9f77032d785f2f9661b754 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 24 Dec 2015 15:24:18 +0100 Subject: First test on PocketImport Giving ability to define the Client add abitliy to easliy test the import. --- src/Wallabag/ImportBundle/Import/PocketImport.php | 151 ++++++++++----------- .../ImportBundle/Resources/config/services.yml | 11 ++ .../ImportBundle/Tests/Import/PocketImportTest.php | 117 ++++++++++++++++ 3 files changed, 202 insertions(+), 77 deletions(-) create mode 100644 src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php (limited to 'src/Wallabag') diff --git a/src/Wallabag/ImportBundle/Import/PocketImport.php b/src/Wallabag/ImportBundle/Import/PocketImport.php index 85bab0db..e5c86f07 100644 --- a/src/Wallabag/ImportBundle/Import/PocketImport.php +++ b/src/Wallabag/ImportBundle/Import/PocketImport.php @@ -44,20 +44,83 @@ class PocketImport implements ImportInterface } /** - * Create a new Client. + * {@inheritdoc} + */ + public function oAuthRequest($redirectUri, $callbackUri) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'redirect_uri' => $redirectUri, + ]), + ] + ); + + $response = $this->client->send($request); + $values = $response->json(); + + // store code in session for callback method + $this->session->set('pocketCode', $values['code']); + + return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; + } + + /** + * {@inheritdoc} + */ + public function oAuthAuthorize() + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'code' => $this->session->get('pocketCode'), + ]), + ] + ); + + $response = $this->client->send($request); + + return $response->json()['access_token']; + } + + /** + * {@inheritdoc} + */ + public function import($accessToken) + { + $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get', + [ + 'body' => json_encode([ + 'consumer_key' => $this->consumerKey, + 'access_token' => $accessToken, + 'detailType' => 'complete', + 'state' => 'all', + 'sort' => 'oldest', + ]), + ] + ); + + $response = $this->client->send($request); + $entries = $response->json(); + + $this->parsePocketEntries($entries['list']); + + $this->session->getFlashBag()->add( + 'notice', + $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' + ); + } + + /** + * Set the Guzzle client. * - * @return Client + * @param Client $client */ - private function createClient() + public function setClient(Client $client) { - return new Client([ - 'defaults' => [ - 'headers' => [ - 'content-type' => 'application/json', - 'X-Accept' => 'application/json', - ], - ], - ]); + $this->client = $client; } /** @@ -165,70 +228,4 @@ class PocketImport implements ImportInterface $this->em->flush(); } - - public function oAuthRequest($redirectUri, $callbackUri) - { - $client = $this->createClient(); - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'redirect_uri' => $redirectUri, - ]), - ] - ); - - $response = $client->send($request); - $values = $response->json(); - - // store code in session for callback method - $this->session->set('pocketCode', $values['code']); - - return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri; - } - - public function oAuthAuthorize() - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'code' => $this->session->get('pocketCode'), - ]), - ] - ); - - $response = $client->send($request); - - return $response->json()['access_token']; - } - - public function import($accessToken) - { - $client = $this->createClient(); - - $request = $client->createRequest('POST', 'https://getpocket.com/v3/get', - [ - 'body' => json_encode([ - 'consumer_key' => $this->consumerKey, - 'access_token' => $accessToken, - 'detailType' => 'complete', - 'state' => 'all', - 'sort' => 'oldest', - ]), - ] - ); - - $response = $client->send($request); - $entries = $response->json(); - - $this->parsePocketEntries($entries['list']); - - $this->session->getFlashBag()->add( - 'notice', - $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.' - ); - } } diff --git a/src/Wallabag/ImportBundle/Resources/config/services.yml b/src/Wallabag/ImportBundle/Resources/config/services.yml index d77779eb..ab516ca5 100644 --- a/src/Wallabag/ImportBundle/Resources/config/services.yml +++ b/src/Wallabag/ImportBundle/Resources/config/services.yml @@ -6,3 +6,14 @@ services: - "@session" - "@doctrine.orm.entity_manager" - %pocket_consumer_key% + calls: + - [ setClient, [ "@wallabag_import.pocket.client" ] ] + + wallabag_import.pocket.client: + class: GuzzleHttp\Client + arguments: + - + defaults: + headers: + content-type: "application/json" + X-Accept: "application/json" diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php new file mode 100644 index 00000000..4c718fa3 --- /dev/null +++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php @@ -0,0 +1,117 @@ +user = new User(); + + $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') + ->disableOriginalConstructor() + ->getMock(); + + $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface') + ->disableOriginalConstructor() + ->getMock(); + + $token->expects($this->once()) + ->method('getUser') + ->willReturn($this->user); + + $this->tokenStorage->expects($this->once()) + ->method('getToken') + ->willReturn($token); + + $this->session = new Session(new MockArraySessionStorage()); + + $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + return new PocketImport( + $this->tokenStorage, + $this->session, + $this->em, + $consumerKey + ); + } + + public function testInit() + { + $pocketImport = $this->getPocketImport(); + + $this->assertEquals('Pocket', $pocketImport->getName()); + $this->assertEquals('This importer will import all your Pocket data.', $pocketImport->getDescription()); + } + + public function testOAuthRequest() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar']))), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $url = $pocketImport->oAuthRequest('http://0.0.0.0./redirect', 'http://0.0.0.0./callback'); + + $this->assertEquals('https://getpocket.com/auth/authorize?request_token=wunderbar&redirect_uri=http://0.0.0.0./callback', $url); + $this->assertEquals('wunderbar', $this->session->get('pocketCode')); + } + + public function testOAuthAuthorize() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar']))), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $accessToken = $pocketImport->oAuthAuthorize(); + + $this->assertEquals('wunderbar', $accessToken); + } + + public function testImport() + { + $client = new Client(); + + $mock = new Mock([ + new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['list' => []]))), + ]); + + $client->getEmitter()->attach($mock); + + $pocketImport = $this->getPocketImport(); + $pocketImport->setClient($client); + + $pocketImport->import('wunderbar'); + + $this->assertEquals('0 entries imported, 0 already saved.', $this->session->getFlashBag()->get('notice')[0]); + } +} -- cgit v1.2.3