Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tests/system/Images/GDHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
use CodeIgniter\Test\CIUnitTestCase;
use org\bovigo\vfs\vfsStream;
use org\bovigo\vfs\vfsStreamDirectory;
use org\bovigo\vfs\vfsStreamFile;
use PHPUnit\Framework\Attributes\Group;
use Throwable;

/**
* Unit testing for the GD image handler.
Expand Down Expand Up @@ -463,4 +465,75 @@ public function testClearMetadataReturnsSelf(): void

$this->assertSame($this->handler, $result);
}

public function testSaveFailed(): void
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
$this->markTestSkipped('webp is not supported.');
}

$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->handler->getResource(); // make sure resource is loaded

try {
$this->handler->save($this->start . 'wontwork/ci-logo.' . $type);
$this->fail('Exception should have been thrown for ' . $type);
} catch (Throwable $e) {
$this->assertInstanceOf(ImageException::class, $e);
$this->assertSame(lang('Images.saveFailed'), $e->getMessage());
}
}
}

public function testImageCopyTargetWithMaxQuality(): void
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
$this->markTestSkipped('webp is not supported.');
}

$this->handler->withFile($this->origin . 'ci-logo.' . $type);
$this->assertTrue($this->handler->save($this->start . 'work/ci-logo.' . $type, 100));
$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));

/** @var vfsStreamFile $child */
$child = $this->root->getChild('work/ci-logo.' . $type);

$this->assertSame(
file_get_contents($this->origin . 'ci-logo.' . $type),
$child->getContent(),
);
}
}

public function testSaveUnsupportedImageType(): void
{
$this->handler->withFile($this->path);

// Force an invalid image type to ensure proper exception handling
$image = $this->handler->getFile();
$image->imageType = 9999;

$this->expectException(ImageException::class);
$this->expectExceptionMessage(lang('Images.unsupportedImageCreate'));

// save() should throw an exception instead of fatal error in GD
$this->handler->save($this->start . 'work/ci-logo.jpg');
}

public function testProcessUnsupportedImageType(): void
{
$this->handler->withFile($this->path);

// Force an invalid image type to trigger getImageResource default case
$image = $this->handler->getFile();
$image->imageType = 9999;

$this->expectException(ImageException::class);
$this->expectExceptionMessage('Ima');

// Calling any process function that triggers ensureResource/createImage
$this->handler->resize(10, 10);
}
}
Loading