1<?php
2
3namespace OAuthTest\Unit\OAuth2\Service;
4
5use OAuth\OAuth2\Service\Bitly;
6use OAuth\Common\Token\TokenInterface;
7
8class BitlyTest extends \PHPUnit_Framework_TestCase
9{
10    /**
11     * @covers OAuth\OAuth2\Service\Bitly::__construct
12     */
13    public function testConstructCorrectInterfaceWithoutCustomUri()
14    {
15        $service = new Bitly(
16            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
17            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
18            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
19        );
20
21        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service);
22    }
23
24    /**
25     * @covers OAuth\OAuth2\Service\Bitly::__construct
26     */
27    public function testConstructCorrectInstanceWithoutCustomUri()
28    {
29        $service = new Bitly(
30            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
31            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
32            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
33        );
34
35        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
36    }
37
38    /**
39     * @covers OAuth\OAuth2\Service\Bitly::__construct
40     */
41    public function testConstructCorrectInstanceWithCustomUri()
42    {
43        $service = new Bitly(
44            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
45            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
46            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),
47            array(),
48            $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface')
49        );
50
51        $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service);
52    }
53
54    /**
55     * @covers OAuth\OAuth2\Service\Bitly::__construct
56     * @covers OAuth\OAuth2\Service\Bitly::getAuthorizationEndpoint
57     */
58    public function testGetAuthorizationEndpoint()
59    {
60        $service = new Bitly(
61            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
62            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
63            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
64        );
65
66        $this->assertSame('https://bitly.com/oauth/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
67    }
68
69    /**
70     * @covers OAuth\OAuth2\Service\Bitly::__construct
71     * @covers OAuth\OAuth2\Service\Bitly::getAccessTokenEndpoint
72     */
73    public function testGetAccessTokenEndpoint()
74    {
75        $service = new Bitly(
76            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
77            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
78            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
79        );
80
81        $this->assertSame('https://api-ssl.bitly.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
82    }
83
84    /**
85     * @covers OAuth\OAuth2\Service\Bitly::__construct
86     * @covers OAuth\OAuth2\Service\Bitly::getAuthorizationMethod
87     */
88    public function testGetAuthorizationMethod()
89    {
90        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
91        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
92
93        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
94        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
95        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
96
97        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
98        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
99
100        $service = new Bitly(
101            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
102            $client,
103            $storage
104        );
105
106        $uri         = $service->request('https://pieterhordijk.com/my/awesome/path');
107        $absoluteUri = parse_url($uri->getAbsoluteUri());
108
109        $this->assertSame('access_token=foo', $absoluteUri['query']);
110    }
111
112    /**
113     * @covers OAuth\OAuth2\Service\Bitly::__construct
114     * @covers OAuth\OAuth2\Service\Bitly::parseAccessTokenResponse
115     */
116    public function testParseAccessTokenResponseThrowsExceptionOnError()
117    {
118        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
119        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
120
121        $service = new Bitly(
122            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
123            $client,
124            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
125        );
126
127        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
128
129        $service->requestAccessToken('foo');
130    }
131
132    /**
133     * @covers OAuth\OAuth2\Service\Bitly::__construct
134     * @covers OAuth\OAuth2\Service\Bitly::parseAccessTokenResponse
135     * @covers OAuth\OAuth2\Service\Bitly::requestAccessToken
136     */
137    public function testParseAccessTokenResponseValid()
138    {
139        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
140        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('access_token=foo'));
141
142        $service = new Bitly(
143            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
144            $client,
145            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
146        );
147
148        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
149    }
150}
151