aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2015-12-24 15:24:18 +0100
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-01-02 23:27:41 +0100
commit7ec2897ee0ad190dcb9f77032d785f2f9661b754 (patch)
tree7f91eabb9d555628d0c2a1e5da8deb6fc2a41b3c /src
parent0aa344dc247c77376fcbf2112191f9f8b3dfc846 (diff)
downloadwallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.tar.gz
wallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.tar.zst
wallabag-7ec2897ee0ad190dcb9f77032d785f2f9661b754.zip
First test on PocketImport
Giving ability to define the Client add abitliy to easliy test the import.
Diffstat (limited to 'src')
-rw-r--r--src/Wallabag/ImportBundle/Import/PocketImport.php151
-rw-r--r--src/Wallabag/ImportBundle/Resources/config/services.yml11
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php117
3 files changed, 202 insertions, 77 deletions
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
44 } 44 }
45 45
46 /** 46 /**
47 * Create a new Client. 47 * {@inheritdoc}
48 */
49 public function oAuthRequest($redirectUri, $callbackUri)
50 {
51 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
52 [
53 'body' => json_encode([
54 'consumer_key' => $this->consumerKey,
55 'redirect_uri' => $redirectUri,
56 ]),
57 ]
58 );
59
60 $response = $this->client->send($request);
61 $values = $response->json();
62
63 // store code in session for callback method
64 $this->session->set('pocketCode', $values['code']);
65
66 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function oAuthAuthorize()
73 {
74 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
75 [
76 'body' => json_encode([
77 'consumer_key' => $this->consumerKey,
78 'code' => $this->session->get('pocketCode'),
79 ]),
80 ]
81 );
82
83 $response = $this->client->send($request);
84
85 return $response->json()['access_token'];
86 }
87
88 /**
89 * {@inheritdoc}
90 */
91 public function import($accessToken)
92 {
93 $request = $this->client->createRequest('POST', 'https://getpocket.com/v3/get',
94 [
95 'body' => json_encode([
96 'consumer_key' => $this->consumerKey,
97 'access_token' => $accessToken,
98 'detailType' => 'complete',
99 'state' => 'all',
100 'sort' => 'oldest',
101 ]),
102 ]
103 );
104
105 $response = $this->client->send($request);
106 $entries = $response->json();
107
108 $this->parsePocketEntries($entries['list']);
109
110 $this->session->getFlashBag()->add(
111 'notice',
112 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
113 );
114 }
115
116 /**
117 * Set the Guzzle client.
48 * 118 *
49 * @return Client 119 * @param Client $client
50 */ 120 */
51 private function createClient() 121 public function setClient(Client $client)
52 { 122 {
53 return new Client([ 123 $this->client = $client;
54 'defaults' => [
55 'headers' => [
56 'content-type' => 'application/json',
57 'X-Accept' => 'application/json',
58 ],
59 ],
60 ]);
61 } 124 }
62 125
63 /** 126 /**
@@ -165,70 +228,4 @@ class PocketImport implements ImportInterface
165 228
166 $this->em->flush(); 229 $this->em->flush();
167 } 230 }
168
169 public function oAuthRequest($redirectUri, $callbackUri)
170 {
171 $client = $this->createClient();
172 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/request',
173 [
174 'body' => json_encode([
175 'consumer_key' => $this->consumerKey,
176 'redirect_uri' => $redirectUri,
177 ]),
178 ]
179 );
180
181 $response = $client->send($request);
182 $values = $response->json();
183
184 // store code in session for callback method
185 $this->session->set('pocketCode', $values['code']);
186
187 return 'https://getpocket.com/auth/authorize?request_token='.$values['code'].'&redirect_uri='.$callbackUri;
188 }
189
190 public function oAuthAuthorize()
191 {
192 $client = $this->createClient();
193
194 $request = $client->createRequest('POST', 'https://getpocket.com/v3/oauth/authorize',
195 [
196 'body' => json_encode([
197 'consumer_key' => $this->consumerKey,
198 'code' => $this->session->get('pocketCode'),
199 ]),
200 ]
201 );
202
203 $response = $client->send($request);
204
205 return $response->json()['access_token'];
206 }
207
208 public function import($accessToken)
209 {
210 $client = $this->createClient();
211
212 $request = $client->createRequest('POST', 'https://getpocket.com/v3/get',
213 [
214 'body' => json_encode([
215 'consumer_key' => $this->consumerKey,
216 'access_token' => $accessToken,
217 'detailType' => 'complete',
218 'state' => 'all',
219 'sort' => 'oldest',
220 ]),
221 ]
222 );
223
224 $response = $client->send($request);
225 $entries = $response->json();
226
227 $this->parsePocketEntries($entries['list']);
228
229 $this->session->getFlashBag()->add(
230 'notice',
231 $this->importedEntries.' entries imported, '.$this->skippedEntries.' already saved.'
232 );
233 }
234} 231}
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:
6 - "@session" 6 - "@session"
7 - "@doctrine.orm.entity_manager" 7 - "@doctrine.orm.entity_manager"
8 - %pocket_consumer_key% 8 - %pocket_consumer_key%
9 calls:
10 - [ setClient, [ "@wallabag_import.pocket.client" ] ]
11
12 wallabag_import.pocket.client:
13 class: GuzzleHttp\Client
14 arguments:
15 -
16 defaults:
17 headers:
18 content-type: "application/json"
19 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 @@
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\ImportBundle\Import\PocketImport;
7use Symfony\Component\HttpFoundation\Session\Session;
8use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
9use GuzzleHttp\Client;
10use GuzzleHttp\Subscriber\Mock;
11use GuzzleHttp\Message\Response;
12use GuzzleHttp\Stream\Stream;
13
14class PocketImportTest extends \PHPUnit_Framework_TestCase
15{
16 protected $token;
17 protected $user;
18 protected $session;
19 protected $em;
20
21 private function getPocketImport($consumerKey = 'ConsumerKey')
22 {
23 $this->user = new User();
24
25 $this->tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
26 ->disableOriginalConstructor()
27 ->getMock();
28
29 $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
30 ->disableOriginalConstructor()
31 ->getMock();
32
33 $token->expects($this->once())
34 ->method('getUser')
35 ->willReturn($this->user);
36
37 $this->tokenStorage->expects($this->once())
38 ->method('getToken')
39 ->willReturn($token);
40
41 $this->session = new Session(new MockArraySessionStorage());
42
43 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
44 ->disableOriginalConstructor()
45 ->getMock();
46
47 return new PocketImport(
48 $this->tokenStorage,
49 $this->session,
50 $this->em,
51 $consumerKey
52 );
53 }
54
55 public function testInit()
56 {
57 $pocketImport = $this->getPocketImport();
58
59 $this->assertEquals('Pocket', $pocketImport->getName());
60 $this->assertEquals('This importer will import all your <a href="https://getpocket.com">Pocket</a> data.', $pocketImport->getDescription());
61 }
62
63 public function testOAuthRequest()
64 {
65 $client = new Client();
66
67 $mock = new Mock([
68 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar']))),
69 ]);
70
71 $client->getEmitter()->attach($mock);
72
73 $pocketImport = $this->getPocketImport();
74 $pocketImport->setClient($client);
75
76 $url = $pocketImport->oAuthRequest('http://0.0.0.0./redirect', 'http://0.0.0.0./callback');
77
78 $this->assertEquals('https://getpocket.com/auth/authorize?request_token=wunderbar&redirect_uri=http://0.0.0.0./callback', $url);
79 $this->assertEquals('wunderbar', $this->session->get('pocketCode'));
80 }
81
82 public function testOAuthAuthorize()
83 {
84 $client = new Client();
85
86 $mock = new Mock([
87 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar']))),
88 ]);
89
90 $client->getEmitter()->attach($mock);
91
92 $pocketImport = $this->getPocketImport();
93 $pocketImport->setClient($client);
94
95 $accessToken = $pocketImport->oAuthAuthorize();
96
97 $this->assertEquals('wunderbar', $accessToken);
98 }
99
100 public function testImport()
101 {
102 $client = new Client();
103
104 $mock = new Mock([
105 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['list' => []]))),
106 ]);
107
108 $client->getEmitter()->attach($mock);
109
110 $pocketImport = $this->getPocketImport();
111 $pocketImport->setClient($client);
112
113 $pocketImport->import('wunderbar');
114
115 $this->assertEquals('0 entries imported, 0 already saved.', $this->session->getFlashBag()->get('notice')[0]);
116 }
117}