koel/tests/Unit/Services/MediaInformationServiceTest.php

116 lines
3.5 KiB
PHP
Raw Normal View History

2018-08-18 13:19:40 +00:00
<?php
2022-08-08 16:00:59 +00:00
namespace Tests\Unit\Services;
2018-08-18 13:19:40 +00:00
use App\Models\Album;
use App\Models\Artist;
use App\Services\Contracts\MusicEncyclopedia;
2018-08-18 13:19:40 +00:00
use App\Services\LastfmService;
use App\Services\MediaInformationService;
2022-07-18 11:00:37 +00:00
use App\Services\MediaMetadataService;
use App\Values\AlbumInformation;
use App\Values\ArtistInformation;
use Mockery;
2022-07-07 10:45:57 +00:00
use Mockery\LegacyMockInterface;
use Mockery\MockInterface;
2018-08-18 13:19:40 +00:00
use Tests\TestCase;
class MediaInformationServiceTest extends TestCase
{
2022-08-08 16:00:59 +00:00
private MusicEncyclopedia|MockInterface|LegacyMockInterface $encyclopedia;
2022-07-18 11:00:37 +00:00
private MediaMetadataService|LegacyMockInterface|MockInterface $mediaMetadataService;
2021-06-05 10:47:56 +00:00
private MediaInformationService $mediaInformationService;
2018-08-18 13:19:40 +00:00
2019-07-22 07:03:23 +00:00
public function setUp(): void
2018-08-18 13:19:40 +00:00
{
parent::setUp();
2022-08-08 16:00:59 +00:00
$this->encyclopedia = Mockery::mock(LastfmService::class);
2022-07-18 11:00:37 +00:00
$this->mediaMetadataService = Mockery::mock(MediaMetadataService::class);
2024-06-04 13:35:00 +00:00
$this->mediaInformationService = new MediaInformationService($this->encyclopedia, $this->mediaMetadataService);
2018-08-18 13:19:40 +00:00
}
2019-07-22 07:03:23 +00:00
public function testGetAlbumInformation(): void
2018-08-18 13:19:40 +00:00
{
/** @var Album $album */
$album = Album::factory()->create();
2022-07-18 11:00:37 +00:00
$info = AlbumInformation::make();
2022-08-08 16:00:59 +00:00
$this->encyclopedia
2022-07-18 11:00:37 +00:00
->shouldReceive('getAlbumInformation')
->once()
->with($album)
->andReturn($info);
$this->mediaMetadataService
->shouldNotReceive('tryDownloadAlbumCover');
2022-07-18 11:00:37 +00:00
self::assertSame($info, $this->mediaInformationService->getAlbumInformation($album));
2022-08-08 16:00:59 +00:00
self::assertNotNull(cache()->get('album.info.' . $album->id));
2022-07-18 11:00:37 +00:00
}
public function testGetAlbumInformationTriesDownloadingCover(): void
{
/** @var Album $album */
$album = Album::factory()->create(['cover' => '']);
$info = AlbumInformation::make();
2018-08-18 13:19:40 +00:00
self::assertFalse($album->has_cover);
2022-08-08 16:00:59 +00:00
$this->encyclopedia
->shouldReceive('getAlbumInformation')
2018-08-18 13:19:40 +00:00
->once()
2022-07-18 11:00:37 +00:00
->with($album)
->andReturn($info);
2018-08-18 13:19:40 +00:00
2022-07-18 11:00:37 +00:00
$this->mediaMetadataService
->shouldReceive('tryDownloadAlbumCover')
->with($album);
2018-08-18 13:19:40 +00:00
2022-07-18 11:00:37 +00:00
self::assertSame($info, $this->mediaInformationService->getAlbumInformation($album));
2018-08-18 13:19:40 +00:00
}
2019-07-22 07:03:23 +00:00
public function testGetArtistInformation(): void
2018-08-18 13:19:40 +00:00
{
/** @var Artist $artist */
$artist = Artist::factory()->create();
2022-07-18 11:00:37 +00:00
$info = ArtistInformation::make();
self::assertTrue($artist->has_image);
2022-08-08 16:00:59 +00:00
$this->encyclopedia
2022-07-18 11:00:37 +00:00
->shouldReceive('getArtistInformation')
->once()
->with($artist)
->andReturn($info);
$this->mediaMetadataService
->shouldNotReceive('tryDownloadArtistImage');
2022-07-18 11:00:37 +00:00
self::assertSame($info, $this->mediaInformationService->getArtistInformation($artist));
2022-08-08 16:00:59 +00:00
self::assertNotNull(cache()->get('artist.info.' . $artist->id));
2022-07-18 11:00:37 +00:00
}
public function testGetArtistInformationTriesDownloadingImage(): void
{
/** @var Artist $artist */
$artist = Artist::factory()->create(['image' => '']);
$info = ArtistInformation::make();
2018-08-18 13:19:40 +00:00
self::assertFalse($artist->has_image);
2022-08-08 16:00:59 +00:00
$this->encyclopedia
->shouldReceive('getArtistInformation')
2018-08-18 13:19:40 +00:00
->once()
2022-07-18 11:00:37 +00:00
->with($artist)
->andReturn($info);
2018-08-18 13:19:40 +00:00
2022-07-18 11:00:37 +00:00
$this->mediaMetadataService
->shouldReceive('tryDownloadArtistImage')
->with($artist);
2018-08-18 13:19:40 +00:00
2022-07-18 11:00:37 +00:00
self::assertSame($info, $this->mediaInformationService->getArtistInformation($artist));
2018-08-18 13:19:40 +00:00
}
}