mirror of
https://github.com/koel/koel
synced 2025-02-17 13:58:28 +00:00
Tests for streamer factory
This commit is contained in:
parent
eed836bcb9
commit
6cadfc5ac3
4 changed files with 129 additions and 32 deletions
|
@ -7,21 +7,25 @@ use App\Services\Streamers\DirectStreamerInterface;
|
|||
use App\Services\Streamers\ObjectStorageStreamerInterface;
|
||||
use App\Services\Streamers\StreamerInterface;
|
||||
use App\Services\Streamers\TranscodingStreamerInterface;
|
||||
use App\Services\TranscodingService;
|
||||
|
||||
class StreamerFactory
|
||||
{
|
||||
private $directStreamer;
|
||||
private $transcodingStreamer;
|
||||
private $objectStorageStreamer;
|
||||
private $transcodingService;
|
||||
|
||||
public function __construct(
|
||||
DirectStreamerInterface $directStreamer,
|
||||
TranscodingStreamerInterface $transcodingStreamer,
|
||||
ObjectStorageStreamerInterface $objectStorageStreamer
|
||||
ObjectStorageStreamerInterface $objectStorageStreamer,
|
||||
TranscodingService $transcodingService
|
||||
) {
|
||||
$this->directStreamer = $directStreamer;
|
||||
$this->transcodingStreamer = $transcodingStreamer;
|
||||
$this->objectStorageStreamer = $objectStorageStreamer;
|
||||
$this->transcodingService = $transcodingService;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,8 +44,7 @@ class StreamerFactory
|
|||
return $this->objectStorageStreamer;
|
||||
}
|
||||
|
||||
// If `transcode` parameter isn't passed, the default is to only transcode FLAC.
|
||||
if ($transcode === null && ends_with(mime_content_type($song->path), 'flac')) {
|
||||
if ($transcode === null && $this->transcodingService->songShouldBeTranscoded($song)) {
|
||||
$transcode = true;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Factories\StreamerFactory;
|
||||
use App\Services\Streamers\DirectStreamerInterface;
|
||||
use App\Services\Streamers\ObjectStorageStreamerInterface;
|
||||
use App\Services\Streamers\PHPStreamer;
|
||||
|
@ -17,25 +16,18 @@ class StreamerServiceProvider extends ServiceProvider
|
|||
{
|
||||
public function register()
|
||||
{
|
||||
$this->app->when(StreamerFactory::class)
|
||||
->needs(DirectStreamerInterface::class)
|
||||
->give(static function () {
|
||||
switch (config('koel.streaming.method')) {
|
||||
case 'x-sendfile':
|
||||
return new XSendFileStreamer();
|
||||
case 'x-accel-redirect':
|
||||
return new XAccelRedirectStreamer();
|
||||
default:
|
||||
return new PHPStreamer();
|
||||
}
|
||||
});
|
||||
$this->app->bind(DirectStreamerInterface::class, static function () {
|
||||
switch (config('koel.streaming.method')) {
|
||||
case 'x-sendfile':
|
||||
return new XSendFileStreamer();
|
||||
case 'x-accel-redirect':
|
||||
return new XAccelRedirectStreamer();
|
||||
default:
|
||||
return new PHPStreamer();
|
||||
}
|
||||
});
|
||||
|
||||
$this->app->when(StreamerFactory::class)
|
||||
->needs(TranscodingStreamerInterface::class)
|
||||
->give(TranscodingStreamer::class);
|
||||
|
||||
$this->app->when(StreamerFactory::class)
|
||||
->needs(ObjectStorageStreamerInterface::class)
|
||||
->give(S3Streamer::class);
|
||||
$this->app->bind(TranscodingStreamerInterface::class, TranscodingStreamer::class);
|
||||
$this->app->bind(ObjectStorageStreamerInterface::class, S3Streamer::class);
|
||||
}
|
||||
}
|
||||
|
|
20
app/Services/TranscodingService.php
Normal file
20
app/Services/TranscodingService.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Song;
|
||||
|
||||
class TranscodingService
|
||||
{
|
||||
/**
|
||||
* Determine if a song should be transcoded.
|
||||
*
|
||||
* @param Song $song
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function songShouldBeTranscoded(Song $song)
|
||||
{
|
||||
return ends_with(mime_content_type($song->path), 'flac');
|
||||
}
|
||||
}
|
|
@ -1,13 +1,95 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Integration\Factories;
|
||||
|
||||
use Tests\TestCase;
|
||||
|
||||
class StreamerFactoryTest extends TestCase
|
||||
{
|
||||
public function testCreate()
|
||||
{
|
||||
self::fail('Write the test you lazy ass.');
|
||||
namespace App\Services\Streamers {
|
||||
function file_exists() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Tests\Integration\Factories {
|
||||
use App\Factories\StreamerFactory;
|
||||
use App\Models\Song;
|
||||
use App\Services\Streamers\PHPStreamer;
|
||||
use App\Services\Streamers\S3Streamer;
|
||||
use App\Services\Streamers\TranscodingStreamer;
|
||||
use App\Services\Streamers\XAccelRedirectStreamer;
|
||||
use App\Services\Streamers\XSendFileStreamer;
|
||||
use App\Services\TranscodingService;
|
||||
use Tests\TestCase;
|
||||
|
||||
class StreamerFactoryTest extends TestCase
|
||||
{
|
||||
/** @var StreamerFactory */
|
||||
private $streamerFactory;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
$this->streamerFactory = app(StreamerFactory::class);
|
||||
}
|
||||
|
||||
public function testCreateS3Streamer()
|
||||
{
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->make(['path' => 's3://bucket/foo.mp3']);
|
||||
|
||||
self::assertInstanceOf(S3Streamer::class, $this->streamerFactory->createStreamer($song));
|
||||
}
|
||||
|
||||
public function testCreateTranscodingStreamerIfSupported()
|
||||
{
|
||||
$this->mockIocDependency(TranscodingService::class, [
|
||||
'songShouldBeTranscoded' => true,
|
||||
]);
|
||||
|
||||
/** @var StreamerFactory $streamerFactory */
|
||||
$streamerFactory = app(StreamerFactory::class);
|
||||
/** @var Song $song */
|
||||
$song = factory(Song::class)->make();
|
||||
self::assertInstanceOf(TranscodingStreamer::class, $streamerFactory->createStreamer($song, null));
|
||||
}
|
||||
|
||||
public function testCreateTranscodingStreamerIfForced()
|
||||
{
|
||||
$this->mockIocDependency(TranscodingService::class, [
|
||||
'songShouldBeTranscoded' => false,
|
||||
]);
|
||||
|
||||
/** @var StreamerFactory $streamerFactory */
|
||||
$streamerFactory = app(StreamerFactory::class);
|
||||
|
||||
$song = factory(Song::class)->make();
|
||||
self::assertInstanceOf(TranscodingStreamer::class, $streamerFactory->createStreamer($song, true));
|
||||
}
|
||||
|
||||
public function provideStreamingConfigData()
|
||||
{
|
||||
return [
|
||||
[null, PHPStreamer::class],
|
||||
['x-sendfile', XSendFileStreamer::class],
|
||||
['x-accel-redirect', XAccelRedirectStreamer::class],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider provideStreamingConfigData
|
||||
*
|
||||
* @param string|null $config
|
||||
* @param string $expectedClass
|
||||
*/
|
||||
public function testCreatePHPStreamer($config, $expectedClass)
|
||||
{
|
||||
$this->mockIocDependency(TranscodingService::class, [
|
||||
'songShouldBeTranscoded' => false,
|
||||
]);
|
||||
|
||||
config(['koel.streaming.method' => $config]);
|
||||
|
||||
/** @var StreamerFactory $streamerFactory */
|
||||
$streamerFactory = app(StreamerFactory::class);
|
||||
|
||||
$song = factory(Song::class)->make();
|
||||
self::assertInstanceOf($expectedClass, $streamerFactory->createStreamer($song));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue