koel/tests/Unit/Services/SongStorages/S3LambdaStorageTest.php

127 lines
3.8 KiB
PHP
Raw Normal View History

2018-08-29 04:06:17 +00:00
<?php
namespace Tests\Unit\Services\SongStorages;
2018-08-29 04:06:17 +00:00
use App\Models\Song;
use App\Repositories\SongRepository;
use App\Repositories\UserRepository;
use App\Services\MediaMetadataService;
use App\Services\SongStorages\S3LambdaStorage;
2018-08-29 04:06:17 +00:00
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;
use function Tests\create_admin;
2024-02-05 22:47:13 +00:00
class S3LambdaStorageTest extends TestCase
2018-08-29 04:06:17 +00:00
{
private SongRepository|LegacyMockInterface|MockInterface $songRepository;
private UserRepository|LegacyMockInterface|MockInterface $userRepository;
2024-02-05 22:47:13 +00:00
private S3LambdaStorage $storage;
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();
2022-07-27 15:32:36 +00:00
$metadataService = Mockery::mock(MediaMetadataService::class);
$this->songRepository = Mockery::mock(SongRepository::class);
$this->userRepository = Mockery::mock(UserRepository::class);
2024-02-05 22:47:13 +00:00
$this->storage = new S3LambdaStorage(
$metadataService,
$this->songRepository,
$this->userRepository
);
}
public function testCreateSongEntry(): void
{
$user = create_admin();
$this->userRepository->shouldReceive('getDefaultAdminUser')
->once()
->andReturn($user);
2024-02-05 22:47:13 +00:00
$song = $this->storage->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);
self::assertSame($user->id, $song->owner_id);
}
public function testUpdateSongEntry(): void
{
$user = create_admin();
2024-02-24 15:37:01 +00:00
$this->userRepository->shouldReceive('getDefaultAdminUser')
->once()
->andReturn($user);
/** @var Song $song */
$song = Song::factory()->create([
'path' => 's3://foo/bar',
2024-02-24 15:37:01 +00:00
'storage' => 's3-lambda',
]);
2024-02-05 22:47:13 +00:00
$this->storage->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);
self::assertSame($user->id, $song->owner_id);
}
public function testDeleteSong(): void
{
/** @var Song $song */
$song = Song::factory()->create([
'path' => 's3://foo/bar',
2024-02-24 15:37:01 +00:00
'storage' => 's3-lambda',
]);
$this->songRepository->shouldReceive('findOneByPath')
->with('s3://foo/bar')
->once()
->andReturn($song);
2024-02-05 22:47:13 +00:00
$this->storage->deleteSongEntry('foo', 'bar');
self::assertModelMissing($song);
2018-08-29 04:06:17 +00:00
}
}