Completed
Pull Request — master (#466)
by
unknown
03:50
created

testGetAuthorizationEndpointException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 17
rs 9.4285
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\OAuth2\Service\Google;
6
use OAuth\Common\Token\TokenInterface;
7
8
class GoogleTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\Google::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri()
14
    {
15
        $service = new Google(
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\Google::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new Google(
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\Google::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri()
42
    {
43
        $service = new Google(
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\Google::__construct
56
     * @covers OAuth\OAuth2\Service\Google::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint()
59
    {
60
        $service = new Google(
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(
67
            'https://accounts.google.com/o/oauth2/auth?access_type=online',
68
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
69
        );
70
71
        // Verify that 'offine' works
72
        $service->setAccessType('offline');
73
        $this->assertSame(
74
            'https://accounts.google.com/o/oauth2/auth?access_type=offline',
75
            $service->getAuthorizationEndpoint()->getAbsoluteUri()
76
        );
77
78
    }
79
80
    /**
81
     * @covers OAuth\OAuth2\Service\Google::__construct
82
     * @covers OAuth\OAuth2\Service\Google::getAuthorizationEndpoint
83
     */
84
    public function testGetAuthorizationEndpointException()
85
    {
86
        $service = new Google(
87
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
88
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
89
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
90
        );
91
92
        $this->setExpectedException('OAuth\OAuth2\Service\Exception\InvalidAccessTypeException');
93
94
        try {
95
            $service->setAccessType('invalid');
96
        } catch (InvalidAccessTypeException $e) {
0 ignored issues
show
Bug introduced by
The class OAuthTest\Unit\OAuth2\Se...alidAccessTypeException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
97
            return;
98
        }
99
        $this->fail('Expected InvalidAccessTypeException not thrown');
100
    }
101
102
    /**
103
     * @covers OAuth\OAuth2\Service\Google::__construct
104
     * @covers OAuth\OAuth2\Service\Google::getAccessTokenEndpoint
105
     */
106
    public function testGetAccessTokenEndpoint()
107
    {
108
        $service = new Google(
109
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
110
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
111
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
112
        );
113
114
        $this->assertSame(
115
            'https://accounts.google.com/o/oauth2/token',
116
            $service->getAccessTokenEndpoint()->getAbsoluteUri()
117
        );
118
    }
119
120
    /**
121
     * @covers OAuth\OAuth2\Service\Google::__construct
122
     * @covers OAuth\OAuth2\Service\Google::parseAccessTokenResponse
123
     */
124
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
125
    {
126
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
127
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
128
129
        $service = new Google(
130
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
131
            $client,
132
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
133
        );
134
135
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
136
137
        $service->requestAccessToken('foo');
138
    }
139
140
    /**
141
     * @covers OAuth\OAuth2\Service\Google::__construct
142
     * @covers OAuth\OAuth2\Service\Google::parseAccessTokenResponse
143
     */
144
    public function testParseAccessTokenResponseThrowsExceptionOnError()
145
    {
146
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
147
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
148
149
        $service = new Google(
150
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
151
            $client,
152
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
153
        );
154
155
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
156
157
        $service->requestAccessToken('foo');
158
    }
159
160
    /**
161
     * @covers OAuth\OAuth2\Service\Google::__construct
162
     * @covers OAuth\OAuth2\Service\Google::parseAccessTokenResponse
163
     */
164
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
165
    {
166
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
167
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
168
169
        $service = new Google(
170
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
171
            $client,
172
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
173
        );
174
175
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
176
    }
177
178
    /**
179
     * @covers OAuth\OAuth2\Service\Google::__construct
180
     * @covers OAuth\OAuth2\Service\Google::parseAccessTokenResponse
181
     */
182
    public function testParseAccessTokenResponseValidWithRefreshToken()
183
    {
184
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
185
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
186
187
        $service = new Google(
188
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
189
            $client,
190
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
191
        );
192
193
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
194
    }
195
}
196