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\Model\Config; |
11
|
|
|
use SpotifyWebAPI\SpotifyWebAPIException; |
12
|
|
|
|
13
|
|
|
class SpotifyWebApiTest extends TestCase |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $accessToken; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var SpotifyWebApi; |
22
|
|
|
*/ |
23
|
|
|
private $spotifyWebApi; |
24
|
|
|
|
25
|
|
|
private const spotifyInfo = [ |
26
|
|
|
'user' => 'fenix0', |
27
|
|
|
'playlistId' => '5tlDaMgGUWqamYp5JVoNua', |
28
|
|
|
'playlistName' => 'UnitTest' |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
private const spotifySong = [ |
32
|
|
|
'artist' => 'sting', |
33
|
|
|
'track' => 'englishman in new york', |
34
|
|
|
'trackId' => '0k9un4VZY52tvtxNhg6XLo' |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
protected function setUp() |
38
|
|
|
{ |
39
|
|
|
parent::setUp(); |
40
|
|
|
if ($this->accessToken === null) { |
41
|
|
|
$config = new Config( |
42
|
|
|
getenv('CLIENT_ID'), |
43
|
|
|
getenv('CLIENT_SECRET'), |
44
|
|
|
getenv('REDIRECT_URI') |
45
|
|
|
); |
46
|
|
|
$session = new Session( |
47
|
|
|
$config |
48
|
|
|
); |
49
|
|
|
$session->refreshAccessToken(getenv('REFRESH_TOKEN')); |
50
|
|
|
$this->accessToken = $session->getAccessToken(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->spotifyWebApi = new SpotifyWebApi(); |
54
|
|
|
$this->spotifyWebApi->setAccessToken($this->accessToken); |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function testGetUserPlaylists(): void |
61
|
|
|
{ |
62
|
|
|
$spotifyPlayLists = $this->spotifyWebApi->getUserPlaylists(static::spotifyInfo['user']); |
63
|
|
|
$playlistId = false; |
64
|
|
|
$items = (array)$spotifyPlayLists->items; |
65
|
|
|
foreach ($items as $item) { |
66
|
|
|
|
67
|
|
|
if (trim($item->name) === static::spotifyInfo['playlistName']) { |
68
|
|
|
$playlistId = $item->id; |
69
|
|
|
break; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $playlistId); |
74
|
|
|
$this->assertTrue($items > 5); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testGetUserPlaylistsNotFoundUser(): void |
78
|
|
|
{ |
79
|
|
|
$this->expectException(SpotifyWebAPIException::class); |
80
|
|
|
$spotifyPlayLists = $this->spotifyWebApi->getUserPlaylists('no-existUser_For-Unit-ttest'); |
81
|
|
|
$this->assertEmpty($spotifyPlayLists->items); |
82
|
|
|
$this->assertSame(0, $spotifyPlayLists->total); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testGetPlaylist(): void |
86
|
|
|
{ |
87
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylist( |
88
|
|
|
static::spotifyInfo['playlistId'] |
89
|
|
|
); |
90
|
|
|
$this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlayList->getId()); |
91
|
|
|
$this->assertSame(static::spotifyInfo['playlistName'], $spotifyPlayList->getName()); |
92
|
|
|
$this->assertTrue($spotifyPlayList->getPublic()); |
93
|
|
|
$this->assertSame('U2', $spotifyPlayList->getTracks()->getItems()[0]->getTrack()->getArtists()[0]->getName()); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function testGetNoExistUserPlaylist(): void |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$this->spotifyWebApi->getPlaylist( |
100
|
|
|
'no-exist-PlAyList' |
101
|
|
|
); |
102
|
|
|
} catch (SpotifyWebAPIException $e) { |
103
|
|
|
$this->assertSame( |
104
|
|
|
'Invalid playlist Id', |
105
|
|
|
trim($e->getMessage()) |
106
|
|
|
); |
107
|
|
|
return; |
108
|
|
|
} |
109
|
|
|
$this->fail(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testSearch() |
113
|
|
|
{ |
114
|
|
|
$searchResult = $this->spotifyWebApi->search( |
115
|
|
|
sprintf('track:%s artist:%s', static::spotifySong['track'], static::spotifySong['artist']), ['track'] |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
$this->assertSame(strtolower(static::spotifySong['track']), strtolower($searchResult->tracks->items[0]->name)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function testSearchNotFound() |
122
|
|
|
{ |
123
|
|
|
$searchNoResult = $this->spotifyWebApi->search( |
124
|
|
|
sprintf('track:%s artist:%s', 'NotFound-Track-For_UNITtest', 'NotFOundArtistForUnitTest'), ['track'] |
125
|
|
|
); |
126
|
|
|
$this->assertSame(0, $searchNoResult->tracks->total); |
127
|
|
|
$this->assertEmpty($searchNoResult->tracks->items); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function testAddUserPlaylistTracks() |
131
|
|
|
{ |
132
|
|
|
$addResult = $this->spotifyWebApi->addUserPlaylistTracks( |
133
|
|
|
static::spotifyInfo['user'], |
134
|
|
|
static::spotifyInfo['playlistId'], |
135
|
|
|
[ |
136
|
|
|
static::spotifySong['trackId'] |
137
|
|
|
] |
138
|
|
|
); |
139
|
|
|
$this->assertTrue($addResult); |
140
|
|
|
|
141
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
142
|
|
|
static::spotifyInfo['playlistId'] |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$this->assertNotEmpty($spotifyPlayList->getItems()); |
146
|
|
|
$this->assertNotEmpty($spotifyPlayList->getTotal()); |
147
|
|
|
$this->assertSame(static::spotifySong['trackId'], $spotifyPlayList->getItems()[1]->getTrack()->getId()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function testAddUserPlaylistTracksNotExistTrackId() |
151
|
|
|
{ |
152
|
|
|
try { |
153
|
|
|
$this->spotifyWebApi->addUserPlaylistTracks( |
154
|
|
|
static::spotifyInfo['user'], |
155
|
|
|
static::spotifyInfo['playlistId'], |
156
|
|
|
[ |
157
|
|
|
'uniTest-Not_FOUND--ID' |
158
|
|
|
] |
159
|
|
|
); |
160
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
161
|
|
|
$this->assertSame( |
162
|
|
|
'Invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
163
|
|
|
$e->getMessage() |
164
|
|
|
); |
165
|
|
|
return; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$this->fail(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function testDeleteUserPlaylistTracks() |
172
|
|
|
{ |
173
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
174
|
|
|
static::spotifyInfo['playlistId'] |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$this->assertCount(2, $spotifyPlayList->getItems()); |
178
|
|
|
$this->assertSame(2, $spotifyPlayList->getTotal()); |
179
|
|
|
|
180
|
|
|
$this->spotifyWebApi->deleteUserPlaylistTracks( |
181
|
|
|
static::spotifyInfo['user'], |
182
|
|
|
static::spotifyInfo['playlistId'], |
183
|
|
|
[ |
184
|
|
|
[ |
185
|
|
|
'id' => static::spotifySong['trackId'] |
186
|
|
|
] |
187
|
|
|
] |
188
|
|
|
); |
189
|
|
|
|
190
|
|
|
$spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks( |
191
|
|
|
static::spotifyInfo['playlistId'] |
192
|
|
|
); |
193
|
|
|
|
194
|
|
|
$this->assertCount(1, $spotifyPlayList->getItems()); |
195
|
|
|
$this->assertSame(1, $spotifyPlayList->getTotal()); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function testDeleteUserPlaylistTracksNoExistId() |
199
|
|
|
{ |
200
|
|
|
try { |
201
|
|
|
$this->spotifyWebApi->deleteUserPlaylistTracks( |
202
|
|
|
static::spotifyInfo['user'], |
203
|
|
|
static::spotifyInfo['playlistId'], |
204
|
|
|
[ |
205
|
|
|
[ |
206
|
|
|
'id' => 'uniTest-Not_FOUND--ID' |
207
|
|
|
] |
208
|
|
|
] |
209
|
|
|
); |
210
|
|
|
} catch (\SpotifyWebAPI\SpotifyWebAPIException $e) { |
211
|
|
|
$this->assertSame( |
212
|
|
|
'JSON body contains an invalid track uri: spotify:track:uniTest-Not_FOUND--ID', |
213
|
|
|
$e->getMessage() |
214
|
|
|
); |
215
|
|
|
return; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$this->fail(); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|