Skip to content

Commit

Permalink
Add methods for adding album and tracks to library/favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
paulradt committed Aug 19, 2020
1 parent bdfd04b commit 9903c37
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/DeezerAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,43 @@ public function addArtistToFavorites($artistId)
return $this->client->apiRequest('POST', 'user/me/artists', [], sprintf('artist_id=%s', $artistId));
}


/**
* Add an album to the user's library.
*
* @param string|int $albumId
*
* @return array|object
*(
* @throws DeezerAPIException
*/
public function addAlbumToLibrary($albumId)
{
if (empty($albumId)) {
throw new DeezerAPIException('Album library: invalid albumId');
}

return $this->client->apiRequest('POST', 'user/me/albums', [], sprintf('album_id=%s', $albumId));
}

/**
* Add a track to the user's favorites.
*
* @param string|int $trackId
*
* @return array|object
*(
* @throws DeezerAPIException
*/
public function addTrackToFavorites($trackId)
{
if (empty($trackId)) {
throw new DeezerAPIException('Track favorites: invalid trackId');
}

return $this->client->apiRequest('POST', 'user/me/tracks', [], sprintf('track_id=%s', $trackId));
}

/**
* Follow user.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/DeezerAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,52 @@ public function testArtistToFavorites(): void
self::assertEquals('{}', $this->deezerApi->addArtistToFavorites('artist'));
}

/**
* @throws DeezerAPIException
*/
public function testAlbumToLibraryEmptyAlbumId(): void
{
$this->expectException(DeezerAPIException::class);
$this->expectExceptionMessage('Album library: invalid albumId');
$this->deezerApi->addAlbumToLibrary('');
}

/**
* @throws DeezerAPIException
*/
public function testAlbumToLibrary(): void
{
$this->client->expects(static::once())
->method('apiRequest')
->with('POST', 'user/me/albums', [], 'album_id=789')
->willReturn('{}');

self::assertEquals('{}', $this->deezerApi->addAlbumToLibrary('789'));
}

/**
* @throws DeezerAPIException
*/
public function testTrackToFavoritesEmptyTrackId(): void
{
$this->expectException(DeezerAPIException::class);
$this->expectExceptionMessage('Track favorites: invalid trackId');
$this->deezerApi->addTrackToFavorites('');
}

/**
* @throws DeezerAPIException
*/
public function testTrackToFavorites(): void
{
$this->client->expects(static::once())
->method('apiRequest')
->with('POST', 'user/me/tracks', [], 'track_id=12345')
->willReturn('{}');

self::assertEquals('{}', $this->deezerApi->addTrackToFavorites('12345'));
}

/**
* @throws DeezerAPIException
*/
Expand Down

0 comments on commit 9903c37

Please sign in to comment.