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( |
123
|
|
|
strtolower(static::spotifySong['track']), |
124
|
|
|
strtolower( |
125
|
|
|
$searchResult->getItems()[0]->getName() |
126
|
|
|
) |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testSearchTrackNotFound() |
131
|
|
|
{ |
132
|
|
|
$trackSearchRequestDataProvider = new TrackSearchRequestDataProvider(); |
133
|
|
|
$trackSearchRequestDataProvider->setArtist( |
134
|
|
|
'NotFOundArtistForUnitTest' |
135
|
|
|
); |
136
|
|
|
$trackSearchRequestDataProvider->setTrack( |
137
|
|
|
'NotFound-Track-For_UNITtest' |
138
|
|
|
); |
139
|
|
|
$searchNoResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider); |
140
|
|
|
$this->assertSame(0, $searchNoResult->getTotal()); |
141
|
|
|
$this->assertEmpty($searchNoResult->getItems()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testAddPlaylistTracks() |
145
|
|
|
{ |
146
|
|
|
$addResult = $this->spotifyWebApi->addPlaylistTracks( |
147
|
|
|
static::spotifyInfo['playlistId'], |
148
|
|
|
[ |
149
|
|
|
static::spotifySong['trackId'] |
150
|
|
|
] |
151
|
|
|
); |
152
|
|
|
$this->assertTrue($addResult); |
153
|
|
|
|
154
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
155
|
|
|
static::spotifyInfo['playlistId'] |
156
|
|
|
); |
157
|
|
|
|
158
|
|
|
$this->assertNotEmpty($spotifyPlayList->getItems()); |
159
|
|
|
$this->assertNotEmpty($spotifyPlayList->getTotal()); |
160
|
|
|
$this->assertSame(static::spotifySong['trackId'], $spotifyPlayList->getItems()[1]->getTrack()->getId()); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function testAddPlaylistTracksNotExistTrackId() |
164
|
|
|
{ |
165
|
|
|
try { |
166
|
|
|
$this->spotifyWebApi->addPlaylistTracks( |
167
|
|
|
static::spotifyInfo['playlistId'], |
168
|
|
|
[ |
169
|
|
|
'uniTest-Not_FOUND--ID' |
170
|
|
|
] |
171
|
|
|
); |
172
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
173
|
|
|
$this->assertSame( |
174
|
|
|
'Invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
175
|
|
|
$e->getMessage() |
176
|
|
|
); |
177
|
|
|
return; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->fail(); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function testDeleteUserPlaylistTracks() |
184
|
|
|
{ |
185
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
186
|
|
|
static::spotifyInfo['playlistId'] |
187
|
|
|
); |
188
|
|
|
|
189
|
|
|
$this->assertCount(2, $spotifyPlayList->getItems()); |
190
|
|
|
$this->assertSame(2, $spotifyPlayList->getTotal()); |
191
|
|
|
|
192
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
193
|
|
|
$deleteTrack->setId(static::spotifySong['trackId']); |
194
|
|
|
|
195
|
|
|
$result = $this->spotifyWebApi->deletePlaylistTracks( |
196
|
|
|
static::spotifyInfo['playlistId'], |
197
|
|
|
[$deleteTrack] |
198
|
|
|
); |
199
|
|
|
|
200
|
|
|
$this->assertTrue($result); |
201
|
|
|
|
202
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
203
|
|
|
static::spotifyInfo['playlistId'] |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$this->assertCount(1, $spotifyPlayList->getItems()); |
207
|
|
|
$this->assertSame(1, $spotifyPlayList->getTotal()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function testDeleteUserPlaylistTracksNoExistId() |
211
|
|
|
{ |
212
|
|
|
try { |
213
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
214
|
|
|
$deleteTrack->setId('uniTest-Not_FOUND--ID'); |
215
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
216
|
|
|
static::spotifyInfo['playlistId'], |
217
|
|
|
[$deleteTrack] |
218
|
|
|
); |
219
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
220
|
|
|
$this->assertSame( |
221
|
|
|
'JSON body contains an invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
222
|
|
|
$e->getMessage() |
223
|
|
|
); |
224
|
|
|
return; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$this->fail(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function testDeleteUserPlaylistTracksException() |
231
|
|
|
{ |
232
|
|
|
$this->expectException(\RuntimeException::class); |
233
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
234
|
|
|
static::spotifyInfo['playlistId'], |
235
|
|
|
['id' => 'hahaha'] |
236
|
|
|
); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|