Completed
Pull Request — master (#467)
by
unknown
03:05
created

FacebookTest::testGetAccessTokenEndpoint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Facebook;
6
use OAuth\Common\Token\TokenInterface;
7
8
class FacebookTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Facebook::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri()
14
    {
15
        $service = new Facebook(
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\Facebook::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new Facebook(
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\Facebook::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri()
42
    {
43
        $service = new Facebook(
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\Facebook::__construct
56
     * @covers OAuth\OAuth2\Service\Facebook::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint()
59
    {
60
        $service = new Facebook(
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://www.facebook.com/dialog/oauth', $service->getAuthorizationEndpoint()->getAbsoluteUri());
67
    }
68
69
    /**
70
     * @covers OAuth\OAuth2\Service\Facebook::__construct
71
     * @covers OAuth\OAuth2\Service\Facebook::getAccessTokenEndpoint
72
     */
73
    public function testGetAccessTokenEndpoint()
74
    {
75
        $service = new Facebook(
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://graph.facebook.com/oauth/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
82
    }
83
84
    /**
85
     * @covers OAuth\OAuth2\Service\Facebook::__construct
86
     */
87
    public function testGetAuthorizationMethod()
88
    {
89
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
90
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2));
91
92
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
93
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
94
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
95
96
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
97
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
98
99
        $service = new Facebook(
100
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
101
            $client,
102
            $storage
103
        );
104
105
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
106
107
        $this->assertTrue(array_key_exists('Authorization', $headers));
108
        $this->assertTrue(in_array('OAuth foo', $headers, true));
109
    }
110
111
    /**
112
     * @covers OAuth\OAuth2\Service\Facebook::__construct
113
     * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse
114
     */
115
    public function testParseAccessTokenResponseThrowsExceptionOnError()
116
    {
117
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
118
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
119
120
        $service = new Facebook(
121
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
122
            $client,
123
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
124
        );
125
126
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
127
128
        $service->requestAccessToken('foo');
129
    }
130
131
    /**
132
     * @covers OAuth\OAuth2\Service\Facebook::__construct
133
     * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse
134
     */
135
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
136
    {
137
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
138
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('access_token=foo&expires=bar'));
139
140
        $service = new Facebook(
141
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
142
            $client,
143
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
144
        );
145
146
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
147
    }
148
149
    /**
150
     * @covers OAuth\OAuth2\Service\Facebook::__construct
151
     * @covers OAuth\OAuth2\Service\Facebook::parseAccessTokenResponse
152
     */
153
    public function testParseAccessTokenResponseValidWithRefreshToken()
154
    {
155
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
156
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('access_token=foo&expires=bar&refresh_token=baz'));
157
158
        $service = new Facebook(
159
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
160
            $client,
161
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
162
        );
163
164
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
165
    }
166
167
    /**
168
     * @covers OAuth\OAuth2\Service\Facebook::__construct
169
     * @covers OAuth\OAuth2\Service\Facebook::getDialogUri
170
     */
171
    public function testGetDialogUriRedirectUriMissing()
172
    {
173
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
174
175
        $service = new Facebook(
176
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
177
            $client,
178
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
179
        );
180
181
        $this->setExpectedException('\\OAuth\\Common\\Exception\\Exception');
182
183
        $service->getDialogUri('feed', array());
184
    }
185
186
    /**
187
     * @covers OAuth\OAuth2\Service\Facebook::__construct
188
     * @covers OAuth\OAuth2\Service\Facebook::getDialogUri
189
     */
190
    public function testGetDialogUriInstanceofUri()
191
    {
192
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
193
194
        $service = new Facebook(
195
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
196
            $client,
197
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
198
        );
199
200
        $dialogUri = $service->getDialogUri(
201
            'feed',
202
            array(
203
                'redirect_uri' => 'http://www.facebook.com',
204
                'state' => 'Random state'
205
            )
206
        );
207
        $this->assertInstanceOf('\\OAuth\\Common\\Http\\Uri\\Uri',$dialogUri);
208
    }
209
210
    /**
211
     * @covers OAuth\OAuth2\Service\Facebook::__construct
212
     * @covers OAuth\OAuth2\Service\Facebook::getDialogUri
213
     */
214
    public function testGetDialogUriContainsAppIdAndOtherParameters()
215
    {
216
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
217
        $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface');
218
        $credentials->expects($this->any())->method('getConsumerId')->will($this->returnValue('application_id'));
219
220
221
        $service = new Facebook(
222
            $credentials,
223
            $client,
224
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
225
        );
226
227
        $dialogUri = $service->getDialogUri(
228
            'feed',
229
            array(
230
                'redirect_uri' => 'http://www.facebook.com',
231
                'state' => 'Random state'
232
            )
233
        );
234
235
        $queryString = $dialogUri->getQuery();
236
        parse_str($queryString, $queryArray);
237
238
        $this->assertArrayHasKey('app_id', $queryArray);
239
        $this->assertArrayHasKey('redirect_uri', $queryArray);
240
        $this->assertArrayHasKey('state', $queryArray);
241
    }
242
}
243