1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SpotifyApiConnectTest\Integration\Application\SpotifyWebApiPhp; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SpotifyApiConnect\Application\SpotifyWebApiPhp\Session; |
9
|
|
|
use SpotifyApiConnect\Application\SpotifyWebApiPhp\SpotifyWebApi; |
10
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\DeleteTrackInfoDataProvider; |
11
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\TrackSearchRequestDataProvider; |
12
|
|
|
use SpotifyApiConnect\Domain\Exception\PlaylistNotFound; |
13
|
|
|
use SpotifyApiConnect\Domain\Model\Config; |
14
|
|
|
use SpotifyWebAPI\SpotifyWebAPIException; |
15
|
|
|
|
16
|
|
|
class SpotifyWebApiTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
private $accessToken; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var SpotifyWebApi; |
25
|
|
|
*/ |
26
|
|
|
private $spotifyWebApi; |
27
|
|
|
|
28
|
|
|
private const spotifyInfo = [ |
29
|
|
|
'user' => 'fenix0', |
30
|
|
|
'playlistId' => '5tlDaMgGUWqamYp5JVoNua', |
31
|
|
|
'playlistName' => 'UnitTest' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private const spotifySong = [ |
35
|
|
|
'artist' => 'sting', |
36
|
|
|
'track' => 'englishman in new york', |
37
|
|
|
'trackId' => '0k9un4VZY52tvtxNhg6XLo' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
protected function setUp() |
41
|
|
|
{ |
42
|
|
|
parent::setUp(); |
43
|
|
|
if ($this->accessToken === null) { |
44
|
|
|
$config = new Config( |
45
|
|
|
getenv('CLIENT_ID'), |
46
|
|
|
getenv('CLIENT_SECRET'), |
47
|
|
|
getenv('REDIRECT_URI') |
48
|
|
|
); |
49
|
|
|
$session = new Session( |
50
|
|
|
$config |
51
|
|
|
); |
52
|
|
|
$session->refreshAccessToken(getenv('REFRESH_TOKEN')); |
53
|
|
|
$this->accessToken = $session->getAccessToken(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$this->spotifyWebApi = new SpotifyWebApi( |
57
|
|
|
$this->accessToken |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
public function testGetUserPlaylists(): void |
63
|
|
|
{ |
64
|
|
|
$spotifyPlaylist = $this->spotifyWebApi->getUserPlaylistByName( |
65
|
|
|
static::spotifyInfo['user'], |
66
|
|
|
static::spotifyInfo['playlistName'] |
67
|
|
|
); |
68
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlaylist->getId()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testGetUserPlaylistsNotFoundUser(): void |
72
|
|
|
{ |
73
|
|
|
$this->expectException(SpotifyWebAPIException::class); |
74
|
|
|
$this->spotifyWebApi->getUserPlaylistByName('no-existUser_For-Unit-ttest', 'unitPlaylist'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetUserPlaylistsNotFoundPlayList(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectException(PlaylistNotFound::class); |
80
|
|
|
$this->spotifyWebApi->getUserPlaylistByName(static::spotifyInfo['user'], 'uniTest-Not_FOUND-Playlist'); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetPlaylist(): void |
84
|
|
|
{ |
85
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylist( |
86
|
|
|
static::spotifyInfo['playlistId'] |
87
|
|
|
); |
88
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlayList->getId()); |
89
|
|
|
$this->assertSame(static::spotifyInfo['playlistName'], $spotifyPlayList->getName()); |
90
|
|
|
$this->assertTrue($spotifyPlayList->getPublic()); |
91
|
|
|
$this->assertSame('U2', $spotifyPlayList->getTracks()->getItems()[0]->getTrack()->getArtists()[0]->getName()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testGetNoExistUserPlaylist(): void |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
|
|
$this->spotifyWebApi->getPlaylist( |
98
|
|
|
'no-exist-PlAyList' |
99
|
|
|
); |
100
|
|
|
} catch (SpotifyWebAPIException $e) { |
101
|
|
|
$this->assertSame( |
102
|
|
|
'Invalid playlist Id', |
103
|
|
|
trim($e->getMessage()) |
104
|
|
|
); |
105
|
|
|
return; |
106
|
|
|
} |
107
|
|
|
$this->fail(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testSearchTrack() |
111
|
|
|
{ |
112
|
|
|
$trackSearchRequestDataProvider = new TrackSearchRequestDataProvider(); |
113
|
|
|
$trackSearchRequestDataProvider->setArtist( |
114
|
|
|
static::spotifySong['artist'] |
115
|
|
|
); |
116
|
|
|
$trackSearchRequestDataProvider->setTrack( |
117
|
|
|
static::spotifySong['track'] |
118
|
|
|
); |
119
|
|
|
$searchResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider); |
120
|
|
|
|
121
|
|
|
$this->assertGreaterThan(0, $searchResult->getItems()); |
122
|
|
|
$this->assertSame(strtolower(static::spotifySong['track']), strtolower($searchResult->getItems()[0]->getName(9))); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testSearchTrackNotFound() |
126
|
|
|
{ |
127
|
|
|
$trackSearchRequestDataProvider = new TrackSearchRequestDataProvider(); |
128
|
|
|
$trackSearchRequestDataProvider->setArtist( |
129
|
|
|
'NotFOundArtistForUnitTest' |
130
|
|
|
); |
131
|
|
|
$trackSearchRequestDataProvider->setTrack( |
132
|
|
|
'NotFound-Track-For_UNITtest' |
133
|
|
|
); |
134
|
|
|
$searchNoResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider); |
135
|
|
|
$this->assertSame(0, $searchNoResult->getTotal()); |
136
|
|
|
$this->assertEmpty($searchNoResult->getItems()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testAddPlaylistTracks() |
140
|
|
|
{ |
141
|
|
|
$addResult = $this->spotifyWebApi->addPlaylistTracks( |
142
|
|
|
static::spotifyInfo['playlistId'], |
143
|
|
|
[ |
144
|
|
|
static::spotifySong['trackId'] |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
$this->assertTrue($addResult); |
148
|
|
|
|
149
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
150
|
|
|
static::spotifyInfo['playlistId'] |
151
|
|
|
); |
152
|
|
|
|
153
|
|
|
$this->assertNotEmpty($spotifyPlayList->getItems()); |
154
|
|
|
$this->assertNotEmpty($spotifyPlayList->getTotal()); |
155
|
|
|
$this->assertSame(static::spotifySong['trackId'], $spotifyPlayList->getItems()[1]->getTrack()->getId()); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testAddPlaylistTracksNotExistTrackId() |
159
|
|
|
{ |
160
|
|
|
try { |
161
|
|
|
$this->spotifyWebApi->addPlaylistTracks( |
162
|
|
|
static::spotifyInfo['playlistId'], |
163
|
|
|
[ |
164
|
|
|
'uniTest-Not_FOUND--ID' |
165
|
|
|
] |
166
|
|
|
); |
167
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
168
|
|
|
$this->assertSame( |
169
|
|
|
'Invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
170
|
|
|
$e->getMessage() |
171
|
|
|
); |
172
|
|
|
return; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$this->fail(); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function testDeleteUserPlaylistTracks() |
179
|
|
|
{ |
180
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
181
|
|
|
static::spotifyInfo['playlistId'] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->assertCount(2, $spotifyPlayList->getItems()); |
185
|
|
|
$this->assertSame(2, $spotifyPlayList->getTotal()); |
186
|
|
|
|
187
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
188
|
|
|
$deleteTrack->setId(static::spotifySong['trackId']); |
189
|
|
|
|
190
|
|
|
$result = $this->spotifyWebApi->deletePlaylistTracks( |
191
|
|
|
static::spotifyInfo['playlistId'], |
192
|
|
|
[$deleteTrack] |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
$this->assertTrue($result); |
196
|
|
|
|
197
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
198
|
|
|
static::spotifyInfo['playlistId'] |
199
|
|
|
); |
200
|
|
|
|
201
|
|
|
$this->assertCount(1, $spotifyPlayList->getItems()); |
202
|
|
|
$this->assertSame(1, $spotifyPlayList->getTotal()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function testDeleteUserPlaylistTracksNoExistId() |
206
|
|
|
{ |
207
|
|
|
try { |
208
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
209
|
|
|
$deleteTrack->setId('uniTest-Not_FOUND--ID'); |
210
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
211
|
|
|
static::spotifyInfo['playlistId'], |
212
|
|
|
[$deleteTrack] |
213
|
|
|
); |
214
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
215
|
|
|
$this->assertSame( |
216
|
|
|
'JSON body contains an invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
217
|
|
|
$e->getMessage() |
218
|
|
|
); |
219
|
|
|
return; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$this->fail(); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testDeleteUserPlaylistTracksException() |
226
|
|
|
{ |
227
|
|
|
$this->expectException(\RuntimeException::class); |
228
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
229
|
|
|
static::spotifyInfo['playlistId'], |
230
|
|
|
['id' => 'hahaha'] |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
} |
234
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.