Skip to content

Commit

Permalink
Add FileExtension::mediaType() & FileExtension::mediaTypes()
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Oct 6, 2024
1 parent 57508a5 commit 20ad74e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/FileExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,24 @@ public function format(): Format
self::HEIF => Format::HEIC,
};
}

/**
* Return media types for the current format
*
* @return array<MediaType>
*/
public function mediaTypes(): array
{
return $this->format()->mediaTypes();
}

/**
* Return the first found media type for the current format
*
* @return MediaType
*/
public function mediaType(): MediaType
{
return $this->format()->mediaType();
}
}
25 changes: 25 additions & 0 deletions tests/Unit/FileExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

use Intervention\Image\FileExtension;
use Intervention\Image\Format;
use Intervention\Image\MediaType;
use Intervention\Image\Tests\BaseTestCase;
use PHPUnit\Framework\Attributes\DataProvider;

final class FileExtensionTest extends BaseTestCase
{
Expand Down Expand Up @@ -81,4 +83,27 @@ public function testFormatHeic(): void
$ext = FileExtension::HEIF;
$this->assertEquals(Format::HEIC, $ext->format());
}

#[DataProvider('mediaTypesDataProvider')]
public function testMediatypes(FileExtension $extension, int $mediaTypeCount, MediaType $mediaType): void
{
$this->assertCount($mediaTypeCount, $extension->mediaTypes());
$this->assertEquals($mediaType, $extension->mediaType());
}

public static function mediaTypesDataProvider(): array
{
return [
[FileExtension::JPEG, 4, MediaType::IMAGE_JPEG],
[FileExtension::WEBP, 2, MediaType::IMAGE_WEBP],
[FileExtension::GIF, 1, MediaType::IMAGE_GIF],
[FileExtension::PNG, 2, MediaType::IMAGE_PNG],
[FileExtension::AVIF, 2, MediaType::IMAGE_AVIF],
[FileExtension::BMP, 8, MediaType::IMAGE_BMP],
[FileExtension::TIFF, 1, MediaType::IMAGE_TIFF],
[FileExtension::TIF, 1, MediaType::IMAGE_TIFF],
[FileExtension::JP2, 3, MediaType::IMAGE_JP2],
[FileExtension::HEIC, 3, MediaType::IMAGE_HEIC],
];
}
}

0 comments on commit 20ad74e

Please sign in to comment.