mirror of
https://github.com/wallabag/wallabag.git
synced 2024-11-26 19:11:07 +00:00
First test on PocketImport
Giving ability to define the Client add abitliy to easliy test the import.
This commit is contained in:
parent
0aa344dc24
commit
7ec2897ee0
4 changed files with 210 additions and 85 deletions
14
composer.lock
generated
14
composer.lock
generated
|
@ -4,8 +4,8 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"hash": "7f1939f54201ee0e068eddde41454261",
|
"hash": "fdba142656b2089b0e4cbddb45e2ad1f",
|
||||||
"content-hash": "9b7801951c4ea69565ee8f4914071c25",
|
"content-hash": "a233f851c52683783b6a42be707c52b1",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "behat/transliterator",
|
"name": "behat/transliterator",
|
||||||
|
@ -2044,16 +2044,16 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "hoa/stream",
|
"name": "hoa/stream",
|
||||||
"version": "0.15.08.28",
|
"version": "0.15.10.26",
|
||||||
"source": {
|
"source": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/hoaproject/Stream.git",
|
"url": "https://github.com/hoaproject/Stream.git",
|
||||||
"reference": "cbd0f4749e447f4d31001e7c898f5e794c5861cb"
|
"reference": "011ab91d942f1d7096deade4c8a10fe57d51c5b3"
|
||||||
},
|
},
|
||||||
"dist": {
|
"dist": {
|
||||||
"type": "zip",
|
"type": "zip",
|
||||||
"url": "https://api.github.com/repos/hoaproject/Stream/zipball/cbd0f4749e447f4d31001e7c898f5e794c5861cb",
|
"url": "https://api.github.com/repos/hoaproject/Stream/zipball/011ab91d942f1d7096deade4c8a10fe57d51c5b3",
|
||||||
"reference": "cbd0f4749e447f4d31001e7c898f5e794c5861cb",
|
"reference": "011ab91d942f1d7096deade4c8a10fe57d51c5b3",
|
||||||
"shasum": ""
|
"shasum": ""
|
||||||
},
|
},
|
||||||
"require": {
|
"require": {
|
||||||
|
@ -2098,7 +2098,7 @@
|
||||||
"stream",
|
"stream",
|
||||||
"wrapper"
|
"wrapper"
|
||||||
],
|
],
|
||||||
"time": "2015-08-28 07:31:43"
|
"time": "2015-10-22 06:30:43"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"name": "hoa/ustring",
|
"name": "hoa/ustring",
|
||||||
|
|
|
@ -44,20 +44,83 @@ class PocketImport implements ImportInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new Client.
|
* {@inheritdoc}
|
||||||
*
|
|
||||||
* @return Client
|
|
||||||
*/
|
*/
|
||||||
private function createClient()
|
public function oAuthRequest($redirectUri, $callbackUri)
|
||||||
{
|
{
|
||||||
return new Client([
|
$request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
|
||||||
'defaults' => [
|
[
|
||||||
'headers' => [
|
'body' => json_encode([
|
||||||
'content-type' => 'application/json',
|
'consumer_key' => $this->consumerKey,
|
||||||
'X-Accept' => 'application/json',
|
'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.
|
||||||
|
*
|
||||||
|
* @param Client $client
|
||||||
|
*/
|
||||||
|
public function setClient(Client $client)
|
||||||
|
{
|
||||||
|
$this->client = $client;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -165,70 +228,4 @@ class PocketImport implements ImportInterface
|
||||||
|
|
||||||
$this->em->flush();
|
$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.'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,3 +6,14 @@ services:
|
||||||
- "@session"
|
- "@session"
|
||||||
- "@doctrine.orm.entity_manager"
|
- "@doctrine.orm.entity_manager"
|
||||||
- %pocket_consumer_key%
|
- %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"
|
||||||
|
|
117
src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
Normal file
117
src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Wallabag\ImportBundle\Tests\Import;
|
||||||
|
|
||||||
|
use Wallabag\UserBundle\Entity\User;
|
||||||
|
use Wallabag\ImportBundle\Import\PocketImport;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Session;
|
||||||
|
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
|
||||||
|
use GuzzleHttp\Client;
|
||||||
|
use GuzzleHttp\Subscriber\Mock;
|
||||||
|
use GuzzleHttp\Message\Response;
|
||||||
|
use GuzzleHttp\Stream\Stream;
|
||||||
|
|
||||||
|
class PocketImportTest extends \PHPUnit_Framework_TestCase
|
||||||
|
{
|
||||||
|
protected $token;
|
||||||
|
protected $user;
|
||||||
|
protected $session;
|
||||||
|
protected $em;
|
||||||
|
|
||||||
|
private function getPocketImport($consumerKey = 'ConsumerKey')
|
||||||
|
{
|
||||||
|
$this->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 <a href="https://getpocket.com">Pocket</a> 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]);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue