diff --git a/src/Api/Encoder.php b/src/Api/Encoder.php index 81e6862..eab97e0 100644 --- a/src/Api/Encoder.php +++ b/src/Api/Encoder.php @@ -104,7 +104,11 @@ public function getFormat(ImageInterface $image): string { $fm = (string) $this->getParam('fm'); if ($fm) { - return array_key_exists($fm, static::supportedFormats()) ? $fm : 'jpg'; + if (!array_key_exists($fm, static::supportedFormats())) { + throw new \InvalidArgumentException("Invalid format provided: {$fm}"); + } + + return $fm; } $mediaType = MediaType::tryFrom($image->origin()->mediaType()); diff --git a/tests/Api/EncoderTest.php b/tests/Api/EncoderTest.php index 025d10a..d437bcd 100644 --- a/tests/Api/EncoderTest.php +++ b/tests/Api/EncoderTest.php @@ -123,7 +123,6 @@ public function testGetFormat(): void $this->assertSame('jpg', $this->encoder->setParams(['fm' => ''])->getFormat($this->getImageByMimeType('image/jpeg'))); $this->assertSame('png', $this->encoder->setParams(['fm' => ''])->getFormat($this->getImageByMimeType('image/png'))); - $this->assertSame('jpg', $this->encoder->setParams(['fm' => 'invalid'])->getFormat($this->getImageByMimeType('image/png'))); if (function_exists('imagecreatefromwebp')) { $this->assertSame('webp', $this->encoder->setParams(['fm' => null])->getFormat($this->getImageByMimeType('image/webp'))); @@ -136,6 +135,13 @@ public function testGetFormat(): void } } + public function testGetFormatThrowsExceptionForInvalidFormat(): void + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid format provided: invalid'); + $this->encoder->setParams(['fm' => 'invalid'])->getFormat($this->getImageByMimeType('image/png')); + } + protected function getImageByMimeType(string $mimeType): ImageInterface { return \Mockery::mock(ImageInterface::class, function ($mock) use ($mimeType) {