koel/tests/Integration/Services/LastfmServiceTest.php

235 lines
7.2 KiB
PHP
Raw Normal View History

2017-12-09 22:39:34 +00:00
<?php
namespace Tests\Integration\Services;
2017-12-09 22:39:34 +00:00
use App\Http\Integrations\Lastfm\Requests\GetAlbumInfoRequest;
use App\Http\Integrations\Lastfm\Requests\GetArtistInfoRequest;
use App\Http\Integrations\Lastfm\Requests\ScrobbleRequest;
use App\Http\Integrations\Lastfm\Requests\ToggleLoveTrackRequest;
use App\Http\Integrations\Lastfm\Requests\UpdateNowPlayingRequest;
2022-08-08 16:00:59 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
2018-08-18 13:19:40 +00:00
use App\Services\LastfmService;
use Illuminate\Support\Facades\File;
use Saloon\Http\Faking\MockResponse;
use Saloon\Laravel\Saloon;
2017-12-09 22:39:34 +00:00
use Tests\TestCase;
2024-01-11 12:41:33 +00:00
use function Tests\create_user;
use function Tests\test_path;
class LastfmServiceTest extends TestCase
2017-12-09 22:39:34 +00:00
{
2022-08-08 16:00:59 +00:00
private LastfmService $service;
public function setUp(): void
{
parent::setUp();
config([
'koel.lastfm.key' => 'key',
'koel.lastfm.secret' => 'secret',
]);
$this->service = app(LastfmService::class);
2022-08-08 16:00:59 +00:00
}
public function testGetArtistInformation(): void
{
/** @var Artist $artist */
$artist = Artist::factory()->make(['name' => 'Kamelot']);
2022-08-08 16:00:59 +00:00
Saloon::fake([
GetArtistInfoRequest::class => MockResponse::make(body: File::get(test_path('blobs/lastfm/artist.json'))),
]);
2022-08-08 16:00:59 +00:00
$info = $this->service->getArtistInformation($artist);
Saloon::assertSent(static function (GetArtistInfoRequest $request): bool {
self::assertSame([
'method' => 'artist.getInfo',
'artist' => 'Kamelot',
'autocorrect' => 1,
'format' => 'json',
], $request->query()->all());
return true;
});
2022-08-08 16:00:59 +00:00
self::assertEquals([
'url' => 'https://www.last.fm/music/Kamelot',
'image' => null,
'bio' => [
'summary' => 'Quisque ut nisi.',
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
],
], $info->toArray());
}
public function testGetArtistInformationForNonExistentArtist(): void
2017-12-09 22:39:34 +00:00
{
2022-08-08 16:00:59 +00:00
/** @var Artist $artist */
$artist = Artist::factory()->make(['name' => 'bar']);
Saloon::fake([
GetArtistInfoRequest::class => MockResponse::make(
body: File::get(test_path('blobs/lastfm/artist-notfound.json'))
),
]);
2022-08-08 16:00:59 +00:00
self::assertNull($this->service->getArtistInformation($artist));
}
public function testGetAlbumInformation(): void
{
/** @var Album $album */
$album = Album::factory()->for(Artist::factory()->create(['name' => 'Kamelot']))->create(['name' => 'Epica']);
2017-12-09 22:39:34 +00:00
Saloon::fake([
GetAlbumInfoRequest::class => MockResponse::make(body: File::get(test_path('blobs/lastfm/album.json'))),
]);
2022-08-08 16:00:59 +00:00
$info = $this->service->getAlbumInformation($album);
2017-12-09 22:39:34 +00:00
Saloon::assertSent(static function (GetAlbumInfoRequest $request): bool {
self::assertSame([
'method' => 'album.getInfo',
'artist' => 'Kamelot',
'album' => 'Epica',
'autocorrect' => 1,
'format' => 'json',
], $request->query()->all());
return true;
});
2020-09-06 18:21:39 +00:00
self::assertEquals([
2022-08-08 16:00:59 +00:00
'url' => 'https://www.last.fm/music/Kamelot/Epica',
'cover' => null,
'tracks' => [
[
'title' => 'Track 1',
'url' => 'https://foo/track1',
'length' => 100,
],
[
'title' => 'Track 2',
'url' => 'https://foo/track2',
'length' => 150,
],
],
'wiki' => [
'summary' => 'Quisque ut nisi.',
'full' => 'Quisque ut nisi. Vestibulum ullamcorper mauris at ligula.',
],
], $info->toArray());
}
public function testGetAlbumInformationForNonExistentAlbum(): void
{
/** @var Album $album */
$album = Album::factory()->for(Artist::factory()->create(['name' => 'Kamelot']))->create(['name' => 'Foo']);
2022-08-08 16:00:59 +00:00
Saloon::fake([
GetAlbumInfoRequest::class => MockResponse::make(
body: File::get(test_path('blobs/lastfm/album-notfound.json'))
),
]);
2022-08-08 16:00:59 +00:00
self::assertNull($this->service->getAlbumInformation($album));
}
public function testScrobble(): void
{
2024-01-11 12:41:33 +00:00
$user = create_user([
2022-08-08 16:00:59 +00:00
'preferences' => [
'lastfm_session_key' => 'my_key',
],
]);
/** @var Song $song */
$song = Song::factory()->create();
Saloon::fake([ScrobbleRequest::class => MockResponse::make()]);
$this->service->scrobble($song, $user, 100);
Saloon::assertSent(static function (ScrobbleRequest $request) use ($song): bool {
self::assertSame([
'method' => 'track.scrobble',
2022-08-08 16:00:59 +00:00
'artist' => $song->artist->name,
'track' => $song->title,
'timestamp' => 100,
'sk' => 'my_key',
'album' => $song->album->name,
], $request->body()->all());
return true;
});
2022-08-08 16:00:59 +00:00
}
/** @return array<mixed> */
public function provideToggleLoveTrackData(): array
{
return [[true, 'track.love'], [false, 'track.unlove']];
}
/** @dataProvider provideToggleLoveTrackData */
public function testToggleLoveTrack(bool $love, string $method): void
{
2024-01-11 12:41:33 +00:00
$user = create_user([
2022-08-08 16:00:59 +00:00
'preferences' => [
'lastfm_session_key' => 'my_key',
],
]);
/** @var Song $song */
$song = Song::factory()->create();
2022-08-08 16:00:59 +00:00
Saloon::fake([ToggleLoveTrackRequest::class => MockResponse::make()]);
$this->service->toggleLoveTrack($song, $user, $love);
Saloon::assertSent(static function (ToggleLoveTrackRequest $request) use ($song, $love): bool {
self::assertSame([
'method' => $love ? 'track.love' : 'track.unlove',
'sk' => 'my_key',
'artist' => $song->artist->name,
'track' => $song->title,
], $request->body()->all());
return true;
});
2022-08-08 16:00:59 +00:00
}
public function testUpdateNowPlaying(): void
{
2024-01-11 12:41:33 +00:00
$user = create_user([
2022-08-08 16:00:59 +00:00
'preferences' => [
'lastfm_session_key' => 'my_key',
],
]);
/** @var Song $song */
$song = Song::factory()->for(Artist::factory()->create(['name' => 'foo']))->create(['title' => 'bar']);
Saloon::fake([UpdateNowPlayingRequest::class => MockResponse::make()]);
$this->service->updateNowPlaying($song, $user);
Saloon::assertSent(static function (UpdateNowPlayingRequest $request) use ($song): bool {
self::assertSame([
'method' => 'track.updateNowPlaying',
'artist' => $song->artist->name,
'track' => $song->title,
2022-08-08 16:00:59 +00:00
'duration' => $song->length,
'sk' => 'my_key',
'album' => $song->album->name,
], $request->body()->all());
return true;
});
2017-12-09 22:39:34 +00:00
}
}