Skip to content

test: add unit tests for GDHandler save and security exceptions#10414

Open
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:test/gdhandler-save-coverage
Open

test: add unit tests for GDHandler save and security exceptions#10414
gr8man wants to merge 2 commits into
codeigniter4:developfrom
gr8man:test/gdhandler-save-coverage

Conversation

@gr8man

@gr8man gr8man commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

Description
Adds missing unit tests for GDHandler::save() and unsupported image types exceptions to improve code coverage.

Changes:

  • Added tests for file save failures using vfsStream.
  • Added a test for the save($target, 100) unmodified copy logic.
  • Added tests simulating an unsupported imageType to ensure ImageException is thrown instead of a fatal GD error.

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value (without duplication)
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@github-actions github-actions Bot added the testing Pull requests that changes tests only label Jul 17, 2026
Comment thread tests/system/Images/GDHandlerTest.php Outdated
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
continue; // Skip if not supported

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use markTestSkipped

Comment thread tests/system/Images/GDHandlerTest.php Outdated
Comment on lines +481 to +482
} catch (ImageException $e) {
$this->assertSame(lang('Images.saveFailed'), $e->getMessage());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a generic Throwable catch here. Then assert the exception type as ImageException.

Comment thread tests/system/Images/GDHandlerTest.php Outdated
{
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
continue;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use markTestSkipped

@gr8man
gr8man force-pushed the test/gdhandler-save-coverage branch 2 times, most recently from 7856817 to 00cf519 Compare July 18, 2026 18:00
Comment on lines +510 to +523
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');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests an exception in getImageResource(), not in save().

Comment on lines +492 to +494
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
$this->markTestSkipped('webp is not supported.');
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For max quality, this should not be needed.

Comment thread tests/system/Images/GDHandlerTest.php Outdated
$image->imageType = 9999;

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Ima"?

@gr8man
gr8man force-pushed the test/gdhandler-save-coverage branch from 8a110e5 to 1f8ecac Compare July 19, 2026 19:59
@gr8man
gr8man force-pushed the test/gdhandler-save-coverage branch from 1f8ecac to 1bc83dc Compare July 19, 2026 20:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

testing Pull requests that changes tests only

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants