Skip to content

A self-hosted media server and file management system

feat(testing): subtitle formatter test

aminnausin8ce6751219f0
diff --git a/app/Services/Subtitles/SubtitleFormatter.php b/app/Services/Subtitles/SubtitleFormatter.php
index 45e2f0f3..86b03b9f 100644
--- a/app/Services/Subtitles/SubtitleFormatter.php
+++ b/app/Services/Subtitles/SubtitleFormatter.php
@@ -6,15 +6,12 @@ use App\Services\Subtitles\Formats\VttStrategy;
use Illuminate\Support\Facades\Storage;
class SubtitleFormatter {
- public function convert(string $input, string $output, string $format): string {
+ public function convert(string $input, string $output, string $outputFormat): string {
if (! Storage::disk('local')->exists($input)) {
throw new \RuntimeException("Input file not found: {$input}");
}
- $strategy = match ($format) {
- 'vtt' => new VttStrategy,
- default => throw new \InvalidArgumentException('Unsupported format'),
- };
+ $strategy = $this->matchStrategy($outputFormat);
$strategy->convert($input, $output);
@@ -24,4 +21,11 @@ class SubtitleFormatter {
return $output;
}
+
+ protected function matchStrategy(string $outputFormat) {
+ return match ($outputFormat) {
+ 'vtt' => new VttStrategy,
+ default => throw new \InvalidArgumentException('Unsupported format'),
+ };
+ }
}
diff --git a/tests/Unit/Services/Subtitles/SubtitleFormatterTest.php b/tests/Unit/Services/Subtitles/SubtitleFormatterTest.php
new file mode 100644
index 00000000..ac34f385
--- /dev/null
+++ b/tests/Unit/Services/Subtitles/SubtitleFormatterTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Tests\Unit\Services\Subtitles;
+
+use App\Services\Subtitles\SubtitleFormatter;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+class SubtitleFormatterTest extends TestCase {
+ private SubtitleFormatter $formatter;
+
+ protected function setUp(): void {
+ parent::setUp();
+ $this->formatter = new SubtitleFormatter;
+ }
+
+ public function test_throws_if_input_missing() {
+ Storage::fake('local');
+
+ $this->expectException(\RuntimeException::class);
+ $this->formatter->convert('missing.vtt', 'out.vtt', 'vtt');
+ }
+
+ public function test_throws_for_unsupported_format() {
+ Storage::fake('local');
+ Storage::disk('local')->put('in.srt', 'data');
+
+ $this->expectException(\InvalidArgumentException::class);
+ $this->formatter->convert('in.srt', 'out.foo', 'foo');
+ }
+
+ public function test_returns_output_path_on_success() {
+ Storage::fake('local');
+ Storage::disk('local')->put('in.vtt', 'data');
+
+ $result = $this->formatter->convert('in.vtt', 'out.vtt', 'vtt');
+
+ $this->assertSame('out.vtt', $result);
+ $this->assertFileExists(Storage::disk('local')->path('out.vtt'));
+ }
+}