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