-
Notifications
You must be signed in to change notification settings - Fork 0
/
SoundCloudTest.php
181 lines (144 loc) · 5.67 KB
/
SoundCloudTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
declare(strict_types=1);
/**
* Spinnin' Platform - All rights reserved.
*/
namespace Martin1982\OAuth2\Client\Test\Provider;
use GuzzleHttp\ClientInterface as HttpClientInterface;
use League\OAuth2\Client\Token\AccessToken;
use Martin1982\OAuth2\Client\Provider\Exception\SoundCloudIdentityProviderException;
use Martin1982\OAuth2\Client\Provider\SoundCloud;
use Martin1982\OAuth2\Client\Provider\SoundCloudResourceOwner;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
class FooSoundCloudProvider extends SoundCloud
{
protected function fetchResourceOwnerDetails(AccessToken $token)
{
return json_decode(file_get_contents(__DIR__ . '/../fixtures/user.json'), true);
}
}
/**
* Class SoundCloudTest.
*
* @internal
*
* @coversNothing
*/
class SoundCloudTest extends TestCase
{
protected SoundCloud $provider;
protected function setUp(): void
{
$this->provider = new SoundCloud([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_client_secret',
'redirectUri' => 'none',
'responseType' => SoundCloud::RESPONSE_TYPE,
]);
}
public function testAuthorizationUrl(): void
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
$this->assertArrayHasKey('scope', $query);
$this->assertArrayHasKey('response_type', $query);
$this->assertNotNull($this->provider->getState());
}
public function testGetBaseAuthorizationUrl(): void
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
$this->assertSame('/connect', $uri['path']);
}
public function testGetBaseAccessTokenUrl(): void
{
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertSame('/oauth2/token', $uri['path']);
}
public function testGetResourceOwnerDetailsUrl(): void
{
$accessToken = $this->createMock(AccessToken::class);
$url = $this->provider->getResourceOwnerDetailsUrl($accessToken);
$uri = parse_url($url);
$this->assertSame('/me', $uri['path']);
}
public function testGetAccessToken(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getBody')->willReturn('{"access_token": "mock_access_token", "expires_in": 3600}');
$response->method('getHeader')->willReturn(['content-type' => 'json']);
$response->method('getStatusCode')->willReturn(200);
$client = $this->createMock(HttpClientInterface::class);
$client->method('send')->willReturn($response);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertSame('mock_access_token', $token->getToken());
$this->assertLessThanOrEqual(time() + 3600, $token->getExpires());
$this->assertGreaterThanOrEqual(time(), $token->getExpires());
$this->assertNull($token->getRefreshToken());
$this->assertNull($token->getResourceOwnerId());
}
public function testGetResourceOwner(): void
{
$provider = new FooSoundCloudProvider();
$token = $this->createMock(AccessToken::class);
/** @var SoundCloudResourceOwner $resourceOwner */
$resourceOwner = $provider->getResourceOwner($token);
$this->assertSame('0', $resourceOwner->getId());
$this->assertSame('https://image.img/avatar.png', $resourceOwner->getAvatarUrl());
$this->assertSame('NL', $resourceOwner->getCountry());
$this->assertSame('john', $resourceOwner->getFirstname());
$this->assertSame('doe', $resourceOwner->getLastname());
$this->assertSame('john doe', $resourceOwner->getFullName());
$this->assertSame('BE_nl', $resourceOwner->getLocale());
$this->assertTrue($resourceOwner->getOnline());
$this->assertSame('https://some.uri', $resourceOwner->getUri());
}
public function testCheckResponseFailureWithRegularError(): void
{
$this->expectException(SoundCloudIdentityProviderException::class);
$this->expectExceptionMessage('No data returned.');
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$data = [
'error' => [
'type' => 'DataException',
'message' => 'No data returned.',
],
];
$this->callMethod('checkResponse', [$response, $data]);
}
public function testCheckResponseFailureWithWrongCode(): void
{
$this->expectException(SoundCloudIdentityProviderException::class);
$this->expectExceptionMessage('Wrong code.');
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(200);
$data = [
'wrong_code' => '',
];
$this->callMethod('checkResponse', [$response, $data]);
}
/**
* @param mixed $name
*
* @return mixed|null
*/
protected function callMethod($name, array $args = [])
{
try {
$reflection = new \ReflectionMethod(\get_class($this->provider), $name);
$reflection->setAccessible(true);
return $reflection->invokeArgs($this->provider, $args);
} catch (\ReflectionException $e) {
return null;
}
}
}