Skip to content

Commit 0d1d224

Browse files
authored
test: cover GDHandler save failures and unsupported image types (#10414)
1 parent 299033f commit 0d1d224

1 file changed

Lines changed: 54 additions & 0 deletions

File tree

tests/system/Images/GDHandlerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
use CodeIgniter\Test\CIUnitTestCase;
2020
use org\bovigo\vfs\vfsStream;
2121
use org\bovigo\vfs\vfsStreamDirectory;
22+
use org\bovigo\vfs\vfsStreamFile;
2223
use PHPUnit\Framework\Attributes\Group;
24+
use Throwable;
2325

2426
/**
2527
* Unit testing for the GD image handler.
@@ -463,4 +465,56 @@ public function testClearMetadataReturnsSelf(): void
463465

464466
$this->assertSame($this->handler, $result);
465467
}
468+
469+
public function testSaveFailed(): void
470+
{
471+
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
472+
if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) {
473+
$this->markTestSkipped('webp is not supported.');
474+
}
475+
476+
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
477+
$this->handler->getResource(); // make sure resource is loaded
478+
479+
try {
480+
$this->handler->save($this->start . 'wontwork/ci-logo.' . $type);
481+
$this->fail('Exception should have been thrown for ' . $type);
482+
} catch (Throwable $e) {
483+
$this->assertInstanceOf(ImageException::class, $e);
484+
$this->assertSame(lang('Images.saveFailed'), $e->getMessage());
485+
}
486+
}
487+
}
488+
489+
public function testImageCopyTargetWithMaxQuality(): void
490+
{
491+
foreach (['gif', 'jpeg', 'png', 'webp'] as $type) {
492+
$this->handler->withFile($this->origin . 'ci-logo.' . $type);
493+
$this->assertTrue($this->handler->save($this->start . 'work/ci-logo.' . $type, 100));
494+
$this->assertTrue($this->root->hasChild('work/ci-logo.' . $type));
495+
496+
/** @var vfsStreamFile $child */
497+
$child = $this->root->getChild('work/ci-logo.' . $type);
498+
499+
$this->assertSame(
500+
file_get_contents($this->origin . 'ci-logo.' . $type),
501+
$child->getContent(),
502+
);
503+
}
504+
}
505+
506+
public function testProcessUnsupportedImageType(): void
507+
{
508+
$this->handler->withFile($this->path);
509+
510+
// Force an invalid image type to trigger getImageResource() default case
511+
$image = $this->handler->getFile();
512+
$image->imageType = 9999;
513+
514+
$this->expectException(ImageException::class);
515+
$this->expectExceptionMessage(lang('Images.unsupportedImageCreate'));
516+
517+
// resize() calls ensureResource() -> getImageResource(), which should throw
518+
$this->handler->resize(10, 10);
519+
}
466520
}

0 commit comments

Comments
 (0)