koel/tests/Unit/Services/SpotifyServiceTest.php

95 lines
2.6 KiB
PHP
Raw Normal View History

2022-07-18 11:00:37 +00:00
<?php
namespace Tests\Unit\Services;
use App\Http\Integrations\Spotify\SpotifyClient;
2022-07-18 11:00:37 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Services\SpotifyService;
use Illuminate\Support\Facades\File;
2022-07-18 11:00:37 +00:00
use Mockery;
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
use Tests\TestCase;
2024-01-11 12:41:33 +00:00
use function Tests\test_path;
2022-07-18 11:00:37 +00:00
class SpotifyServiceTest extends TestCase
{
private SpotifyService $service;
2022-07-27 15:32:36 +00:00
private SpotifyClient|MockInterface|LegacyMockInterface $client;
2022-07-18 11:00:37 +00:00
public function setUp(): void
{
parent::setUp();
config([
'koel.spotify.client_id' => 'fake-client-id',
'koel.spotify.client_secret' => 'fake-client-secret',
]);
$this->client = Mockery::mock(SpotifyClient::class);
$this->service = new SpotifyService($this->client);
}
public function testTryGetArtistImage(): void
{
/** @var Artist $artist */
$artist = Artist::factory(['name' => 'Foo'])->create();
$this->client
->shouldReceive('search')
->with('Foo', 'artist', ['limit' => 1])
->andReturn(self::parseFixture('search-artist.json'));
self::assertSame('https://foo/bar.jpg', $this->service->tryGetArtistImage($artist));
}
public function testTryGetArtistImageWhenServiceIsNotEnabled(): void
{
config(['koel.spotify.client_id' => null]);
$this->client->shouldNotReceive('search');
2022-07-27 15:32:36 +00:00
self::assertNull($this->service->tryGetArtistImage(Mockery::mock(Artist::class)));
2022-07-18 11:00:37 +00:00
}
public function testTryGetAlbumImage(): void
{
/** @var Album $album */
2023-06-05 21:46:41 +00:00
$album = Album::factory(['name' => 'Bar'])->for(Artist::factory(['name' => 'Foo']))->create();
2022-07-18 11:00:37 +00:00
$this->client
->shouldReceive('search')
->with('Bar artist:Foo', 'album', ['limit' => 1])
->andReturn(self::parseFixture('search-album.json'));
self::assertSame('https://foo/bar.jpg', $this->service->tryGetAlbumCover($album));
}
public function testTryGetAlbumImageWhenServiceIsNotEnabled(): void
{
config(['koel.spotify.client_id' => null]);
$this->client->shouldNotReceive('search');
2022-07-27 15:32:36 +00:00
self::assertNull($this->service->tryGetAlbumCover(Mockery::mock(Album::class)));
2022-07-18 11:00:37 +00:00
}
/** @return array<mixed> */
private static function parseFixture(string $name): array
{
return json_decode(File::get(test_path("blobs/spotify/$name")), true);
2022-07-18 11:00:37 +00:00
}
protected function tearDown(): void
{
config([
'koel.spotify.client_id' => null,
'koel.spotify.client_secret' => null,
]);
parent::tearDown();
}
}