aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/ImportBundle/Tests/Import
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/ImportBundle/Tests/Import')
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/ImportChainTest.php21
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/ImportCompilerPassTest.php47
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php314
-rw-r--r--src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php97
4 files changed, 479 insertions, 0 deletions
diff --git a/src/Wallabag/ImportBundle/Tests/Import/ImportChainTest.php b/src/Wallabag/ImportBundle/Tests/Import/ImportChainTest.php
new file mode 100644
index 00000000..702d2a9b
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Tests/Import/ImportChainTest.php
@@ -0,0 +1,21 @@
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\ImportBundle\Import\ImportChain;
6
7class ImportChainTest extends \PHPUnit_Framework_TestCase
8{
9 public function testGetAll()
10 {
11 $import = $this->getMockBuilder('Wallabag\ImportBundle\Import\ImportInterface')
12 ->disableOriginalConstructor()
13 ->getMock();
14
15 $importChain = new ImportChain();
16 $importChain->addImport($import, 'alias');
17
18 $this->assertCount(1, $importChain->getAll());
19 $this->assertEquals($import, $importChain->getAll()['alias']);
20 }
21}
diff --git a/src/Wallabag/ImportBundle/Tests/Import/ImportCompilerPassTest.php b/src/Wallabag/ImportBundle/Tests/Import/ImportCompilerPassTest.php
new file mode 100644
index 00000000..bd62ab3b
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Tests/Import/ImportCompilerPassTest.php
@@ -0,0 +1,47 @@
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Symfony\Component\DependencyInjection\ContainerBuilder;
6use Wallabag\ImportBundle\Import\ImportCompilerPass;
7
8class ImportCompilerPassTest extends \PHPUnit_Framework_TestCase
9{
10 public function testProcessNoDefinition()
11 {
12 $container = new ContainerBuilder();
13 $res = $this->process($container);
14
15 $this->assertNull($res);
16 }
17
18 public function testProcess()
19 {
20 $container = new ContainerBuilder();
21 $container
22 ->register('wallabag_import.chain')
23 ->setPublic(false)
24 ;
25
26 $container
27 ->register('foo')
28 ->addTag('wallabag_import.import', array('alias' => 'pocket'))
29 ;
30
31 $this->process($container);
32
33 $this->assertTrue($container->hasDefinition('wallabag_import.chain'));
34
35 $definition = $container->getDefinition('wallabag_import.chain');
36 $this->assertTrue($definition->hasMethodCall('addImport'));
37
38 $calls = $definition->getMethodCalls();
39 $this->assertEquals('pocket', $calls[0][1][1]);
40 }
41
42 protected function process(ContainerBuilder $container)
43 {
44 $repeatedPass = new ImportCompilerPass();
45 $repeatedPass->process($container);
46 }
47}
diff --git a/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
new file mode 100644
index 00000000..043b2114
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php
@@ -0,0 +1,314 @@
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\ImportBundle\Import\PocketImport;
7use GuzzleHttp\Client;
8use GuzzleHttp\Subscriber\Mock;
9use GuzzleHttp\Message\Response;
10use GuzzleHttp\Stream\Stream;
11use Monolog\Logger;
12use Monolog\Handler\TestHandler;
13
14class PocketImportMock extends PocketImport
15{
16 public function getAccessToken()
17 {
18 return $this->accessToken;
19 }
20}
21
22class PocketImportTest extends \PHPUnit_Framework_TestCase
23{
24 protected $token;
25 protected $user;
26 protected $em;
27 protected $contentProxy;
28 protected $logHandler;
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
42 $this->contentProxy = $this->getMockBuilder('Wallabag\CoreBundle\Helper\ContentProxy')
43 ->disableOriginalConstructor()
44 ->getMock();
45
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
54 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
55 ->disableOriginalConstructor()
56 ->getMock();
57
58 $pocket = new PocketImportMock(
59 $this->tokenStorage,
60 $this->em,
61 $this->contentProxy,
62 $consumerKey
63 );
64
65 $this->logHandler = new TestHandler();
66 $logger = new Logger('test', array($this->logHandler));
67 $pocket->setLogger($logger);
68
69 return $pocket;
70 }
71
72 public function testInit()
73 {
74 $pocketImport = $this->getPocketImport();
75
76 $this->assertEquals('Pocket', $pocketImport->getName());
77 $this->assertNotEmpty($pocketImport->getUrl());
78 $this->assertContains('This importer will import all your Pocket data.', $pocketImport->getDescription());
79 }
80
81 public function testOAuthRequest()
82 {
83 $client = new Client();
84
85 $mock = new Mock([
86 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['code' => 'wunderbar_code']))),
87 ]);
88
89 $client->getEmitter()->attach($mock);
90
91 $pocketImport = $this->getPocketImport();
92 $pocketImport->setClient($client);
93
94 $code = $pocketImport->getRequestToken('http://0.0.0.0/redirect');
95
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']);
119 }
120
121 public function testOAuthAuthorize()
122 {
123 $client = new Client();
124
125 $mock = new Mock([
126 new Response(200, ['Content-Type' => 'application/json'], Stream::factory(json_encode(['access_token' => 'wunderbar_token']))),
127 ]);
128
129 $client->getEmitter()->attach($mock);
130
131 $pocketImport = $this->getPocketImport();
132 $pocketImport->setClient($client);
133
134 $res = $pocketImport->authorize('wunderbar_code');
135
136 $this->assertTrue($res);
137 $this->assertEquals('wunderbar_token', $pocketImport->getAccessToken());
138 }
139
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 */
165 public function testImport()
166 {
167 $client = new Client();
168
169 $mock = new Mock([
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))
251 ->method('existByUrlAndUserId')
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))
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')
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),
298 ]);
299
300 $client->getEmitter()->attach($mock);
301
302 $pocketImport = $this->getPocketImport();
303 $pocketImport->setClient($client);
304 $pocketImport->authorize('wunderbar_code');
305
306 $res = $pocketImport->import();
307
308 $this->assertFalse($res);
309
310 $records = $this->logHandler->getRecords();
311 $this->assertContains('PocketImport: Failed to import', $records[0]['message']);
312 $this->assertEquals('ERROR', $records[0]['level_name']);
313 }
314}
diff --git a/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php b/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php
new file mode 100644
index 00000000..d5b41777
--- /dev/null
+++ b/src/Wallabag/ImportBundle/Tests/Import/WallabagV1ImportTest.php
@@ -0,0 +1,97 @@
1<?php
2
3namespace Wallabag\ImportBundle\Tests\Import;
4
5use Wallabag\UserBundle\Entity\User;
6use Wallabag\ImportBundle\Import\WallabagV1Import;
7use Monolog\Logger;
8use Monolog\Handler\TestHandler;
9
10class WallabagV1ImportTest extends \PHPUnit_Framework_TestCase
11{
12 protected $user;
13 protected $em;
14 protected $logHandler;
15
16 private function getWallabagV1Import($unsetUser = false)
17 {
18 $this->user = new User();
19
20 $this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
21 ->disableOriginalConstructor()
22 ->getMock();
23
24 $pocket = new WallabagV1Import($this->em);
25
26 $this->logHandler = new TestHandler();
27 $logger = new Logger('test', array($this->logHandler));
28 $pocket->setLogger($logger);
29
30 if (false === $unsetUser) {
31 $pocket->setUser($this->user);
32 }
33
34 return $pocket;
35 }
36
37 public function testInit()
38 {
39 $wallabagV1Import = $this->getWallabagV1Import();
40
41 $this->assertEquals('wallabag v1', $wallabagV1Import->getName());
42 $this->assertNotEmpty($wallabagV1Import->getUrl());
43 $this->assertContains('This importer will import all your wallabag v1 articles.', $wallabagV1Import->getDescription());
44 }
45
46 public function testImport()
47 {
48 $wallabagV1Import = $this->getWallabagV1Import();
49 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
50
51 $entryRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
52 ->disableOriginalConstructor()
53 ->getMock();
54
55 $entryRepo->expects($this->exactly(3))
56 ->method('existByUrlAndUserId')
57 ->will($this->onConsecutiveCalls(false, true, false));
58
59 $this->em
60 ->expects($this->any())
61 ->method('getRepository')
62 ->willReturn($entryRepo);
63
64 $res = $wallabagV1Import->import();
65
66 $this->assertTrue($res);
67 $this->assertEquals(['skipped' => 1, 'imported' => 2], $wallabagV1Import->getSummary());
68 }
69
70 public function testImportBadFile()
71 {
72 $wallabagV1Import = $this->getWallabagV1Import();
73 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.jsonx');
74
75 $res = $wallabagV1Import->import();
76
77 $this->assertFalse($res);
78
79 $records = $this->logHandler->getRecords();
80 $this->assertContains('WallabagV1Import: unable to read file', $records[0]['message']);
81 $this->assertEquals('ERROR', $records[0]['level_name']);
82 }
83
84 public function testImportUserNotDefined()
85 {
86 $wallabagV1Import = $this->getWallabagV1Import(true);
87 $wallabagV1Import->setFilepath(__DIR__.'/../fixtures/wallabag-v1.json');
88
89 $res = $wallabagV1Import->import();
90
91 $this->assertFalse($res);
92
93 $records = $this->logHandler->getRecords();
94 $this->assertContains('WallabagV1Import: user is not defined', $records[0]['message']);
95 $this->assertEquals('ERROR', $records[0]['level_name']);
96 }
97}