Completed
Pull Request — master (#483)
by
unknown
03:10
created

testConstructCorrectInstanceWithoutCustomUri()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
dl 0
loc 10
rs 9.4285
c 1
b 0
f 1
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace OAuthTest\Unit\OAuth2\Service;
4
5
use OAuth\Common\Token\TokenInterface;
6
use OAuth\OAuth2\Service\EveOnline;
7
8
class EveOnlineTest extends \PHPUnit_Framework_TestCase
9
{
10
    /**
11
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
12
     */
13
    public function testConstructCorrectInterfaceWithoutCustomUri()
14
    {
15
        $service = new EveOnline(
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\EveOnline::__construct
26
     */
27
    public function testConstructCorrectInstanceWithoutCustomUri()
28
    {
29
        $service = new EveOnline(
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\EveOnline::__construct
40
     */
41
    public function testConstructCorrectInstanceWithCustomUri()
42
    {
43
        $service = new EveOnline(
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\EveOnline::__construct
56
     * @covers OAuth\OAuth2\Service\EveOnline::getAuthorizationEndpoint
57
     */
58
    public function testGetAuthorizationEndpoint()
59
    {
60
        $service = new EveOnline(
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://login.eveonline.com/oauth/authorize',
67
            $service->getAuthorizationEndpoint()->getAbsoluteUri());
68
    }
69
70
    /**
71
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
72
     * @covers OAuth\OAuth2\Service\EveOnline::getAccessTokenEndpoint
73
     */
74
    public function testGetAccessTokenEndpoint()
75
    {
76
        $service = new EveOnline(
77
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
78
            $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),
79
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
80
        );
81
82
        $this->assertSame('https://login.eveonline.com/oauth/token',
83
            $service->getAccessTokenEndpoint()->getAbsoluteUri());
84
    }
85
86
    /**
87
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
88
     * @covers OAuth\OAuth2\Service\EveOnline::getAuthorizationMethod
89
     */
90
    public function testGetAuthorizationMethod()
91
    {
92
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
93
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2));
94
95
        $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface');
96
        $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES));
97
        $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo'));
98
99
        $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface');
100
        $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token));
101
102
        $service = new EveOnline(
103
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
104
            $client,
105
            $storage
106
        );
107
108
        $headers = $service->request('https://pieterhordijk.com/my/awesome/path');
109
110
        $this->assertTrue(array_key_exists('Authorization', $headers));
111
        $this->assertTrue(in_array('Bearer foo', $headers, true));
112
    }
113
114
    /**
115
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
116
     * @covers OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse
117
     */
118
    public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse()
119
    {
120
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
121
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue(null));
122
123
        $service = new EveOnline(
124
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
125
            $client,
126
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
127
        );
128
129
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
130
131
        $service->requestAccessToken('foo');
132
    }
133
134
    /**
135
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
136
     * @covers OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse
137
     */
138
    public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription()
139
    {
140
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
141
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error_description=some_error'));
142
143
        $service = new EveOnline(
144
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
145
            $client,
146
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
147
        );
148
149
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
150
151
        $service->requestAccessToken('foo');
152
    }
153
154
    /**
155
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
156
     * @covers OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse
157
     */
158
    public function testParseAccessTokenResponseThrowsExceptionOnError()
159
    {
160
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
161
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('error=some_error'));
162
163
        $service = new EveOnline(
164
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
165
            $client,
166
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
167
        );
168
169
        $this->setExpectedException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException');
170
171
        $service->requestAccessToken('foo');
172
    }
173
174
    /**
175
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
176
     * @covers OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse
177
     */
178
    public function testParseAccessTokenResponseValidWithoutRefreshToken()
179
    {
180
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
181
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar"}'));
182
183
        $service = new EveOnline(
184
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
185
            $client,
186
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
187
        );
188
189
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
190
    }
191
192
    /**
193
     * @covers OAuth\OAuth2\Service\EveOnline::__construct
194
     * @covers OAuth\OAuth2\Service\EveOnline::parseAccessTokenResponse
195
     */
196
    public function testParseAccessTokenResponseValidWithRefreshToken()
197
    {
198
        $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface');
199
        $client->expects($this->once())->method('retrieveResponse')->will($this->returnValue('{"access_token":"foo","expires_in":"bar","refresh_token":"baz"}'));
200
201
        $service = new EveOnline(
202
            $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),
203
            $client,
204
            $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface')
205
        );
206
207
        $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo'));
208
    }
209
}
210