2020-06-12 13:55:45 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services;
|
|
|
|
|
|
|
|
use App\Models\Album;
|
|
|
|
use App\Models\Artist;
|
|
|
|
use App\Services\ImageWriter;
|
|
|
|
use App\Services\MediaMetadataService;
|
2022-07-18 11:00:37 +00:00
|
|
|
use App\Services\SpotifyService;
|
2020-06-12 13:55:45 +00:00
|
|
|
use Mockery;
|
2022-07-16 22:42:29 +00:00
|
|
|
use Mockery\LegacyMockInterface;
|
|
|
|
use Mockery\MockInterface;
|
2020-06-12 13:55:45 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class MediaMetadataServiceTest extends TestCase
|
|
|
|
{
|
2022-07-18 11:00:37 +00:00
|
|
|
private LegacyMockInterface|SpotifyService|MockInterface $spotifyService;
|
2022-07-16 22:42:29 +00:00
|
|
|
private LegacyMockInterface|ImageWriter|MockInterface $imageWriter;
|
2022-07-18 11:00:37 +00:00
|
|
|
private MediaMetadataService $mediaMetadataService;
|
2020-06-12 13:55:45 +00:00
|
|
|
|
|
|
|
public function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
$this->spotifyService = Mockery::mock(SpotifyService::class);
|
2020-06-12 13:55:45 +00:00
|
|
|
$this->imageWriter = Mockery::mock(ImageWriter::class);
|
2022-07-18 11:00:37 +00:00
|
|
|
|
2022-08-08 16:00:59 +00:00
|
|
|
$this->mediaMetadataService = new MediaMetadataService($this->spotifyService, $this->imageWriter);
|
2022-07-18 11:00:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testTryDownloadAlbumCover(): void
|
|
|
|
{
|
|
|
|
/** @var Album $album */
|
|
|
|
$album = Album::factory()->create(['cover' => '']);
|
|
|
|
|
|
|
|
$this->spotifyService
|
|
|
|
->shouldReceive('tryGetAlbumCover')
|
|
|
|
->with($album)
|
|
|
|
->andReturn('/dev/null/cover.jpg');
|
|
|
|
|
2023-04-17 19:45:43 +00:00
|
|
|
$this->imageWriter->shouldReceive('write')->twice();
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
$this->mediaMetadataService->tryDownloadAlbumCover($album);
|
2020-06-12 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testWriteAlbumCover(): void
|
|
|
|
{
|
|
|
|
/** @var Album $album */
|
2020-11-14 16:57:25 +00:00
|
|
|
$album = Album::factory()->create();
|
2020-09-12 15:01:48 +00:00
|
|
|
$coverPath = '/koel/public/img/album/foo.jpg';
|
2020-06-12 13:55:45 +00:00
|
|
|
|
|
|
|
$this->imageWriter
|
2022-07-18 11:00:37 +00:00
|
|
|
->shouldReceive('write')
|
2020-06-12 13:55:45 +00:00
|
|
|
->once()
|
2022-07-18 11:00:37 +00:00
|
|
|
->with('/koel/public/img/album/foo.jpg', 'dummy-src');
|
2020-06-12 13:55:45 +00:00
|
|
|
|
2023-04-17 19:45:43 +00:00
|
|
|
$this->imageWriter->shouldReceive('write')->once();
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
$this->mediaMetadataService->writeAlbumCover($album, 'dummy-src', 'jpg', $coverPath);
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame(album_cover_url('foo.jpg'), $album->refresh()->cover);
|
2020-06-12 13:55:45 +00:00
|
|
|
}
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
public function testTryDownloadArtistImage(): void
|
|
|
|
{
|
|
|
|
/** @var Artist $artist */
|
|
|
|
$artist = Artist::factory()->create(['image' => '']);
|
|
|
|
|
|
|
|
$this->spotifyService
|
|
|
|
->shouldReceive('tryGetArtistImage')
|
|
|
|
->with($artist)
|
|
|
|
->andReturn('/dev/null/img.jpg');
|
|
|
|
|
2023-04-17 19:45:43 +00:00
|
|
|
$this->imageWriter->shouldReceive('write')->once();
|
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
$this->mediaMetadataService->tryDownloadArtistImage($artist);
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:55:45 +00:00
|
|
|
public function testWriteArtistImage(): void
|
|
|
|
{
|
2020-06-12 15:05:18 +00:00
|
|
|
/** @var Artist $artist */
|
2020-11-14 16:57:25 +00:00
|
|
|
$artist = Artist::factory()->create();
|
2020-09-12 15:01:48 +00:00
|
|
|
$imagePath = '/koel/public/img/artist/foo.jpg';
|
2020-06-12 13:55:45 +00:00
|
|
|
|
|
|
|
$this->imageWriter
|
2022-07-18 11:00:37 +00:00
|
|
|
->shouldReceive('write')
|
2020-06-12 13:55:45 +00:00
|
|
|
->once()
|
2022-07-18 11:00:37 +00:00
|
|
|
->with('/koel/public/img/artist/foo.jpg', 'dummy-src');
|
2020-06-12 13:55:45 +00:00
|
|
|
|
2022-07-18 11:00:37 +00:00
|
|
|
$this->mediaMetadataService->writeArtistImage($artist, 'dummy-src', 'jpg', $imagePath);
|
2022-08-09 18:45:11 +00:00
|
|
|
|
2022-10-07 14:25:44 +00:00
|
|
|
self::assertSame(artist_image_url('foo.jpg'), $artist->refresh()->image);
|
2020-06-12 13:55:45 +00:00
|
|
|
}
|
|
|
|
}
|