3 namespace Wallabag\ImportBundle\Tests\Import
;
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
;
10 use GuzzleHttp\Subscriber\Mock
;
11 use GuzzleHttp\Message\Response
;
12 use GuzzleHttp\Stream\Stream
;
14 class PocketImportTest
extends \PHPUnit_Framework_TestCase
21 private function getPocketImport($consumerKey = 'ConsumerKey')
23 $this->user
= new User();
25 $this->tokenStorage
= $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface')
26 ->disableOriginalConstructor()
29 $token = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\TokenInterface')
30 ->disableOriginalConstructor()
33 $token->expects($this->once())
35 ->willReturn($this->user
);
37 $this->tokenStorage
->expects($this->once())
41 $this->session
= new Session(new MockArraySessionStorage());
43 $this->em
= $this->getMockBuilder('Doctrine\ORM\EntityManager')
44 ->disableOriginalConstructor()
47 return new PocketImport(
55 public function testInit()
57 $pocketImport = $this->getPocketImport();
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());
63 public function testOAuthRequest()
65 $client = new Client();
68 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['code' => 'wunderbar']))),
71 $client->getEmitter()->attach($mock);
73 $pocketImport = $this->getPocketImport();
74 $pocketImport->setClient($client);
76 $url = $pocketImport->oAuthRequest('http://0.0.0.0./redirect', 'http://0.0.0.0./callback');
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'));
82 public function testOAuthAuthorize()
84 $client = new Client();
87 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['access_token' => 'wunderbar']))),
90 $client->getEmitter()->attach($mock);
92 $pocketImport = $this->getPocketImport();
93 $pocketImport->setClient($client);
95 $accessToken = $pocketImport->oAuthAuthorize();
97 $this->assertEquals('wunderbar', $accessToken);
100 public function testImport()
102 $client = new Client();
105 new Response(200, ['Content-Type' => 'application/json'], Stream
::factory(json_encode(['list' => []]))),
108 $client->getEmitter()->attach($mock);
110 $pocketImport = $this->getPocketImport();
111 $pocketImport->setClient($client);
113 $pocketImport->import('wunderbar');
115 $this->assertEquals('0 entries imported, 0 already saved.', $this->session
->getFlashBag()->get('notice')[0]);