diff --git a/src/Api/Encoder.php b/src/Api/Encoder.php index 95ff7ff..420ced9 100644 --- a/src/Api/Encoder.php +++ b/src/Api/Encoder.php @@ -110,9 +110,8 @@ public function getFormat(ImageInterface $image): string { try { $mediaType = $this->getMediaType($image); - $extensions = $mediaType->format()->fileExtensions(); - return reset($extensions)->value; // @phpstan-ignore-line + return array_search($mediaType->value, self::supportedFormats(), true) ?: 'jpg'; } catch (\Exception) { return 'jpg'; } diff --git a/tests/Api/ApiTest.php b/tests/Api/ApiTest.php index be06f9a..a95f6f9 100644 --- a/tests/Api/ApiTest.php +++ b/tests/Api/ApiTest.php @@ -14,7 +14,7 @@ class ApiTest extends TestCase { - private $api; + private Api $api; public function setUp(): void { @@ -26,30 +26,30 @@ public function tearDown(): void \Mockery::close(); } - public function testCreateInstance() + public function testCreateInstance(): void { $this->assertInstanceOf(Api::class, $this->api); } - public function testSetImageManager() + public function testSetImageManager(): void { $this->api->setImageManager(ImageManager::gd()); $this->assertInstanceOf(ImageManager::class, $this->api->getImageManager()); } - public function testGetImageManager() + public function testGetImageManager(): void { $this->assertInstanceOf(ImageManager::class, $this->api->getImageManager()); } - public function testSetManipulators() + public function testSetManipulators(): void { $this->api->setManipulators([\Mockery::mock(ManipulatorInterface::class)]); $manipulators = $this->api->getManipulators(); $this->assertInstanceOf(ManipulatorInterface::class, $manipulators[0]); } - public function testSetInvalidManipulator() + public function testSetInvalidManipulator(): void { $this->expectException(\InvalidArgumentException::class); $this->expectExceptionMessage('Not a valid manipulator.'); @@ -57,12 +57,12 @@ public function testSetInvalidManipulator() $this->api->setManipulators([new \stdClass()]); } - public function testGetManipulators() + public function testGetManipulators(): void { $this->assertEquals([], $this->api->getManipulators()); } - public function testRun() + public function testRun(): void { $image = \Mockery::mock(ImageInterface::class, function ($mock) { $mock->shouldReceive('origin')->andReturn(\Mockery::mock(Origin::class, function ($mock) { @@ -88,7 +88,7 @@ public function testRun() $api = new Api($manager, [$manipulator]); $this->assertEquals('encoded', $api->run( - file_get_contents(dirname(__FILE__, 2).'/files/red-pixel.png'), + (string) file_get_contents(dirname(__FILE__, 2).'/files/red-pixel.png'), [] )); } diff --git a/tests/Api/EncoderTest.php b/tests/Api/EncoderTest.php index 492caa7..78b9e8e 100644 --- a/tests/Api/EncoderTest.php +++ b/tests/Api/EncoderTest.php @@ -130,12 +130,14 @@ public function testGetFormat(): void $this->assertSame('png', $this->encoder->setParams(['fm' => 'png'])->getFormat($this->getImageByMimeType('image/jpeg'))); $this->assertSame('gif', $this->encoder->setParams(['fm' => 'gif'])->getFormat($this->getImageByMimeType('image/jpeg'))); $this->assertSame('bmp', $this->encoder->setParams(['fm' => 'bmp'])->getFormat($this->getImageByMimeType('image/jpeg'))); + $this->assertSame('pjpg', $this->encoder->setParams(['fm' => 'pjpg'])->getFormat($this->getImageByMimeType('image/jpeg'))); // Make sure we keep the current format if no format is provided $this->assertSame('jpg', $this->encoder->setParams(['fm' => null])->getFormat($this->getImageByMimeType('image/jpeg'))); $this->assertSame('png', $this->encoder->setParams(['fm' => null])->getFormat($this->getImageByMimeType('image/png'))); $this->assertSame('gif', $this->encoder->setParams(['fm' => null])->getFormat($this->getImageByMimeType('image/gif'))); $this->assertSame('bmp', $this->encoder->setParams(['fm' => null])->getFormat($this->getImageByMimeType('image/bmp'))); + $this->assertSame('jpg', $this->encoder->setParams(['fm' => 'null'])->getFormat($this->getImageByMimeType('image/pjpeg'))); $this->assertSame('jpg', $this->encoder->setParams(['fm' => ''])->getFormat($this->getImageByMimeType('image/jpeg'))); $this->assertSame('png', $this->encoder->setParams(['fm' => ''])->getFormat($this->getImageByMimeType('image/png')));