Passed
Pull Request — master (#5)
by Rafal
02:30
created

testGetUserPlaylistsNotFoundUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
    protected function setUp() : void
35
    {
36
        parent::setUp();
37
        $spotifyApiConnectFactory = new SpotifyApiConnectFactory();
38
        $spotifyApiAuth = $spotifyApiConnectFactory->createSpotifyApiAuth();
39
        $accessToken = $spotifyApiAuth->getAccessByRefreshToken(getenv('REFRESH_TOKEN'));
40
        $this->spotifyWebApi =  $spotifyApiConnectFactory->createSpotifyWebApi($accessToken);
41
    }
42
43
44
    public function testGetUserPlaylists(): void
45
    {
46
        $spotifyPlaylist = $this->spotifyWebApi->getUserPlaylistByName(
47
            static::spotifyInfo['playlistName']
48
        );
49
        $this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlaylist->getId());
50
    }
51
52
    public function testGetUserPlaylistsNotFoundPlayList(): void
53
    {
54
        $this->expectException(PlaylistNotFound::class);
55
        $this->spotifyWebApi->getUserPlaylistByName('uniTest-Not_FOUND-Playlist');
56
    }
57
58
    public function testGetPlaylist(): void
59
    {
60
        $spotifyPlayList = $this->spotifyWebApi->getPlaylist(
61
            static::spotifyInfo['playlistId']
62
        );
63
        $this->assertSame(static::spotifyInfo['playlistId'], $spotifyPlayList->getId());
64
        $this->assertSame(static::spotifyInfo['playlistName'], $spotifyPlayList->getName());
65
        $this->assertTrue($spotifyPlayList->getPublic());
66
        $this->assertSame('U2', $spotifyPlayList->getTracks()->getItems()[0]->getTrack()->getArtists()[0]->getName());
67
    }
68
69
    public function testGetNoExistUserPlaylist(): void
70
    {
71
        try {
72
            $this->spotifyWebApi->getPlaylist(
73
                'no-exist-PlAyList'
74
            );
75
        } catch (SpotifyWebAPIException $e) {
76
            $this->assertSame(
77
                'Invalid playlist Id',
78
                trim($e->getMessage())
79
            );
80
            return;
81
        }
82
        $this->fail();
83
    }
84
85
    public function testSearchTrack()
86
    {
87
        $trackSearchRequestDataProvider = new TrackSearchRequestDataProvider();
88
        $trackSearchRequestDataProvider->setArtist(
89
            static::spotifySong['artist']
90
        );
91
        $trackSearchRequestDataProvider->setTrack(
92
            static::spotifySong['track']
93
        );
94
        $searchResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider);
95
96
        $this->assertGreaterThan(0, $searchResult->getItems());
97
        $this->assertSame(
98
            strtolower(static::spotifySong['track']),
99
            strtolower(
100
                $searchResult->getItems()[0]->getName()
101
            )
102
        );
103
    }
104
105
    public function testSearchTrackNotFound()
106
    {
107
        $trackSearchRequestDataProvider = new TrackSearchRequestDataProvider();
108
        $trackSearchRequestDataProvider->setArtist(
109
            'NotFOundArtistForUnitTest'
110
        );
111
        $trackSearchRequestDataProvider->setTrack(
112
            'NotFound-Track-For_UNITtest'
113
        );
114
        $searchNoResult = $this->spotifyWebApi->searchTrack($trackSearchRequestDataProvider);
115
        $this->assertSame(0, $searchNoResult->getTotal());
116
        $this->assertEmpty($searchNoResult->getItems());
117
    }
118
119
    public function testAddPlaylistTracks()
120
    {
121
        $addResult = $this->spotifyWebApi->addPlaylistTracks(
122
            static::spotifyInfo['playlistId'],
123
            [
124
                static::spotifySong['trackId']
125
            ]
126
        );
127
        $this->assertTrue($addResult);
128
129
        $spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks(
130
            static::spotifyInfo['playlistId']
131
        );
132
133
        $this->assertNotEmpty($spotifyPlayList->getItems());
134
        $this->assertNotEmpty($spotifyPlayList->getTotal());
135
        $this->assertSame(static::spotifySong['trackId'], $spotifyPlayList->getItems()[1]->getTrack()->getId());
136
    }
137
138
    public function testAddPlaylistTracksNotExistTrackId()
139
    {
140
        try {
141
            $this->spotifyWebApi->addPlaylistTracks(
142
                static::spotifyInfo['playlistId'],
143
                [
144
                    'uniTest-Not_FOUND--ID'
145
                ]
146
            );
147
        } catch (\SpotifyWebAPI\SpotifyWebAPIException $e) {
148
            $this->assertSame(
149
                'Invalid track uri: spotify:track:uniTest-Not_FOUND--ID',
150
                $e->getMessage()
151
            );
152
            return;
153
        }
154
155
        $this->fail();
156
    }
157
158
    public function testDeleteUserPlaylistTracks()
159
    {
160
        $spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks(
161
            static::spotifyInfo['playlistId']
162
        );
163
164
        $this->assertCount(2, $spotifyPlayList->getItems());
165
        $this->assertSame(2, $spotifyPlayList->getTotal());
166
167
        $deleteTrack = new DeleteTrackInfoDataProvider();
168
        $deleteTrack->setId(static::spotifySong['trackId']);
169
170
        $result = $this->spotifyWebApi->deletePlaylistTracks(
171
            static::spotifyInfo['playlistId'],
172
            [$deleteTrack]
173
        );
174
175
        $this->assertTrue($result);
176
177
        $spotifyPlayList = $this->spotifyWebApi->getPlaylistTracks(
178
            static::spotifyInfo['playlistId']
179
        );
180
181
        $this->assertCount(1, $spotifyPlayList->getItems());
182
        $this->assertSame(1, $spotifyPlayList->getTotal());
183
    }
184
185
    public function testDeleteUserPlaylistTracksNoExistId()
186
    {
187
        try {
188
            $deleteTrack = new DeleteTrackInfoDataProvider();
189
            $deleteTrack->setId('uniTest-Not_FOUND--ID');
190
            $this->spotifyWebApi->deletePlaylistTracks(
191
                static::spotifyInfo['playlistId'],
192
                [$deleteTrack]
193
            );
194
        } catch (\SpotifyWebAPI\SpotifyWebAPIException $e) {
195
            $this->assertSame(
196
                'JSON body contains an invalid track uri: spotify:track:uniTest-Not_FOUND--ID',
197
                $e->getMessage()
198
            );
199
            return;
200
        }
201
202
        $this->fail();
203
    }
204
205
    public function testDeleteUserPlaylistTracksException()
206
    {
207
        $this->expectException(\RuntimeException::class);
208
        $this->spotifyWebApi->deletePlaylistTracks(
209
            static::spotifyInfo['playlistId'],
210
            ['id' => 'hahaha']
211
        );
212
    }
213
}
214