koel/tests/Unit/Services/LastfmServiceTest.php

199 lines
6.1 KiB
PHP
Raw Normal View History

2017-12-09 22:39:34 +00:00
<?php
namespace Tests\Unit\Services;
2022-08-08 16:00:59 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Models\Song;
use App\Services\ApiClients\LastfmClient;
2018-08-18 13:19:40 +00:00
use App\Services\LastfmService;
use Illuminate\Support\Facades\File;
use Mockery;
2022-08-08 16:00:59 +00:00
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
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 LastfmClient|MockInterface|LegacyMockInterface $client;
private LastfmService $service;
public function setUp(): void
{
parent::setUp();
config([
'koel.lastfm.key' => 'key',
'koel.lastfm.secret' => 'secret',
]);
$this->client = Mockery::mock(LastfmClient::class);
$this->service = new LastfmService($this->client);
}
public function testGetArtistInformation(): void
{
/** @var Artist $artist */
$artist = Artist::factory()->make(['name' => 'foo']);
$this->client->shouldReceive('get')
->with('?method=artist.getInfo&autocorrect=1&artist=foo&format=json')
->once()
->andReturn(json_decode(File::get(test_path('blobs/lastfm/artist.json'))));
2022-08-08 16:00:59 +00:00
$info = $this->service->getArtistInformation($artist);
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']);
$this->client->shouldReceive('get')
->with('?method=artist.getInfo&autocorrect=1&artist=bar&format=json')
->once()
->andReturn(json_decode(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' => 'bar']))->create(['name' => 'foo']);
2017-12-09 22:39:34 +00:00
2022-08-08 16:00:59 +00:00
$this->client->shouldReceive('get')
->with('?method=album.getInfo&autocorrect=1&album=foo&artist=bar&format=json')
->once()
->andReturn(json_decode(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
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' => 'bar']))->create(['name' => 'foo']);
$this->client->shouldReceive('get')
->with('?method=album.getInfo&autocorrect=1&album=foo&artist=bar&format=json')
->once()
->andReturn(json_decode(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();
$this->client->shouldReceive('post')
2022-08-08 16:00:59 +00:00
->with('/', [
'artist' => $song->artist->name,
'track' => $song->title,
'timestamp' => 100,
'sk' => 'my_key',
'method' => 'track.scrobble',
'album' => $song->album->name,
], false)
->once();
$this->service->scrobble($song, $user, 100);
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()->for(Artist::factory()->create(['name' => 'foo']))->create(['title' => 'bar']);
$this->client->shouldReceive('post')
2022-08-08 16:00:59 +00:00
->with('/', [
'artist' => 'foo',
'track' => 'bar',
'sk' => 'my_key',
'method' => $method,
], false)
->once();
$this->service->toggleLoveTrack($song, $user, $love);
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']);
$this->client->shouldReceive('post')
2022-08-08 16:00:59 +00:00
->with('/', [
'artist' => 'foo',
'track' => 'bar',
'duration' => $song->length,
'sk' => 'my_key',
'method' => 'track.updateNowPlaying',
'album' => $song->album->name,
], false)
->once();
$this->service->updateNowPlaying($song, $user);
2017-12-09 22:39:34 +00:00
}
}