|
19 | 19 | use CodeIgniter\Test\CIUnitTestCase; |
20 | 20 | use org\bovigo\vfs\vfsStream; |
21 | 21 | use org\bovigo\vfs\vfsStreamDirectory; |
| 22 | +use org\bovigo\vfs\vfsStreamFile; |
22 | 23 | use PHPUnit\Framework\Attributes\Group; |
| 24 | +use Throwable; |
23 | 25 |
|
24 | 26 | /** |
25 | 27 | * Unit testing for the GD image handler. |
@@ -463,4 +465,56 @@ public function testClearMetadataReturnsSelf(): void |
463 | 465 |
|
464 | 466 | $this->assertSame($this->handler, $result); |
465 | 467 | } |
| 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 | + } |
466 | 520 | } |
0 commit comments