From 65f66c8225c220c9aa935221756441fec244dff5 Mon Sep 17 00:00:00 2001 From: Paulo Castellano Date: Thu, 25 Jun 2026 11:10:57 -0300 Subject: [PATCH] test(media): cover Type classification branches and MediaItem detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add direct coverage for the centralized media-type logic: every legacy image/video extension arm of Type::fromExtension, and a new MediaItem DataTransferObject test (fromArray's extension->mime backfill — previously the only zero-coverage branch — plus is* delegation and the null-mime extension fallback). Locks the broad classification lists so a dropped format fails a test. --- .../DataTransferObjects/MediaItemTest.php | 36 +++++++++++++++++++ tests/Unit/Enums/MediaTypeTest.php | 10 ++++++ 2 files changed, 46 insertions(+) create mode 100644 tests/Unit/DataTransferObjects/MediaItemTest.php diff --git a/tests/Unit/DataTransferObjects/MediaItemTest.php b/tests/Unit/DataTransferObjects/MediaItemTest.php new file mode 100644 index 00000000..e178a702 --- /dev/null +++ b/tests/Unit/DataTransferObjects/MediaItemTest.php @@ -0,0 +1,36 @@ + 'a/b/photo.JPG'])->mime_type)->toBe('image/jpeg'); + expect(MediaItem::fromArray(['path' => 'clip.mp4'])->mime_type)->toBe('video/mp4'); + expect(MediaItem::fromArray(['path' => 'clip.mov'])->mime_type)->toBe('video/quicktime'); + expect(MediaItem::fromArray(['path' => 'deck.pdf'])->mime_type)->toBe('application/pdf'); + expect(MediaItem::fromArray(['path' => 'archive.zip'])->mime_type)->toBeNull(); +}); + +test('fromArray keeps an explicit mime type over the extension', function () { + $item = MediaItem::fromArray(['path' => 'thing.png', 'mime_type' => 'video/mp4']); + + expect($item->mime_type)->toBe('video/mp4'); +}); + +test('media item classifies its type via the Type enum', function () { + expect(MediaItem::fromArray(['path' => 'x.png'])->isImage())->toBeTrue(); + expect(MediaItem::fromArray(['path' => 'x.mp4'])->isVideo())->toBeTrue(); + expect(MediaItem::fromArray(['path' => 'x.pdf'])->isDocument())->toBeTrue(); + + $pdf = MediaItem::fromArray(['path' => 'x.pdf']); + expect($pdf->isImage())->toBeFalse(); + expect($pdf->isVideo())->toBeFalse(); +}); + +test('media item falls back to the extension when no mime is present', function () { + // A stored heic photo (not in the upload allow-list) still classifies as an image. + $item = new MediaItem(id: '1', path: 'photo.heic', url: 'https://x/p.heic', mime_type: null); + + expect($item->isImage())->toBeTrue(); +}); diff --git a/tests/Unit/Enums/MediaTypeTest.php b/tests/Unit/Enums/MediaTypeTest.php index 98ddc807..446180bb 100644 --- a/tests/Unit/Enums/MediaTypeTest.php +++ b/tests/Unit/Enums/MediaTypeTest.php @@ -86,6 +86,16 @@ expect(Type::fromExtension(null))->toBeNull(); }); +test('fromExtension covers every legacy image and video extension', function () { + foreach (['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'svg', 'heic', 'heif'] as $ext) { + expect(Type::fromExtension($ext))->toBe(Type::Image, "expected {$ext} to be an image"); + } + + foreach (['mp4', 'mov', 'avi', 'wmv', 'webm', 'mkv', 'm4v'] as $ext) { + expect(Type::fromExtension($ext))->toBe(Type::Video, "expected {$ext} to be a video"); + } +}); + test('isGif only matches the gif mime', function () { expect(Type::isGif('image/gif'))->toBeTrue(); expect(Type::isGif('image/png'))->toBeFalse();