1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace SpotifyApiConnectTest\Integration\Application\SpotifyWebApiPhp; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use SpotifyApiConnect\Application\SpotifyWebApiPhp\SpotifyWebApi; |
9
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\DeleteTrackInfoDataProvider; |
10
|
|
|
use SpotifyApiConnect\Domain\DataTransferObject\TrackSearchRequestDataProvider; |
11
|
|
|
use SpotifyApiConnect\Domain\Exception\PlaylistNotFound; |
12
|
|
|
use SpotifyApiConnect\SpotifyApiConnectFactory; |
13
|
|
|
use SpotifyWebAPI\SpotifyWebAPIException; |
14
|
|
|
|
15
|
|
|
class SpotifyWebApiTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var SpotifyWebApi; |
19
|
|
|
*/ |
20
|
|
|
private $spotifyWebApi; |
21
|
|
|
|
22
|
|
|
private const spotifyInfo = [ |
23
|
|
|
'user' => 'fenix0', |
24
|
|
|
'playlistId' => '5tlDaMgGUWqamYp5JVoNua', |
25
|
|
|
'playlistName' => 'UnitTest' |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
private const spotifySong = [ |
29
|
|
|
'artist' => 'sting', |
30
|
|
|
'track' => 'englishman in new york', |
31
|
|
|
'trackId' => '0k9un4VZY52tvtxNhg6XLo' |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private const spotifySongInit = [ |
35
|
|
|
'artist' => 'U2', |
36
|
|
|
'track' => 'I Still Haven\'t Found What I\'m Looking For', |
37
|
|
|
'trackId' => '6wpGqhRvJGNNXwWlPmkMyO' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
|
41
|
|
|
protected function setUp(): void |
42
|
|
|
{ |
43
|
|
|
parent::setUp(); |
44
|
|
|
$spotifyApiConnectFactory = new SpotifyApiConnectFactory(); |
45
|
|
|
$spotifyApiAuth = $spotifyApiConnectFactory->createSpotifyApiAuth(); |
46
|
|
|
$accessToken = $spotifyApiAuth->getAccessByRefreshToken($_ENV['REFRESH_TOKEN']); |
47
|
|
|
$this->spotifyWebApi = $spotifyApiConnectFactory->createSpotifyWebApi($accessToken); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public function testGetUserPlaylists(): void |
52
|
|
|
{ |
53
|
|
|
$spotifyPlaylist = $this->spotifyWebApi->getUserPlaylistByName( |
54
|
|
|
static::spotifyInfo['playlistName'] |
55
|
|
|
); |
56
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlaylist->getId()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testGetUserPlaylistsNotFoundPlayList(): void |
60
|
|
|
{ |
61
|
|
|
$this->expectException(PlaylistNotFound::class); |
62
|
|
|
$this->spotifyWebApi->getUserPlaylistByName('uniTest-Not_FOUND-Playlist'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testAddPlaylistTracks() |
66
|
|
|
{ |
67
|
|
|
$addResult = $this->spotifyWebApi->addPlaylistTracks( |
68
|
|
|
static::spotifyInfo['playlistId'], |
69
|
|
|
[ |
70
|
|
|
static::spotifySongInit['trackId'] |
71
|
|
|
] |
72
|
|
|
); |
73
|
|
|
$this->assertTrue($addResult); |
74
|
|
|
|
75
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
76
|
|
|
static::spotifyInfo['playlistId'] |
77
|
|
|
); |
78
|
|
|
|
79
|
|
|
$this->assertNotEmpty($spotifyPlayList->getItems()); |
80
|
|
|
$this->assertNotEmpty($spotifyPlayList->getTotal()); |
81
|
|
|
$this->assertSame(static::spotifySongInit['trackId'], $spotifyPlayList->getItems()[0]->getTrack()->getId()); |
82
|
|
|
$this->assertSame(static::spotifySongInit['artist'], $spotifyPlayList->getItems()[0]->getTrack()->getArtists()[0]->getName()); |
83
|
|
|
$this->assertSame(static::spotifySongInit['track'], $spotifyPlayList->getItems()[0]->getTrack()->getName()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testAddPlaylistTracksNotExistTrackId() |
87
|
|
|
{ |
88
|
|
|
try { |
89
|
|
|
$this->spotifyWebApi->addPlaylistTracks( |
90
|
|
|
static::spotifyInfo['playlistId'], |
91
|
|
|
[ |
92
|
|
|
'uniTest-Not_FOUND--ID' |
93
|
|
|
] |
94
|
|
|
); |
95
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
96
|
|
|
$this->assertSame( |
97
|
|
|
'Invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
98
|
|
|
$e->getMessage() |
99
|
|
|
); |
100
|
|
|
return; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->fail(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testGetPlaylist(): void |
107
|
|
|
{ |
108
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylist( |
109
|
|
|
static::spotifyInfo['playlistId'] |
110
|
|
|
); |
111
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlayList->getId()); |
112
|
|
|
$this->assertSame(static::spotifyInfo['playlistName'], $spotifyPlayList->getName()); |
113
|
|
|
$this->assertTrue($spotifyPlayList->getPublic()); |
114
|
|
|
$this->assertSame(self::spotifySongInit['artist'], $spotifyPlayList->getTracks()->getItems()[0]->getTrack()->getArtists()[0]->getName()); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function testGetNoExistUserPlaylist(): void |
118
|
|
|
{ |
119
|
|
|
try { |
120
|
|
|
$this->spotifyWebApi->getPlaylist( |
121
|
|
|
'no-exist-PlAyList' |
122
|
|
|
); |
123
|
|
|
} catch (SpotifyWebAPIException $e) { |
124
|
|
|
$this->assertSame( |
125
|
|
|
'Invalid playlist Id', |
126
|
|
|
trim($e->getMessage()) |
127
|
|
|
); |
128
|
|
|
return; |
129
|
|
|
} |
130
|
|
|
$this->fail(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function testSearchTrack() |
134
|
|
|
{ |
135
|
|
|
$trackSearchRequestDataProvider = new TrackSearchRequestDataProvider(); |
136
|
|
|
$trackSearchRequestDataProvider->setArtist( |
137
|
|
|
static::spotifySong['artist'] |
138
|
|
|
); |
139
|
|
|
$trackSearchRequestDataProvider->setTrack( |
140
|
|
|
static::spotifySong['track'] |
141
|
|
|
); |
142
|
|
|
$searchResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider); |
143
|
|
|
|
144
|
|
|
$this->assertGreaterThan(0, $searchResult->getItems()); |
145
|
|
|
$this->assertSame( |
146
|
|
|
strtolower(static::spotifySong['track']), |
147
|
|
|
strtolower( |
148
|
|
|
$searchResult->getItems()[0]->getName() |
149
|
|
|
) |
150
|
|
|
); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function testSearchTrackNotFound() |
154
|
|
|
{ |
155
|
|
|
$trackSearchRequestDataProvider = new TrackSearchRequestDataProvider(); |
156
|
|
|
$trackSearchRequestDataProvider->setArtist( |
157
|
|
|
'NotFOundArtistForUnitTest' |
158
|
|
|
); |
159
|
|
|
$trackSearchRequestDataProvider->setTrack( |
160
|
|
|
'NotFound-Track-For_UNITtest' |
161
|
|
|
); |
162
|
|
|
$searchNoResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider); |
163
|
|
|
$this->assertSame(0, $searchNoResult->getTotal()); |
164
|
|
|
$this->assertEmpty($searchNoResult->getItems()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
public function testDeleteUserPlaylistTracks() |
168
|
|
|
{ |
169
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
170
|
|
|
static::spotifyInfo['playlistId'] |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
$this->assertCount(1, $spotifyPlayList->getItems()); |
174
|
|
|
$this->assertSame(1, $spotifyPlayList->getTotal()); |
175
|
|
|
|
176
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
177
|
|
|
$deleteTrack->setId(static::spotifySong['trackId']); |
178
|
|
|
|
179
|
|
|
$result = $this->spotifyWebApi->deletePlaylistTracks( |
180
|
|
|
static::spotifyInfo['playlistId'], |
181
|
|
|
[$deleteTrack] |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->assertTrue($result); |
185
|
|
|
|
186
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
187
|
|
|
static::spotifyInfo['playlistId'] |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$this->assertCount(1, $spotifyPlayList->getItems()); |
191
|
|
|
$this->assertSame(1, $spotifyPlayList->getTotal()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
public function testDeleteUserPlaylistTracksNoExistId() |
195
|
|
|
{ |
196
|
|
|
try { |
197
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
198
|
|
|
$deleteTrack->setId('uniTest-Not_FOUND--ID'); |
199
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
200
|
|
|
static::spotifyInfo['playlistId'], |
201
|
|
|
[$deleteTrack] |
202
|
|
|
); |
203
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
204
|
|
|
$this->assertSame( |
205
|
|
|
'JSON body contains an invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
206
|
|
|
$e->getMessage() |
207
|
|
|
); |
208
|
|
|
return; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
$this->fail(); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
public function testDeleteUserPlaylistTracksException() |
215
|
|
|
{ |
216
|
|
|
$this->expectException(\RuntimeException::class); |
217
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
218
|
|
|
static::spotifyInfo['playlistId'], |
219
|
|
|
['id' => 'hahaha'] |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function testDeleteAllSongsInPlaylist() |
224
|
|
|
{ |
225
|
|
|
$songsIds = include __DIR__ . '/songs_ids.php'; |
226
|
|
|
$addResult = $this->spotifyWebApi->addPlaylistTracks( |
227
|
|
|
static::spotifyInfo['playlistId'], |
228
|
|
|
$songsIds |
229
|
|
|
); |
230
|
|
|
$this->assertTrue($addResult); |
231
|
|
|
|
232
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
233
|
|
|
static::spotifyInfo['playlistId'] |
234
|
|
|
); |
235
|
|
|
|
236
|
|
|
$this->assertGreaterThan(210, $spotifyPlayList->getItems()); |
237
|
|
|
|
238
|
|
|
$deleteTracks = []; |
239
|
|
|
foreach ($spotifyPlayList->getItems() as $item) { |
240
|
|
|
$deleteTrack = new DeleteTrackInfoDataProvider(); |
241
|
|
|
$deleteTrack->setId($item->getTrack()->getId()); |
242
|
|
|
$deleteTracks[] = $deleteTrack; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$this->spotifyWebApi->deletePlaylistTracks( |
246
|
|
|
static::spotifyInfo['playlistId'], |
247
|
|
|
$deleteTracks |
248
|
|
|
); |
249
|
|
|
|
250
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
251
|
|
|
static::spotifyInfo['playlistId'] |
252
|
|
|
); |
253
|
|
|
|
254
|
|
|
$this->assertCount(0, $spotifyPlayList->getItems()); |
255
|
|
|
} |
256
|
|
|
} |
257
|
|
|
|