Skip to content

A self-hosted media server and file management system

feat: subtitle controller test

aminnausine9a485d6f0f3
diff --git a/app/Models/Subtitle.php b/app/Models/Subtitle.php
index eb23a127..26a736c2 100644
--- a/app/Models/Subtitle.php
+++ b/app/Models/Subtitle.php
@@ -3,10 +3,13 @@
namespace App\Models;
use App\Services\Subtitles\SubtitlePath;
+use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Subtitle extends Model {
+ use HasFactory;
+
protected $fillable = [
'track_id',
'language',
diff --git a/database/factories/MetadataFactory.php b/database/factories/MetadataFactory.php
index cdb96f2a..01071550 100644
--- a/database/factories/MetadataFactory.php
+++ b/database/factories/MetadataFactory.php
@@ -3,6 +3,7 @@
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
+use Illuminate\Support\Str;
/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Metadata>
@@ -20,6 +21,7 @@ class MetadataFactory extends Factory {
'description' => 'Fake Artist - something',
'album' => $this->faker->word,
'duration' => $this->faker->numberBetween(60, 500),
+ 'uuid' => Str::uuid()->toString(),
];
}
}
diff --git a/database/factories/SubtitleFactory.php b/database/factories/SubtitleFactory.php
new file mode 100644
index 00000000..b9fbb2c0
--- /dev/null
+++ b/database/factories/SubtitleFactory.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Database\Factories;
+
+use Illuminate\Database\Eloquent\Factories\Factory;
+
+/**
+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Subtitle>
+ */
+class SubtitleFactory extends Factory {
+ /**
+ * Define the model's default state.
+ *
+ * @return array<string, mixed>
+ */
+ public function definition(): array {
+ return [
+ 'language' => 'eng',
+ ];
+ }
+}
diff --git a/tests/Feature/Subtitles/SubtitleControllerTest.php b/tests/Feature/Subtitles/SubtitleControllerTest.php
new file mode 100644
index 00000000..5ec47366
--- /dev/null
+++ b/tests/Feature/Subtitles/SubtitleControllerTest.php
@@ -0,0 +1,64 @@
+<?php
+
+namespace Tests\Feature\Subtitles;
+
+use App\Models\Metadata;
+use App\Models\Subtitle;
+use App\Services\Subtitles\SubtitleExtractor;
+use App\Services\Subtitles\SubtitleFormatter;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+class SubtitleControllerTest extends TestCase {
+ protected function setUp(): void {
+ parent::setUp();
+
+ $this->mock(SubtitleExtractor::class)->shouldIgnoreMissing();
+ $this->mock(SubtitleFormatter::class)->shouldIgnoreMissing();
+ }
+
+ public function test_returns_subtitle_file() {
+ $metadata = $this->makeMetadataAndSubtitlePair();
+ $response = $this->get("/data/subtitles/{$metadata->uuid}/2.vtt");
+
+ $response->assertOk();
+ $this->assertStringContainsString('text/vtt', $response->headers->get('Content-Type'));
+ }
+
+ public function test_default_format_is_used_when_missing() {
+ $metadata = $this->makeMetadataAndSubtitlePair();
+ $response = $this->get("/data/subtitles/{$metadata->uuid}/2");
+
+ $response->assertOk();
+ $this->assertStringContainsString('text/vtt', $response->headers->get('Content-Type'));
+ }
+
+ public function test_returns_404_for_missing_subtitle() {
+ $metadata = Metadata::factory()->create();
+ $response = $this->get("/data/subtitles/{$metadata->uuid}/999.vtt");
+
+ $response->assertNotFound();
+ }
+
+ public function test_invalid_uuid_returns_404() {
+ $response = $this->get('/data/subtitles/not-a-uuid/2.vtt');
+
+ $response->assertNotFound();
+ }
+
+ private function makeMetadataAndSubtitlePair(): Metadata {
+ $metadata = Metadata::factory()->create();
+ Subtitle::factory()->create([
+ 'metadata_uuid' => $metadata->uuid,
+ 'track_id' => 2,
+ ]);
+
+ Storage::fake('local');
+ Storage::disk('local')->put(
+ 'metadata/media/' . substr($metadata->uuid, 0, 2) . "/{$metadata->uuid}/subtitles/2.vtt",
+ "WEBVTT\n\n00:09.443 --> 00:10.844\nARE YOU READY, KIDS?\n\n00:10.911 --> 00:12.746\nKids:\nAYE AYE, CAPTAIN!"
+ );
+
+ return $metadata;
+ }
+}