2018-08-29 04:06:17 +00:00
|
|
|
<?php
|
|
|
|
|
2021-12-06 16:12:47 +00:00
|
|
|
namespace Tests\Unit\Services;
|
2018-08-29 04:06:17 +00:00
|
|
|
|
2024-01-15 13:32:44 +00:00
|
|
|
use App\Events\LibraryChanged;
|
2018-08-29 04:06:17 +00:00
|
|
|
use App\Models\Song;
|
2021-12-06 16:12:47 +00:00
|
|
|
use App\Repositories\SongRepository;
|
|
|
|
use App\Services\MediaMetadataService;
|
2018-08-29 04:06:17 +00:00
|
|
|
use App\Services\S3Service;
|
|
|
|
use Aws\CommandInterface;
|
|
|
|
use Aws\S3\S3ClientInterface;
|
|
|
|
use GuzzleHttp\Psr7\Request;
|
|
|
|
use Illuminate\Cache\Repository as Cache;
|
|
|
|
use Mockery;
|
2022-07-27 15:32:36 +00:00
|
|
|
use Mockery\LegacyMockInterface;
|
|
|
|
use Mockery\MockInterface;
|
2018-08-29 04:06:17 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
|
|
|
|
class S3ServiceTest extends TestCase
|
|
|
|
{
|
2022-07-29 06:47:10 +00:00
|
|
|
private S3ClientInterface|LegacyMockInterface|MockInterface $s3Client;
|
|
|
|
private Cache|LegacyMockInterface|MockInterface $cache;
|
2024-01-15 13:32:44 +00:00
|
|
|
private SongRepository|LegacyMockInterface|MockInterface $songRepository;
|
2021-06-05 10:47:56 +00:00
|
|
|
private S3Service $s3Service;
|
2018-08-29 04:06:17 +00:00
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2018-08-29 04:06:17 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2018-08-29 04:06:17 +00:00
|
|
|
$this->s3Client = Mockery::mock(S3ClientInterface::class);
|
|
|
|
$this->cache = Mockery::mock(Cache::class);
|
2021-12-06 16:12:47 +00:00
|
|
|
|
2022-07-27 15:32:36 +00:00
|
|
|
$metadataService = Mockery::mock(MediaMetadataService::class);
|
2024-01-15 13:32:44 +00:00
|
|
|
$this->songRepository = Mockery::mock(SongRepository::class);
|
2022-07-27 15:32:36 +00:00
|
|
|
|
2024-01-15 13:32:44 +00:00
|
|
|
$this->s3Service = new S3Service($this->s3Client, $this->cache, $metadataService, $this->songRepository);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateSongEntry(): void
|
|
|
|
{
|
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
|
|
|
|
$song = $this->s3Service->createSongEntry(
|
|
|
|
bucket: 'foo',
|
|
|
|
key: 'bar',
|
|
|
|
artistName: 'Queen',
|
|
|
|
albumName: 'A Night at the Opera',
|
|
|
|
albumArtistName: 'Queen',
|
|
|
|
cover: [],
|
|
|
|
title: 'Bohemian Rhapsody',
|
|
|
|
duration: 355.5,
|
|
|
|
track: 1,
|
|
|
|
lyrics: 'Is this the real life?'
|
|
|
|
);
|
|
|
|
|
|
|
|
self::assertSame('Queen', $song->artist->name);
|
|
|
|
self::assertSame('A Night at the Opera', $song->album->name);
|
|
|
|
self::assertSame('Queen', $song->album_artist->name);
|
|
|
|
self::assertSame('Bohemian Rhapsody', $song->title);
|
|
|
|
self::assertSame(355.5, $song->length);
|
|
|
|
self::assertSame('Is this the real life?', $song->lyrics);
|
|
|
|
self::assertSame(1, $song->track);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateSongEntry(): void
|
|
|
|
{
|
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create([
|
|
|
|
'path' => 's3://foo/bar',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->s3Service->createSongEntry(
|
|
|
|
bucket: 'foo',
|
|
|
|
key: 'bar',
|
|
|
|
artistName: 'Queen',
|
|
|
|
albumName: 'A Night at the Opera',
|
|
|
|
albumArtistName: 'Queen',
|
|
|
|
cover: [],
|
|
|
|
title: 'Bohemian Rhapsody',
|
|
|
|
duration: 355.5,
|
|
|
|
track: 1,
|
|
|
|
lyrics: 'Is this the real life?'
|
|
|
|
);
|
|
|
|
|
|
|
|
self::assertSame(1, Song::query()->count());
|
|
|
|
|
|
|
|
$song->refresh();
|
|
|
|
|
|
|
|
self::assertSame('Queen', $song->artist->name);
|
|
|
|
self::assertSame('A Night at the Opera', $song->album->name);
|
|
|
|
self::assertSame('Queen', $song->album_artist->name);
|
|
|
|
self::assertSame('Bohemian Rhapsody', $song->title);
|
|
|
|
self::assertSame(355.5, $song->length);
|
|
|
|
self::assertSame('Is this the real life?', $song->lyrics);
|
|
|
|
self::assertSame(1, $song->track);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteSong(): void
|
|
|
|
{
|
|
|
|
$this->expectsEvents(LibraryChanged::class);
|
|
|
|
|
|
|
|
/** @var Song $song */
|
|
|
|
$song = Song::factory()->create([
|
|
|
|
'path' => 's3://foo/bar',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->songRepository->shouldReceive('getOneByPath')
|
|
|
|
->with('s3://foo/bar')
|
|
|
|
->once()
|
|
|
|
->andReturn($song);
|
|
|
|
|
|
|
|
$this->s3Service->deleteSongEntry('foo', 'bar');
|
|
|
|
|
|
|
|
self::assertModelMissing($song);
|
2018-08-29 04:06:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSongPublicUrl(): void
|
|
|
|
{
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Song $song */
|
2020-11-14 16:57:25 +00:00
|
|
|
$song = Song::factory()->create(['path' => 's3://foo/bar']);
|
2018-08-29 04:06:17 +00:00
|
|
|
|
|
|
|
$cmd = Mockery::mock(CommandInterface::class);
|
2022-07-27 15:32:36 +00:00
|
|
|
|
2018-08-29 04:06:17 +00:00
|
|
|
$this->s3Client->shouldReceive('getCommand')
|
|
|
|
->with('GetObject', [
|
|
|
|
'Bucket' => 'foo',
|
|
|
|
'Key' => 'bar',
|
|
|
|
])
|
|
|
|
->andReturn($cmd);
|
|
|
|
|
|
|
|
$request = Mockery::mock(Request::class, ['getUri' => 'https://aws.com/foo.mp3']);
|
|
|
|
|
|
|
|
$this->s3Client->shouldReceive('createPresignedRequest')
|
|
|
|
->with($cmd, '+1 hour')
|
|
|
|
->andReturn($request);
|
|
|
|
|
|
|
|
$this->cache->shouldReceive('remember')
|
|
|
|
->once()
|
|
|
|
->andReturn('https://aws.com/foo.mp3');
|
|
|
|
|
|
|
|
self::assertSame('https://aws.com/foo.mp3', $this->s3Service->getSongPublicUrl($song));
|
|
|
|
}
|
|
|
|
}
|