diff --git a/src/DeezerAPI.php b/src/DeezerAPI.php index 77e8bef..271fd25 100644 --- a/src/DeezerAPI.php +++ b/src/DeezerAPI.php @@ -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. * diff --git a/tests/DeezerAPITest.php b/tests/DeezerAPITest.php index 1c93f61..4f6798a 100644 --- a/tests/DeezerAPITest.php +++ b/tests/DeezerAPITest.php @@ -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 */