Completed
Pull Request — master (#456)
by
unknown
09:01
created

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