|
13 | 13 |
|
14 | 14 | namespace CodeIgniter\Images; |
15 | 15 |
|
| 16 | +use org\bovigo\vfs\vfsStreamFile; |
16 | 17 | use CodeIgniter\Config\Services; |
17 | 18 | use CodeIgniter\Images\Exceptions\ImageException; |
18 | 19 | use CodeIgniter\Images\Handlers\BaseHandler; |
@@ -463,4 +464,74 @@ public function testClearMetadataReturnsSelf(): void |
463 | 464 |
|
464 | 465 | $this->assertSame($this->handler, $result); |
465 | 466 | } |
| 467 | + |
| 468 | + public function testSaveFailed(): void |
| 469 | + { |
| 470 | + foreach (['gif', 'jpeg', 'png', 'webp'] as $type) { |
| 471 | + if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) { |
| 472 | + continue; // Skip if not supported |
| 473 | + } |
| 474 | + |
| 475 | + $this->handler->withFile($this->origin . 'ci-logo.' . $type); |
| 476 | + $this->handler->getResource(); // make sure resource is loaded |
| 477 | + |
| 478 | + try { |
| 479 | + $this->handler->save($this->start . 'wontwork/ci-logo.' . $type); |
| 480 | + $this->fail('Exception should have been thrown for ' . $type); |
| 481 | + } catch (ImageException $e) { |
| 482 | + $this->assertSame(lang('Images.saveFailed'), $e->getMessage()); |
| 483 | + } |
| 484 | + } |
| 485 | + } |
| 486 | + |
| 487 | + public function testImageCopyTargetWithMaxQuality(): void |
| 488 | + { |
| 489 | + foreach (['gif', 'jpeg', 'png', 'webp'] as $type) { |
| 490 | + if ($type === 'webp' && ! function_exists('imagecreatefromwebp')) { |
| 491 | + continue; |
| 492 | + } |
| 493 | + |
| 494 | + $this->handler->withFile($this->origin . 'ci-logo.' . $type); |
| 495 | + $this->assertTrue($this->handler->save($this->start . 'work/ci-logo.' . $type, 100)); |
| 496 | + $this->assertTrue($this->root->hasChild('work/ci-logo.' . $type)); |
| 497 | + |
| 498 | + /** @var vfsStreamFile $child */ |
| 499 | + $child = $this->root->getChild('work/ci-logo.' . $type); |
| 500 | + |
| 501 | + $this->assertSame( |
| 502 | + file_get_contents($this->origin . 'ci-logo.' . $type), |
| 503 | + $child->getContent(), |
| 504 | + ); |
| 505 | + } |
| 506 | + } |
| 507 | + |
| 508 | + public function testSaveUnsupportedImageType(): void |
| 509 | + { |
| 510 | + $this->handler->withFile($this->path); |
| 511 | + |
| 512 | + // Force an invalid image type to ensure proper exception handling |
| 513 | + $image = $this->handler->getFile(); |
| 514 | + $image->imageType = 9999; |
| 515 | + |
| 516 | + $this->expectException(ImageException::class); |
| 517 | + $this->expectExceptionMessage(lang('Images.unsupportedImageCreate')); |
| 518 | + |
| 519 | + // save() should throw an exception instead of fatal error in GD |
| 520 | + $this->handler->save($this->start . 'work/ci-logo.jpg'); |
| 521 | + } |
| 522 | + |
| 523 | + public function testProcessUnsupportedImageType(): void |
| 524 | + { |
| 525 | + $this->handler->withFile($this->path); |
| 526 | + |
| 527 | + // Force an invalid image type to trigger getImageResource default case |
| 528 | + $image = $this->handler->getFile(); |
| 529 | + $image->imageType = 9999; |
| 530 | + |
| 531 | + $this->expectException(ImageException::class); |
| 532 | + $this->expectExceptionMessage('Ima'); |
| 533 | + |
| 534 | + // Calling any process function that triggers ensureResource/createImage |
| 535 | + $this->handler->resize(10, 10); |
| 536 | + } |
466 | 537 | } |
0 commit comments