115 lines
4.2 KiB
PHP
115 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Unit\Service;
|
|
|
|
use App\Service\ExternalAlbumClient;
|
|
use App\Service\ExternalProviderManager;
|
|
use App\Service\LastFmClient;
|
|
use App\Service\MusicBrainzClient;
|
|
use App\Service\TidalClient;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class ExternalAlbumClientTest extends TestCase
|
|
{
|
|
public function testUnknownProviderFallsBackToLastFm(): void
|
|
{
|
|
$lastFm = $this->createMock(LastFmClient::class);
|
|
$musicBrainz = $this->createMock(MusicBrainzClient::class);
|
|
$tidal = $this->createMock(TidalClient::class);
|
|
$providerManager = $this->createMock(ExternalProviderManager::class);
|
|
|
|
$providerManager->expects(self::atLeastOnce())
|
|
->method('getEffectiveProvider')
|
|
->willReturn('unknown-provider');
|
|
|
|
$lastFm->expects(self::once())
|
|
->method('searchAlbums')
|
|
->with('radiohead', 5, 1)
|
|
->willReturn(['albums' => ['items' => []]]);
|
|
$lastFm->expects(self::once())
|
|
->method('getProviderName')
|
|
->willReturn('lastfm');
|
|
|
|
$musicBrainz->expects(self::never())->method('searchAlbums');
|
|
$tidal->expects(self::never())->method('searchAlbums');
|
|
|
|
$client = new ExternalAlbumClient($lastFm, $musicBrainz, $tidal, $providerManager);
|
|
$result = $client->searchAlbums('radiohead', 5, 1);
|
|
|
|
self::assertSame(['albums' => ['items' => []]], $result);
|
|
self::assertSame('lastfm', $client->getProviderName());
|
|
}
|
|
|
|
public function testMusicBrainzProviderRoutesCallsToMusicBrainzClient(): void
|
|
{
|
|
$lastFm = $this->createMock(LastFmClient::class);
|
|
$musicBrainz = $this->createMock(MusicBrainzClient::class);
|
|
$tidal = $this->createMock(TidalClient::class);
|
|
$providerManager = $this->createMock(ExternalProviderManager::class);
|
|
|
|
$providerManager->expects(self::atLeastOnce())
|
|
->method('getEffectiveProvider')
|
|
->willReturn('musicbrainz');
|
|
|
|
$musicBrainz->expects(self::once())
|
|
->method('searchAlbums')
|
|
->with('tool', 3, 2)
|
|
->willReturn([
|
|
'albums' => [
|
|
'items' => [
|
|
['id' => 'mbid:abc', 'name' => 'Lateralus'],
|
|
],
|
|
],
|
|
]);
|
|
$musicBrainz->expects(self::once())
|
|
->method('getProviderName')
|
|
->willReturn('musicbrainz');
|
|
|
|
$lastFm->expects(self::never())->method('searchAlbums');
|
|
$tidal->expects(self::never())->method('searchAlbums');
|
|
|
|
$client = new ExternalAlbumClient($lastFm, $musicBrainz, $tidal, $providerManager);
|
|
$result = $client->searchAlbums('tool', 3, 2);
|
|
|
|
self::assertSame('mbid:abc', $result['albums']['items'][0]['id'] ?? null);
|
|
self::assertSame('musicbrainz', $client->getProviderName());
|
|
}
|
|
|
|
public function testTidalProviderRoutesCallsToTidalClient(): void
|
|
{
|
|
$lastFm = $this->createMock(LastFmClient::class);
|
|
$musicBrainz = $this->createMock(MusicBrainzClient::class);
|
|
$tidal = $this->createMock(TidalClient::class);
|
|
$providerManager = $this->createMock(ExternalProviderManager::class);
|
|
|
|
$providerManager->expects(self::atLeastOnce())
|
|
->method('getEffectiveProvider')
|
|
->willReturn('tidal');
|
|
|
|
$tidal->expects(self::once())
|
|
->method('searchAlbums')
|
|
->with('kanye', 4, 1)
|
|
->willReturn([
|
|
'albums' => [
|
|
'items' => [
|
|
['id' => 'tidal:123', 'name' => 'The College Dropout'],
|
|
],
|
|
],
|
|
]);
|
|
$tidal->expects(self::once())
|
|
->method('getProviderName')
|
|
->willReturn('tidal');
|
|
|
|
$lastFm->expects(self::never())->method('searchAlbums');
|
|
$musicBrainz->expects(self::never())->method('searchAlbums');
|
|
|
|
$client = new ExternalAlbumClient($lastFm, $musicBrainz, $tidal, $providerManager);
|
|
$result = $client->searchAlbums('kanye', 4, 1);
|
|
|
|
self::assertSame('tidal:123', $result['albums']['items'][0]['id'] ?? null);
|
|
self::assertSame('tidal', $client->getProviderName());
|
|
}
|
|
}
|