2018-08-22 17:59:14 +00:00
|
|
|
<?php
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
namespace Tests\Integration\Factories;
|
|
|
|
|
|
|
|
use App\Factories\StreamerFactory;
|
|
|
|
use App\Models\Song;
|
2021-01-31 17:49:54 +00:00
|
|
|
use App\Services\Streamers\PhpStreamer;
|
2018-08-31 13:47:15 +00:00
|
|
|
use App\Services\Streamers\S3Streamer;
|
|
|
|
use App\Services\Streamers\TranscodingStreamer;
|
|
|
|
use App\Services\Streamers\XAccelRedirectStreamer;
|
|
|
|
use App\Services\Streamers\XSendFileStreamer;
|
|
|
|
use App\Services\TranscodingService;
|
2020-12-22 23:01:49 +00:00
|
|
|
use Mockery;
|
2018-08-31 13:47:15 +00:00
|
|
|
use phpmock\mockery\PHPMockery;
|
2018-08-31 13:47:45 +00:00
|
|
|
use Tests\TestCase;
|
2018-08-31 13:47:15 +00:00
|
|
|
|
2024-01-11 12:41:33 +00:00
|
|
|
use function Tests\test_path;
|
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
class StreamerFactoryTest extends TestCase
|
|
|
|
{
|
2021-06-05 10:47:56 +00:00
|
|
|
private StreamerFactory $streamerFactory;
|
2018-08-31 13:47:15 +00:00
|
|
|
|
2019-07-22 07:03:23 +00:00
|
|
|
public function setUp(): void
|
2018-08-23 06:58:43 +00:00
|
|
|
{
|
2018-08-31 13:47:15 +00:00
|
|
|
parent::setUp();
|
2020-12-22 20:11:22 +00:00
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
$this->streamerFactory = app(StreamerFactory::class);
|
|
|
|
PHPMockery::mock('App\Services\Streamers', 'file_exists')->andReturn(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateS3Streamer(): void
|
|
|
|
{
|
|
|
|
/** @var Song $song */
|
2020-11-14 16:57:25 +00:00
|
|
|
$song = Song::factory()->make(['path' => 's3://bucket/foo.mp3']);
|
2018-08-31 13:47:15 +00:00
|
|
|
|
|
|
|
self::assertInstanceOf(S3Streamer::class, $this->streamerFactory->createStreamer($song));
|
2018-08-22 19:13:45 +00:00
|
|
|
}
|
2018-08-22 17:59:14 +00:00
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
public function testCreateTranscodingStreamerIfSupported(): void
|
2018-08-22 17:59:14 +00:00
|
|
|
{
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->swap(TranscodingService::class, Mockery::mock(TranscodingService::class, [
|
2018-08-31 13:47:15 +00:00
|
|
|
'songShouldBeTranscoded' => true,
|
2020-12-22 23:01:49 +00:00
|
|
|
]));
|
2018-08-31 13:47:15 +00:00
|
|
|
|
|
|
|
/** @var StreamerFactory $streamerFactory */
|
|
|
|
$streamerFactory = app(StreamerFactory::class);
|
2020-11-14 16:57:25 +00:00
|
|
|
|
2018-08-31 13:47:15 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 00:47:09 +00:00
|
|
|
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
|
2020-12-22 20:11:22 +00:00
|
|
|
self::assertInstanceOf(TranscodingStreamer::class, $streamerFactory->createStreamer($song));
|
2018-08-31 13:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateTranscodingStreamerIfForced(): void
|
|
|
|
{
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->swap(TranscodingService::class, Mockery::mock(TranscodingService::class, [
|
2018-08-31 13:47:15 +00:00
|
|
|
'songShouldBeTranscoded' => false,
|
2020-12-22 23:01:49 +00:00
|
|
|
]));
|
2018-08-31 13:47:15 +00:00
|
|
|
|
|
|
|
/** @var StreamerFactory $streamerFactory */
|
|
|
|
$streamerFactory = app(StreamerFactory::class);
|
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 00:47:09 +00:00
|
|
|
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
|
2018-08-31 13:47:15 +00:00
|
|
|
self::assertInstanceOf(TranscodingStreamer::class, $streamerFactory->createStreamer($song, true));
|
|
|
|
}
|
|
|
|
|
2020-12-22 20:11:22 +00:00
|
|
|
/** @return array<mixed> */
|
2018-08-31 13:47:15 +00:00
|
|
|
public function provideStreamingConfigData(): array
|
|
|
|
{
|
|
|
|
return [
|
2021-01-31 17:49:54 +00:00
|
|
|
[null, PhpStreamer::class],
|
2018-08-31 13:47:15 +00:00
|
|
|
['x-sendfile', XSendFileStreamer::class],
|
|
|
|
['x-accel-redirect', XAccelRedirectStreamer::class],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideStreamingConfigData
|
|
|
|
*
|
|
|
|
* @param string|null $config
|
2020-12-22 20:11:22 +00:00
|
|
|
* @param string $expectedClass
|
2018-08-31 13:47:15 +00:00
|
|
|
*/
|
2021-01-31 17:49:54 +00:00
|
|
|
public function testCreatePhpStreamer($config, $expectedClass): void
|
2018-08-31 13:47:15 +00:00
|
|
|
{
|
2020-12-22 23:01:49 +00:00
|
|
|
$this->swap(TranscodingService::class, Mockery::mock(TranscodingService::class, [
|
2018-08-31 13:47:15 +00:00
|
|
|
'songShouldBeTranscoded' => false,
|
2020-12-22 23:01:49 +00:00
|
|
|
]));
|
2018-08-31 13:47:15 +00:00
|
|
|
|
|
|
|
config(['koel.streaming.method' => $config]);
|
|
|
|
|
|
|
|
/** @var StreamerFactory $streamerFactory */
|
|
|
|
$streamerFactory = app(StreamerFactory::class);
|
|
|
|
|
2021-07-26 21:21:36 +00:00
|
|
|
/** @var Song $song */
|
2024-01-10 00:47:09 +00:00
|
|
|
$song = Song::factory()->make(['path' => test_path('songs/blank.mp3')]);
|
2018-08-31 13:47:15 +00:00
|
|
|
self::assertInstanceOf($expectedClass, $streamerFactory->createStreamer($song));
|
2018-08-22 17:59:14 +00:00
|
|
|
}
|
|
|
|
}
|