koel/tests/Integration/Services/Streamer/StreamerTest.php

112 lines
3.9 KiB
PHP
Raw Normal View History

2024-02-24 07:28:49 +00:00
<?php
namespace Tests\Integration\Services\Streamer;
2024-04-18 17:20:14 +00:00
use App\Enums\SongStorageType;
2024-02-24 07:28:49 +00:00
use App\Exceptions\KoelPlusRequiredException;
use App\Models\Song;
use App\Services\Streamer\Adapters\LocalStreamerAdapter;
use App\Services\Streamer\Adapters\PhpStreamerAdapter;
use App\Services\Streamer\Adapters\PodcastStreamerAdapter;
2024-02-24 07:28:49 +00:00
use App\Services\Streamer\Adapters\S3CompatibleStreamerAdapter;
use App\Services\Streamer\Adapters\TranscodingStreamerAdapter;
use App\Services\Streamer\Adapters\XAccelRedirectStreamerAdapter;
use App\Services\Streamer\Adapters\XSendFileStreamerAdapter;
use App\Services\Streamer\Streamer;
2024-04-26 13:35:26 +00:00
use Exception;
2024-02-24 07:28:49 +00:00
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Http;
2024-02-24 07:28:49 +00:00
use Tests\TestCase;
use function Tests\test_path;
class StreamerTest extends TestCase
{
public function testResolveAdapters(): void
{
// prevent real HTTP calls from being made e.g. from DropboxStorage
Http::fake();
2024-04-18 17:20:14 +00:00
collect(SongStorageType::cases())
->each(function (SongStorageType $type): void {
2024-02-24 07:28:49 +00:00
/** @var Song $song */
$song = Song::factory()->make(['storage' => $type]);
switch ($type) {
2024-04-18 17:20:14 +00:00
case SongStorageType::S3:
case SongStorageType::DROPBOX:
$this->expectException(KoelPlusRequiredException::class);
2024-02-24 07:28:49 +00:00
new Streamer($song);
break;
2024-04-18 17:20:14 +00:00
case SongStorageType::S3_LAMBDA:
2024-02-24 07:28:49 +00:00
self::assertInstanceOf(S3CompatibleStreamerAdapter::class, (new Streamer($song))->getAdapter());
break;
2024-04-18 17:20:14 +00:00
case SongStorageType::LOCAL:
2024-02-24 07:28:49 +00:00
self::assertInstanceOf(LocalStreamerAdapter::class, (new Streamer($song))->getAdapter());
break;
2024-04-26 13:35:26 +00:00
default:
throw new Exception('Storage type uncovered by tests.');
2024-02-24 07:28:49 +00:00
}
});
}
public function testResolveTranscodingAdapter(): void
{
config(['koel.streaming.transcode_flac' => true]);
File::partialMock()->shouldReceive('mimeType')->andReturn('audio/flac');
/** @var Song $song */
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
self::assertInstanceOf(TranscodingStreamerAdapter::class, (new Streamer($song))->getAdapter());
config(['koel.streaming.transcode_flac' => false]);
}
public function testForceTranscodingAdapter(): void
{
/** @var Song $song */
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
self::assertInstanceOf(
TranscodingStreamerAdapter::class,
(new Streamer($song, null, ['transcode' => true]))->getAdapter()
);
}
/** @return array<mixed> */
2024-04-18 19:27:20 +00:00
public static function provideStreamConfigData(): array
2024-02-24 07:28:49 +00:00
{
return [
PhpStreamerAdapter::class => [null, PhpStreamerAdapter::class],
XSendFileStreamerAdapter::class => ['x-sendfile', XSendFileStreamerAdapter::class],
XAccelRedirectStreamerAdapter::class => ['x-accel-redirect', XAccelRedirectStreamerAdapter::class],
];
}
/** @dataProvider provideStreamConfigData */
public function testResolveLocalAdapter(?string $config, string $expectedClass): void
{
config(['koel.streaming.method' => $config]);
/** @var Song $song */
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
self::assertInstanceOf($expectedClass, (new Streamer($song))->getAdapter());
config(['koel.streaming.method' => null]);
}
public function testResolvePodcastAdapter(): void
{
/** @var Song $song */
$song = Song::factory()->asEpisode()->create();
$streamer = new Streamer($song);
self::assertInstanceOf(PodcastStreamerAdapter::class, $streamer->getAdapter());
}
2024-02-24 07:28:49 +00:00
}