diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Tests')
-rw-r--r-- | src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php | 228 |
1 files changed, 211 insertions, 17 deletions
diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php index 4c718fa3..cf706fa9 100644 --- a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php +++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php | |||
@@ -4,19 +4,28 @@ namespace Wallabag\ImportBundle\Tests\Import; | |||
4 | 4 | ||
5 | use Wallabag\UserBundle\Entity\User; | 5 | use Wallabag\UserBundle\Entity\User; |
6 | use Wallabag\ImportBundle\Import\PocketImport; | 6 | use Wallabag\ImportBundle\Import\PocketImport; |
7 | use Symfony\Component\HttpFoundation\Session\Session; | ||
8 | use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; | ||
9 | use GuzzleHttp\Client; | 7 | use GuzzleHttp\Client; |
10 | use GuzzleHttp\Subscriber\Mock; | 8 | use GuzzleHttp\Subscriber\Mock; |
11 | use GuzzleHttp\Message\Response; | 9 | use GuzzleHttp\Message\Response; |
12 | use GuzzleHttp\Stream\Stream; | 10 | use GuzzleHttp\Stream\Stream; |
11 | use Monolog\Logger; | ||
12 | use Monolog\Handler\TestHandler; | ||
13 | |||
14 | class PocketImportMock extends PocketImport | ||
15 | { | ||
16 | public function getAccessToken() | ||
17 | { | ||
18 | return $this->accessToken; | ||
19 | } | ||
20 | } | ||
13 | 21 | ||
14 | class PocketImportTest extends \PHPUnit_Framework_TestCase | 22 | class PocketImportTest extends \PHPUnit_Framework_TestCase |
15 | { | 23 | { |
16 | protected $token; | 24 | protected $token; |
17 | protected $user; | 25 | protected $user; |
18 | protected $session; | ||
19 | protected $em; | 26 | protected $em; |
27 | protected $contentProxy; | ||
28 | protected $logHandler; | ||
20 | 29 | ||
21 | private function getPocketImport($consumerKey = 'ConsumerKey') | 30 | private function getPocketImport($consumerKey = 'ConsumerKey') |
22 | { | 31 | { |
@@ -30,6 +39,10 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
30 | ->disableOriginalConstructor() | 39 | ->disableOriginalConstructor() |
31 | ->getMock(); | 40 | ->getMock(); |
32 | 41 | ||
42 | $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy') | ||
43 | ->disableOriginalConstructor() | ||
44 | ->getMock(); | ||
45 | |||
33 | $token->expects($this->once()) | 46 | $token->expects($this->once()) |
34 | ->method('getUser') | 47 | ->method('getUser') |
35 | ->willReturn($this->user); | 48 | ->willReturn($this->user); |
@@ -38,18 +51,22 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
38 | ->method('getToken') | 51 | ->method('getToken') |
39 | ->willReturn($token); | 52 | ->willReturn($token); |
40 | 53 | ||
41 | $this->session = new Session(new MockArraySessionStorage()); | ||
42 | |||
43 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') | 54 | $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager') |
44 | ->disableOriginalConstructor() | 55 | ->disableOriginalConstructor() |
45 | ->getMock(); | 56 | ->getMock(); |
46 | 57 | ||
47 | return new PocketImport( | 58 | $pocket = new PocketImportMock( |
48 | $this->tokenStorage, | 59 | $this->tokenStorage, |
49 | $this->session, | ||
50 | $this->em, | 60 | $this->em, |
61 | $this->contentProxy, | ||
51 | $consumerKey | 62 | $consumerKey |
52 | ); | 63 | ); |
64 | |||
65 | $this->logHandler = new TestHandler(); | ||
66 | $logger = new Logger('test', array($this->logHandler)); | ||
67 | $pocket->setLogger($logger); | ||
68 | |||
69 | return $pocket; | ||
53 | } | 70 | } |
54 | 71 | ||
55 | public function testInit() | 72 | public function testInit() |
@@ -65,7 +82,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
65 | $client = new Client(); | 82 | $client = new Client(); |
66 | 83 | ||
67 | $mock = new Mock([ | 84 | $mock = new Mock([ |
68 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar']))), | 85 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))), |
69 | ]); | 86 | ]); |
70 | 87 | ||
71 | $client->getEmitter()->attach($mock); | 88 | $client->getEmitter()->attach($mock); |
@@ -73,10 +90,31 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
73 | $pocketImport = $this->getPocketImport(); | 90 | $pocketImport = $this->getPocketImport(); |
74 | $pocketImport->setClient($client); | 91 | $pocketImport->setClient($client); |
75 | 92 | ||
76 | $url = $pocketImport->oAuthRequest('http://0.0.0.0./redirect', 'http://0.0.0.0./callback'); | 93 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); |
77 | 94 | ||
78 | $this->assertEquals('https://getpocket.com/auth/authorize?request_token=wunderbar&redirect_uri=http://0.0.0.0./callback', $url); | 95 | $this->assertEquals('wunderbar_code', $code); |
79 | $this->assertEquals('wunderbar', $this->session->get('pocketCode')); | 96 | } |
97 | |||
98 | public function testOAuthRequestBadResponse() | ||
99 | { | ||
100 | $client = new Client(); | ||
101 | |||
102 | $mock = new Mock([ | ||
103 | new Response(403), | ||
104 | ]); | ||
105 | |||
106 | $client->getEmitter()->attach($mock); | ||
107 | |||
108 | $pocketImport = $this->getPocketImport(); | ||
109 | $pocketImport->setClient($client); | ||
110 | |||
111 | $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect'); | ||
112 | |||
113 | $this->assertFalse($code); | ||
114 | |||
115 | $records = $this->logHandler->getRecords(); | ||
116 | $this->assertContains('PocketImport: Failed to request token', $records[0]['message']); | ||
117 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
80 | } | 118 | } |
81 | 119 | ||
82 | public function testOAuthAuthorize() | 120 | public function testOAuthAuthorize() |
@@ -84,7 +122,7 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
84 | $client = new Client(); | 122 | $client = new Client(); |
85 | 123 | ||
86 | $mock = new Mock([ | 124 | $mock = new Mock([ |
87 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar']))), | 125 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
88 | ]); | 126 | ]); |
89 | 127 | ||
90 | $client->getEmitter()->attach($mock); | 128 | $client->getEmitter()->attach($mock); |
@@ -92,26 +130,182 @@ class PocketImportTest extends \PHPUnit_Framework_TestCase | |||
92 | $pocketImport = $this->getPocketImport(); | 130 | $pocketImport = $this->getPocketImport(); |
93 | $pocketImport->setClient($client); | 131 | $pocketImport->setClient($client); |
94 | 132 | ||
95 | $accessToken = $pocketImport->oAuthAuthorize(); | 133 | $res = $pocketImport->authorize('wunderbar_code'); |
96 | 134 | ||
97 | $this->assertEquals('wunderbar', $accessToken); | 135 | $this->assertTrue($res); |
136 | $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken()); | ||
98 | } | 137 | } |
99 | 138 | ||
139 | public function testOAuthAuthorizeBadResponse() | ||
140 | { | ||
141 | $client = new Client(); | ||
142 | |||
143 | $mock = new Mock([ | ||
144 | new Response(403), | ||
145 | ]); | ||
146 | |||
147 | $client->getEmitter()->attach($mock); | ||
148 | |||
149 | $pocketImport = $this->getPocketImport(); | ||
150 | $pocketImport->setClient($client); | ||
151 | |||
152 | $res = $pocketImport->authorize('wunderbar_code'); | ||
153 | |||
154 | $this->assertFalse($res); | ||
155 | |||
156 | $records = $this->logHandler->getRecords(); | ||
157 | $this->assertContains('PocketImport: Failed to authorize client', $records[0]['message']); | ||
158 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
159 | } | ||
160 | |||
161 | /** | ||
162 | * Will sample results from https://getpocket.com/developer/docs/v3/retrieve. | ||
163 | */ | ||
100 | public function testImport() | 164 | public function testImport() |
101 | { | 165 | { |
102 | $client = new Client(); | 166 | $client = new Client(); |
103 | 167 | ||
104 | $mock = new Mock([ | 168 | $mock = new Mock([ |
105 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['list' => []]))), | 169 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), |
170 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(' | ||
171 | { | ||
172 | "status": 1, | ||
173 | "list": { | ||
174 | "229279689": { | ||
175 | "item_id": "229279689", | ||
176 | "resolved_id": "229279689", | ||
177 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
178 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
179 | "favorite": "1", | ||
180 | "status": "1", | ||
181 | "resolved_title": "The Massive Ryder Cup Preview", | ||
182 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
183 | "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.", | ||
184 | "is_article": "1", | ||
185 | "has_video": "1", | ||
186 | "has_image": "1", | ||
187 | "word_count": "3197", | ||
188 | "images": { | ||
189 | "1": { | ||
190 | "item_id": "229279689", | ||
191 | "image_id": "1", | ||
192 | "src": "http://a.espncdn.com/combiner/i?img=/photo/2012/0927/grant_g_ryder_cr_640.jpg&w=640&h=360", | ||
193 | "width": "0", | ||
194 | "height": "0", | ||
195 | "credit": "Jamie Squire/Getty Images", | ||
196 | "caption": "" | ||
197 | } | ||
198 | }, | ||
199 | "videos": { | ||
200 | "1": { | ||
201 | "item_id": "229279689", | ||
202 | "video_id": "1", | ||
203 | "src": "http://www.youtube.com/v/Er34PbFkVGk?version=3&hl=en_US&rel=0", | ||
204 | "width": "420", | ||
205 | "height": "315", | ||
206 | "type": "1", | ||
207 | "vid": "Er34PbFkVGk" | ||
208 | } | ||
209 | }, | ||
210 | "tags": { | ||
211 | "grantland": { | ||
212 | "item_id": "1147652870", | ||
213 | "tag": "grantland" | ||
214 | }, | ||
215 | "Ryder Cup": { | ||
216 | "item_id": "1147652870", | ||
217 | "tag": "Ryder Cup" | ||
218 | } | ||
219 | } | ||
220 | }, | ||
221 | "229279690": { | ||
222 | "item_id": "229279689", | ||
223 | "resolved_id": "229279689", | ||
224 | "given_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
225 | "given_title": "The Massive Ryder Cup Preview - The Triangle Blog - Grantland", | ||
226 | "favorite": "1", | ||
227 | "status": "1", | ||
228 | "resolved_title": "The Massive Ryder Cup Preview", | ||
229 | "resolved_url": "http://www.grantland.com/blog/the-triangle/post/_/id/38347/ryder-cup-preview", | ||
230 | "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.", | ||
231 | "is_article": "1", | ||
232 | "has_video": "0", | ||
233 | "has_image": "0", | ||
234 | "word_count": "3197" | ||
235 | } | ||
236 | } | ||
237 | } | ||
238 | ')), | ||
239 | ]); | ||
240 | |||
241 | $client->getEmitter()->attach($mock); | ||
242 | |||
243 | $pocketImport = $this->getPocketImport(); | ||
244 | |||
245 | $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') | ||
246 | ->disableOriginalConstructor() | ||
247 | ->getMock(); | ||
248 | |||
249 | $entryRepo->expects($this->exactly(2)) | ||
250 | ->method('existByUrlAndUserId') | ||
251 | ->will($this->onConsecutiveCalls(false, true)); | ||
252 | |||
253 | $tag = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Tag') | ||
254 | ->disableOriginalConstructor() | ||
255 | ->getMock(); | ||
256 | |||
257 | $tagRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | ||
258 | ->disableOriginalConstructor() | ||
259 | ->getMock(); | ||
260 | |||
261 | $tagRepo->expects($this->exactly(2)) | ||
262 | ->method('findOneByLabelAndUserId') | ||
263 | ->will($this->onConsecutiveCalls(false, $tag)); | ||
264 | |||
265 | $this->em | ||
266 | ->expects($this->any()) | ||
267 | ->method('getRepository') | ||
268 | ->will($this->onConsecutiveCalls($entryRepo, $tagRepo, $tagRepo, $entryRepo)); | ||
269 | |||
270 | $entry = $this->getMockBuilder('Wallabag\CoreBundle\Entity\Entry') | ||
271 | ->disableOriginalConstructor() | ||
272 | ->getMock(); | ||
273 | |||
274 | $this->contentProxy | ||
275 | ->expects($this->once()) | ||
276 | ->method('updateEntry') | ||
277 | ->willReturn($entry); | ||
278 | |||
279 | $pocketImport->setClient($client); | ||
280 | $pocketImport->authorize('wunderbar_code'); | ||
281 | |||
282 | $res = $pocketImport->import(); | ||
283 | |||
284 | $this->assertTrue($res); | ||
285 | $this->assertEquals(['skipped' => 1, 'imported' => 1], $pocketImport->getSummary()); | ||
286 | } | ||
287 | |||
288 | public function testImportBadResponse() | ||
289 | { | ||
290 | $client = new Client(); | ||
291 | |||
292 | $mock = new Mock([ | ||
293 | new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))), | ||
294 | new Response(403), | ||
106 | ]); | 295 | ]); |
107 | 296 | ||
108 | $client->getEmitter()->attach($mock); | 297 | $client->getEmitter()->attach($mock); |
109 | 298 | ||
110 | $pocketImport = $this->getPocketImport(); | 299 | $pocketImport = $this->getPocketImport(); |
111 | $pocketImport->setClient($client); | 300 | $pocketImport->setClient($client); |
301 | $pocketImport->authorize('wunderbar_code'); | ||
302 | |||
303 | $res = $pocketImport->import(); | ||
112 | 304 | ||
113 | $pocketImport->import('wunderbar'); | 305 | $this->assertFalse($res); |
114 | 306 | ||
115 | $this->assertEquals('0 entries imported, 0 already saved.', $this->session->getFlashBag()->get('notice')[0]); | 307 | $records = $this->logHandler->getRecords(); |
308 | $this->assertContains('PocketImport: Failed to import', $records[0]['message']); | ||
309 | $this->assertEquals('ERROR', $records[0]['level_name']); | ||
116 | } | 310 | } |
117 | } | 311 | } |