1<?php
2
3namespace OAuthTest\Unit\OAuth2\Service;
4
5use OAuth\OAuth2\Service\Dropbox;
6use OAuth\Common\Token\TokenInterface;
7
8class DropboxTest extends \PHPUnit_Framework_TestCase
9{
10    /**
11     * @covers OAuth\OAuth2\Service\Dropbox::__construct
12     */
13    public function testConstructCorrectInterfaceWithoutCustomUri()
14    {
15        $service = new Dropbox(
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\Dropbox::__construct
26     */
27    public function testConstructCorrectInstanceWithoutCustomUri()
28    {
29        $service = new Dropbox(
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\Dropbox::__construct
40     */
41    public function testConstructCorrectInstanceWithCustomUri()
42    {
43        $service = new Dropbox(
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\Dropbox::__construct
56     * @covers OAuth\OAuth2\Service\Dropbox::getAuthorizationUri
57     */
58    public function testGetAuthorizationUriWithoutAdditionalParams()
59    {
60        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
61        $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo'));
62        $credentials->expects($this->once())->method('getCallbackUrl')->will($this->returnValue('bar'));
63
64        $service = new Dropbox(
65            $credentials,
66            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
67            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
68        );
69
70        $this->assertSame(
71            'https://www.dropbox.com/1/oauth2/authorize?client_id=foo&redirect_uri=bar&response_type=code&scope=',
72            $service->getAuthorizationUri()->getAbsoluteUri()
73        );
74    }
75
76    /**
77     * @covers OAuth\OAuth2\Service\Dropbox::__construct
78     * @covers OAuth\OAuth2\Service\Dropbox::getAuthorizationUri
79     */
80    public function testGetAuthorizationUriWithAdditionalParams()
81    {
82        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
83        $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo'));
84        $credentials->expects($this->once())->method('getCallbackUrl')->will($this->returnValue('bar'));
85
86        $service = new Dropbox(
87            $credentials,
88            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
89            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
90        );
91
92        $this->assertSame(
93            'https://www.dropbox.com/1/oauth2/authorize?client_id=foo&redirect_uri=bar&response_type=code&scope=',
94            $service->getAuthorizationUri()->getAbsoluteUri()
95        );
96    }
97
98    /**
99     * @covers OAuth\OAuth2\Service\Dropbox::__construct
100     * @covers OAuth\OAuth2\Service\Dropbox::getAuthorizationEndpoint
101     */
102    public function testGetAuthorizationEndpoint()
103    {
104        $service = new Dropbox(
105            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
106            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
107            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
108        );
109
110        $this->assertSame('https://www.dropbox.com/1/oauth2/authorize', $service->getAuthorizationEndpoint()->getAbsoluteUri());
111    }
112
113    /**
114     * @covers OAuth\OAuth2\Service\Dropbox::__construct
115     * @covers OAuth\OAuth2\Service\Dropbox::getAccessTokenEndpoint
116     */
117    public function testGetAccessTokenEndpoint()
118    {
119        $service = new Dropbox(
120            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
121            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
122            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
123        );
124
125        $this->assertSame('https://api.dropbox.com/1/oauth2/token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
126    }
127
128    /**
129     * @covers OAuth\OAuth2\Service\Dropbox::__construct
130     * @covers OAuth\OAuth2\Service\Dropbox::getAuthorizationMethod
131     */
132    public function testGetAuthorizationMethod()
133    {
134        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
135        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
136
137        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
138        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
139        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
140
141        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
142        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
143
144        $service = new Dropbox(
145            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
146            $client,
147            $storage
148        );
149
150        $uri         = $service->request('https://pieterhordijk.com/my/awesome/path');
151        $absoluteUri = parse_url($uri->getAbsoluteUri());
152
153        $this->assertSame('access_token=foo', $absoluteUri['query']);
154    }
155
156    /**
157     * @covers OAuth\OAuth2\Service\Dropbox::__construct
158     * @covers OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
159     */
160    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
161    {
162        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
163        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
164
165        $service = new Dropbox(
166            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
167            $client,
168            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
169        );
170
171        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
172
173        $service->requestAccessToken('foo');
174    }
175
176    /**
177     * @covers OAuth\OAuth2\Service\Dropbox::__construct
178     * @covers OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
179     */
180    public function testParseAccessTokenResponseThrowsExceptionOnError()
181    {
182        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
183        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
184
185        $service = new Dropbox(
186            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
187            $client,
188            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
189        );
190
191        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
192
193        $service->requestAccessToken('foo');
194    }
195
196    /**
197     * @covers OAuth\OAuth2\Service\Dropbox::__construct
198     * @covers OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
199     */
200    public function testParseAccessTokenResponseValidWithoutRefreshToken()
201    {
202        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
203        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
204
205        $service = new Dropbox(
206            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
207            $client,
208            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
209        );
210
211        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
212    }
213
214    /**
215     * @covers OAuth\OAuth2\Service\Dropbox::__construct
216     * @covers OAuth\OAuth2\Service\Dropbox::parseAccessTokenResponse
217     */
218    public function testParseAccessTokenResponseValidWithRefreshToken()
219    {
220        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
221        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
222
223        $service = new Dropbox(
224            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
225            $client,
226            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
227        );
228
229        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
230    }
231}
232