Skip to content

Commit

Permalink
add tests for pjpg
Browse files Browse the repository at this point in the history
  • Loading branch information
deluxetom committed Dec 17, 2024
1 parent 48b7765 commit e138f4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 1 addition & 2 deletions src/Api/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Check failure on line 114 in src/Api/Encoder.php

View workflow job for this annotation

GitHub Actions / static-analysis

RiskyTruthyFalsyComparison

src/Api/Encoder.php:114:20: RiskyTruthyFalsyComparison: Operand of type false|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead. (see https://psalm.dev/356)
} catch (\Exception) {
return 'jpg';
}
Expand Down
18 changes: 9 additions & 9 deletions tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class ApiTest extends TestCase
{
private $api;
private Api $api;

public function setUp(): void
{
Expand All @@ -26,43 +26,43 @@ 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.');

$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) {
Expand All @@ -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'),
[]
));
}
Expand Down
2 changes: 2 additions & 0 deletions tests/Api/EncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')));
Expand Down

0 comments on commit e138f4e

Please sign in to comment.