Completed
Pull Request — master (#479)
by Andrey
03:26
created

testConstructCorrectInstanceWithCustomUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Foursquare;
6
use OAuth\Common\Token\TokenInterface;
7
8
class FoursquareTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri()
14
    {
15
        $service = new Foursquare(
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\Foursquare::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new Foursquare(
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\Foursquare::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri()
42
    {
43
        $service = new Foursquare(
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\Foursquare::__construct
56
     * @covers OAuth\OAuth2\Service\Foursquare::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint()
59
    {
60
        $service = new Foursquare(
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://foursquare.com/oauth2/authenticate', $service->getAuthorizationEndpoint()->getAbsoluteUri());
67
    }
68
69
    /**
70
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
71
     * @covers OAuth\OAuth2\Service\Foursquare::getAccessTokenEndpoint
72
     */
73
    public function testGetAccessTokenEndpoint()
74
    {
75
        $service = new Foursquare(
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://foursquare.com/oauth2/access_token', $service->getAccessTokenEndpoint()->getAbsoluteUri());
82
    }
83
84
    /**
85
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
86
     * @covers OAuth\OAuth2\Service\Foursquare::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(2));
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 Foursquare(
101
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
102
            $client,
103
            $storage
104
        );
105
106
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
107
108
        $this->assertTrue(array_key_exists('Authorization', $headers));
109
        $this->assertTrue(in_array('OAuth foo', $headers, true));
110
    }
111
112
    /**
113
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
114
     * @covers OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse
115
     */
116
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
117
    {
118
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
119
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
120
121
        $service = new Foursquare(
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\Foursquare::__construct
134
     * @covers OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse
135
     */
136
    public function testParseAccessTokenResponseThrowsExceptionOnError()
137
    {
138
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
139
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"error":"some_error"}'));
140
141
        $service = new Foursquare(
142
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
143
            $client,
144
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
145
        );
146
147
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
148
149
        $service->requestAccessToken('foo');
150
    }
151
152
    /**
153
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
154
     * @covers OAuth\OAuth2\Service\Foursquare::parseAccessTokenResponse
155
     */
156
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
157
    {
158
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
159
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
160
161
        $service = new Foursquare(
162
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
163
            $client,
164
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
165
        );
166
167
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
168
    }
169
170
    /**
171
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
172
     * @covers OAuth\OAuth2\Service\Foursquare::request
173
     */
174
    public function testRequest()
175
    {
176
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
177
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
178
179
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
180
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
181
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
182
183
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
184
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
185
186
        $service = new Foursquare(
187
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
188
            $client,
189
            $storage
190
        );
191
192
        $this->assertSame(
193
            'https://pieterhordijk.com/my/awesome/path?v=20130829',
194
            $service->request('https://pieterhordijk.com/my/awesome/path')->getAbsoluteUri()
0 ignored issues
show
Bug introduced by
The method getAbsoluteUri cannot be called on $service->request('https...k.com/my/awesome/path') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
195
        );
196
    }
197
198
    /**
199
     * @covers OAuth\OAuth2\Service\Foursquare::__construct
200
     * @covers OAuth\OAuth2\Service\Foursquare::request
201
     */
202
    public function testRequestShortPath()
203
    {
204
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
205
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0));
206
207
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
208
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
209
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
210
211
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
212
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
213
214
        $service = new Foursquare(
215
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
216
            $client,
217
            $storage
218
        );
219
220
        $this->assertSame(
221
            'https://api.foursquare.com/v2/my/awesome/path?v=20130829',
222
            $service->request('my/awesome/path')->getAbsoluteUri()
0 ignored issues
show
Bug introduced by
The method getAbsoluteUri cannot be called on $service->request('my/awesome/path') (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
223
        );
224
    }
225
}
226