From a8ca5d7a2bdd4d8c4547dc5ed5bc964253000ca3 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Thu, 12 Oct 2023 17:08:44 +0900 Subject: [PATCH] Add missing exceptions to Output\Image image rotate Also create a basic phpunit class to test exceptions only --- 4dev/tests/Output/CoreLibsOutputImageTest.php | 84 +++++++++++++++++-- www/admin/class_test.image.php | 24 +++++- www/lib/CoreLibs/Output/Image.php | 12 ++- 3 files changed, 109 insertions(+), 11 deletions(-) diff --git a/4dev/tests/Output/CoreLibsOutputImageTest.php b/4dev/tests/Output/CoreLibsOutputImageTest.php index 53840934..aa8dff97 100644 --- a/4dev/tests/Output/CoreLibsOutputImageTest.php +++ b/4dev/tests/Output/CoreLibsOutputImageTest.php @@ -16,17 +16,89 @@ final class CoreLibsOutputImageTest extends TestCase /** * Undocumented function * - * @testdox Output\Image Class tests + * @covers ::createThumbnail + * @testdox createThumbnail checks * * @return void */ - public function testOutputImage() + public function testCreateThumbnail(): void { - // $this->assertTrue(true, 'Output Image Tests not implemented'); - $this->markTestIncomplete( - 'Output\Image Tests have not yet been implemented' + // CONVERT does not exist + $this->expectException(\RuntimeException::class); + \CoreLibs\Output\Image::createThumbnail('do_not_exist.png', 200, 200); + // set convert + $paths = [ + '/bin', + '/usr/bin', + '/usr/local/bin', + ]; + // find convert + foreach ($paths as $path) { + if ( + file_exists($path . DIRECTORY_SEPARATOR . 'convert') && + is_file($path . DIRECTORY_SEPARATOR . 'convert') + ) { + // image magick convert location + define('CONVERT', $path . DIRECTORY_SEPARATOR . 'convert'); + break; + } + } + unset($paths); + // cannot set dummy file + $this->expectException(\Exception::class); + \CoreLibs\Output\Image::createThumbnail('do_not_exist.png', 200, 200); + } + + /** + * Undocumented function + * + * @covers ::createThumbnailSimple + * @testdox createThumbnailSimple checks + * + * @return void + */ + public function testCreateThumbnailSimple(): void + { + // file does not exist + $this->expectException(\UnexpectedValueException::class); + \CoreLibs\Output\Image::createThumbnailSimple( + 'do_not_exist.png', + 200, + 200, + cache_folder: '/tmp/', + web_folder: '/tmp/' ); - // $this->markTestSkipped('No implementation for Output\Image at the moment'); + // cache folder is not dir + $this->expectException(\UnexpectedValueException::class); + \CoreLibs\Output\Image::createThumbnailSimple( + 'do_not_exist.png', + 200, + 200, + cache_folder: '/foo/bar/', + web_folder: '/tmp/' + ); + // target cache folder is not writeable + + // RuntimeException: imagecreatetruecolor failed + // RuntimeException: imagecolorallocatealpha failed + } + + /** + * Undocumented function + * + * @covers ::correctImageOrientation + * @testdox correctImageOrientation checks + * + * @return void + */ + public function testCorrectImageOrientation(): void + { + // test file does not exist + $this->expectException(\UnexpectedValueException::class); + \CoreLibs\Output\Image::correctImageOrientation('do_not_exist.png'); + // test folder not writeable + // test exit_read_data not present (how)? + // test image rotate } } diff --git a/www/admin/class_test.image.php b/www/admin/class_test.image.php index d2be61c6..0ae756ba 100644 --- a/www/admin/class_test.image.php +++ b/www/admin/class_test.image.php @@ -46,7 +46,13 @@ $image = BASE . LAYOUT . CONTENT_PATH . IMAGES . 'no_picture_square.jpg'; $cache_folder = BASE . LAYOUT . CONTENT_PATH . CACHE . IMAGES; $web_folder = LAYOUT . CACHE . IMAGES; // rotate image first -$_image->correctImageOrientation($image); +try { + $_image->correctImageOrientation($image); +} catch (\UnexpectedValueException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; +} catch (\RuntimeException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; +} // thumbnail tests echo "
CLASS->CREATETHUMBNAILSIMPLE: " . basename($image) . ": WIDTH: $thumb_width
CLASS->CREATETHUMBNAILSIMPLE: " // static $image = BASE . LAYOUT . CONTENT_PATH . IMAGES . 'no_picture.jpg'; // rotate image first -$image_class::correctImageOrientation($image); +try { + $image_class::correctImageOrientation($image); +} catch (\UnexpectedValueException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; +} catch (\RuntimeException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; +} // thumbnail tests echo "
S::CREATETHUMBNAILSIMPLE: " . basename($image) . ": WIDTH: $thumb_width
IMAGE INFO: " . $height . "x" . $width . ", TYPE: " . \CoreLibs\Debug\Support::dumpVar($img_type) . " [" . $finfo->file($image) . "]
"; // rotate image first - Image::correctImageOrientation($image); + try { + Image::correctImageOrientation($image); + } catch (\UnexpectedValueException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; + } catch (\RuntimeException $e) { + print "Message:
" . $e->getMessage() . "
" . $e . "
"; + } // thumbnail tests echo "
" . basename($image) . ": WIDTH: $thumb_width
"; diff --git a/www/lib/CoreLibs/Output/Image.php b/www/lib/CoreLibs/Output/Image.php index 5af6ce3f..897e6495 100644 --- a/www/lib/CoreLibs/Output/Image.php +++ b/www/lib/CoreLibs/Output/Image.php @@ -470,12 +470,20 @@ class Image * * @param string $filename path + filename to rotate. This file must be writeable * @return void + * @throws \RuntimeException if exit_read_data is not found + * @throws \UnexpectedValueException if file name not writeable or file name not found */ public static function correctImageOrientation(string $filename): void { // function exists & file is writeable, else do nothing - if (!function_exists('exif_read_data') || !is_writeable($filename)) { - return; + if (!function_exists('exif_read_data')) { + throw new \RuntimeException('Function \'exit_read_data\' does not exist'); + } + if (!file_exists($filename) || !is_file($filename)) { + throw new \UnexpectedValueException('Missing image file: ' . $filename); + } + if (!is_writeable($filename)) { + throw new \UnexpectedValueException('File name is not writeable: ' . $filename); } [$inc_width, $inc_height, $img_type] = getimagesize($filename) ?: [0, 0, null]; // add @ to avoid "file not supported error"