diff options
Diffstat (limited to 'src/Wallabag/ImportBundle/Tests')
-rw-r--r-- | src/Wallabag/ImportBundle/Tests/Import/PocketImportTest.php | 117 |
1 files changed, 117 insertions, 0 deletions
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 | |||
3 | namespace Wallabag\ImportBundle\Tests\Import; | ||
4 | |||
5 | use Wallabag\UserBundle\Entity\User; | ||
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; | ||
10 | use GuzzleHttp\Subscriber\Mock; | ||
11 | use GuzzleHttp\Message\Response; | ||
12 | use GuzzleHttp\Stream\Stream; | ||
13 | |||
14 | class 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 | } | ||