1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuthTest\Unit\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\Common\Token\TokenInterface; |
6
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
7
|
|
|
use OAuth\OAuth2\Service\Slack; |
8
|
|
|
use PHPUnit\Framework\TestCase; |
9
|
|
|
|
10
|
|
|
class SlackTest extends TestCase |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
14
|
|
|
*/ |
15
|
|
|
public function testConstructCorrectInterfaceWithoutCustomUri(): void |
16
|
|
|
{ |
17
|
|
|
$service = new Slack( |
18
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
19
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
20
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
28
|
|
|
*/ |
29
|
|
|
public function testConstructCorrectInstanceWithoutCustomUri(): void |
30
|
|
|
{ |
31
|
|
|
$service = new Slack( |
32
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
33
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
34
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
35
|
|
|
); |
36
|
|
|
|
37
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
42
|
|
|
*/ |
43
|
|
|
public function testConstructCorrectInstanceWithCustomUri(): void |
44
|
|
|
{ |
45
|
|
|
$service = new Slack( |
46
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
47
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
48
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'), |
|
|
|
|
49
|
|
|
[], |
50
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Uri\\UriInterface') |
|
|
|
|
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Service\\AbstractService', $service); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
58
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::getAuthorizationEndpoint |
59
|
|
|
*/ |
60
|
|
|
public function testGetAuthorizationEndpoint(): void |
61
|
|
|
{ |
62
|
|
|
$service = new Slack( |
63
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
64
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
65
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
66
|
|
|
); |
67
|
|
|
|
68
|
|
|
self::assertSame( |
69
|
|
|
'https://slack.com/oauth/v2/authorize', |
70
|
|
|
$service->getAuthorizationEndpoint()->getAbsoluteUri() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
76
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::getAccessTokenEndpoint |
77
|
|
|
*/ |
78
|
|
|
public function testGetAccessTokenEndpoint(): void |
79
|
|
|
{ |
80
|
|
|
$service = new Slack( |
81
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
82
|
|
|
$this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'), |
|
|
|
|
83
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
84
|
|
|
); |
85
|
|
|
|
86
|
|
|
self::assertSame( |
87
|
|
|
'https://slack.com/api/oauth.v2.access', |
88
|
|
|
$service->getAccessTokenEndpoint()->getAbsoluteUri() |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
94
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::getAuthorizationMethod |
95
|
|
|
*/ |
96
|
|
|
public function testGetAuthorizationMethod(): void |
97
|
|
|
{ |
98
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
99
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturnArgument(2); |
100
|
|
|
|
101
|
|
|
$token = $this->createMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); |
102
|
|
|
$token->expects(self::once())->method('getEndOfLife')->willReturn(TokenInterface::EOL_NEVER_EXPIRES); |
103
|
|
|
$token->expects(self::once())->method('getAccessToken')->willReturn('foo'); |
104
|
|
|
|
105
|
|
|
$storage = $this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); |
106
|
|
|
$storage->expects(self::once())->method('retrieveAccessToken')->willReturn($token); |
107
|
|
|
|
108
|
|
|
$service = new Slack( |
109
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
110
|
|
|
$client, |
|
|
|
|
111
|
|
|
$storage |
|
|
|
|
112
|
|
|
); |
113
|
|
|
|
114
|
|
|
$headers = $service->request('https://pieterhordijk.com/my/awesome/path'); |
115
|
|
|
self::assertArrayHasKey('Authorization', $headers); |
|
|
|
|
116
|
|
|
self::assertTrue(in_array('Bearer foo', $headers, true)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
121
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::parseAccessTokenResponse |
122
|
|
|
*/ |
123
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnNulledResponse(): void |
124
|
|
|
{ |
125
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
126
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn(null); |
127
|
|
|
|
128
|
|
|
$service = new Slack( |
129
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
130
|
|
|
$client, |
|
|
|
|
131
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
132
|
|
|
); |
133
|
|
|
|
134
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
135
|
|
|
|
136
|
|
|
$service->requestAccessToken('foo'); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
141
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::parseAccessTokenResponse |
142
|
|
|
*/ |
143
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnErrorDescription(): void |
144
|
|
|
{ |
145
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
146
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('error_description=some_error'); |
147
|
|
|
|
148
|
|
|
$service = new Slack( |
149
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
150
|
|
|
$client, |
|
|
|
|
151
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
155
|
|
|
|
156
|
|
|
$service->requestAccessToken('foo'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
161
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::parseAccessTokenResponse |
162
|
|
|
*/ |
163
|
|
|
public function testParseAccessTokenResponseThrowsExceptionOnError(): void |
164
|
|
|
{ |
165
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
166
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('error=some_error'); |
167
|
|
|
|
168
|
|
|
$service = new Slack( |
169
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
170
|
|
|
$client, |
|
|
|
|
171
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
172
|
|
|
); |
173
|
|
|
|
174
|
|
|
$this->expectException('\\OAuth\\Common\\Http\\Exception\\TokenResponseException'); |
175
|
|
|
|
176
|
|
|
$service->requestAccessToken('foo'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::__construct |
181
|
|
|
* @covers \OAuth\OAuth2\Service\Slack::parseAccessTokenResponse |
182
|
|
|
*/ |
183
|
|
|
public function testParseAccessTokenResponseValidWithoutRefreshToken(): void |
184
|
|
|
{ |
185
|
|
|
|
186
|
|
|
$client = $this->createMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); |
187
|
|
|
$client->expects(self::once())->method('retrieveResponse')->willReturn('{"access_token":"foo","expires_in":"bar"}'); |
188
|
|
|
|
189
|
|
|
$service = new Slack( |
190
|
|
|
$this->createMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'), |
|
|
|
|
191
|
|
|
$client, |
|
|
|
|
192
|
|
|
$this->createMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') |
|
|
|
|
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
self::assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('foo')); |
196
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: