From 57508a5f5a1cc614f2371d6e8f175851bcdf6936 Mon Sep 17 00:00:00 2001 From: Oliver Vogel Date: Sun, 6 Oct 2024 17:15:16 +0200 Subject: [PATCH] Add new method Format::fileExtension() --- src/Format.php | 12 ++++++ tests/Unit/FormatTest.php | 90 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/Format.php b/src/Format.php index a9aa090a..b3040d1d 100644 --- a/src/Format.php +++ b/src/Format.php @@ -115,6 +115,18 @@ public function fileExtensions(): array }); } + /** + * Return the first found file extension for the current format + * + * @return FileExtension + */ + public function fileExtension(): FileExtension + { + $extensions = $this->fileExtensions(); + + return reset($extensions); + } + /** * Create an encoder instance with given options that matches the format * diff --git a/tests/Unit/FormatTest.php b/tests/Unit/FormatTest.php index 711b1ebb..e578397b 100644 --- a/tests/Unit/FormatTest.php +++ b/tests/Unit/FormatTest.php @@ -193,4 +193,94 @@ public function testEncoderHeic(): void $format = Format::HEIC; $this->assertInstanceOf(HeicEncoder::class, $format->encoder()); } + + public function testFileExtensionsJpeg(): void + { + $format = Format::JPEG; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(2, $extensions); + + $this->assertEquals(FileExtension::JPG, $format->fileExtension()); + } + + public function testFileExtensionsWebp(): void + { + $format = Format::WEBP; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(1, $extensions); + + $this->assertEquals(FileExtension::WEBP, $format->fileExtension()); + } + + public function testFileExtensionsGif(): void + { + $format = Format::GIF; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(1, $extensions); + + $this->assertEquals(FileExtension::GIF, $format->fileExtension()); + } + + public function testFileExtensionsPng(): void + { + $format = Format::PNG; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(1, $extensions); + + $this->assertEquals(FileExtension::PNG, $format->fileExtension()); + } + + public function testFileExtensionsAvif(): void + { + $format = Format::AVIF; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(1, $extensions); + + $this->assertEquals(FileExtension::AVIF, $format->fileExtension()); + } + + public function testFileExtensionsBmp(): void + { + $format = Format::BMP; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(1, $extensions); + + $this->assertEquals(FileExtension::BMP, $format->fileExtension()); + } + + public function testFileExtensionsTiff(): void + { + $format = Format::TIFF; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(2, $extensions); + + $this->assertEquals(FileExtension::TIF, $format->fileExtension()); + } + + public function testFileExtensionsJp2(): void + { + $format = Format::JP2; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(8, $extensions); + + $this->assertEquals(FileExtension::JP2, $format->fileExtension()); + } + + public function testFileExtensionsHeic(): void + { + $format = Format::HEIC; + $extensions = $format->fileExtensions(); + $this->assertIsArray($extensions); + $this->assertCount(2, $extensions); + + $this->assertEquals(FileExtension::HEIC, $format->fileExtension()); + } }