koel/tests/Unit/Services/S3ServiceTest.php

64 lines
1.9 KiB
PHP
Raw Normal View History

2018-08-29 04:06:17 +00:00
<?php
namespace Tests\Unit\Services;
2018-08-29 04:06:17 +00:00
use App\Models\Song;
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-27 15:32:36 +00:00
private LegacyMockInterface|MockInterface|S3ClientInterface $s3Client;
private LegacyMockInterface|Cache|MockInterface $cache;
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();
2018-08-29 04:06:17 +00:00
$this->s3Client = Mockery::mock(S3ClientInterface::class);
$this->cache = Mockery::mock(Cache::class);
2022-07-27 15:32:36 +00:00
$metadataService = Mockery::mock(MediaMetadataService::class);
$songRepository = Mockery::mock(SongRepository::class);
$this->s3Service = new S3Service($this->s3Client, $this->cache, $metadataService, $songRepository);
2018-08-29 04:06:17 +00:00
}
public function testGetSongPublicUrl(): void
{
2021-07-26 21:21:36 +00:00
/** @var Song $song */
$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));
}
}