From a7f868f4556a7717befbf72dac112496b542c4cf Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 20 Jun 2022 22:04:24 +0000 Subject: [PATCH] Tests: Further improve `Tests_Image_Functions::test_wp_crop_image*()` tests. Includes: * Making the test method names more specific. * Converting one-off helper methods to static closures. * Adding a failure message to each assertion when multiple assertions are used in the test. Follow-up to [1126/tests], [1201/tests], [53292], [53495], [53522], [53538]. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53541 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/image/functions.php | 72 ++++++++++++------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 6f72c90541..9ed8ef7348 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -482,37 +482,11 @@ class Tests_Image_Functions extends WP_UnitTestCase { $this->assertSame( 'error_loading_image', $loaded->get_error_code(), 'Error code from WP_Error did not match expectation.' ); } - /** - * @ticket 55403 - * @covers ::wp_crop_image - */ - public function test_wp_crop_image_with_filtered_extension() { - add_filter( 'image_editor_output_format', array( $this, 'filter_image_editor_output_format' ) ); - $file = wp_crop_image( - DIR_TESTDATA . '/images/canola.jpg', - 0, - 0, - 100, - 100, - 100, - 100 - ); - - $this->assertNotWPError( $file ); - $this->assertFileExists( $file ); - - unlink( $file ); - } - - public function filter_image_editor_output_format() { - return array_fill_keys( array( 'image/jpg', 'image/jpeg', 'image/png' ), 'image/webp' ); - } - /** * @covers ::wp_crop_image * @requires function imagejpeg */ - public function test_wp_crop_image_file() { + public function test_wp_crop_image_with_file() { $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg', 0, @@ -539,7 +513,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { * @requires function imagejpeg * @requires extension openssl */ - public function test_wp_crop_image_url() { + public function test_wp_crop_image_with_url() { $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canola.jpg', 0, @@ -571,7 +545,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { /** * @covers ::wp_crop_image */ - public function test_wp_crop_image_file_not_exists() { + public function test_wp_crop_image_should_fail_with_wp_error_object_if_file_does_not_exists() { $file = wp_crop_image( DIR_TESTDATA . '/images/canoladoesnotexist.jpg', 0, @@ -588,7 +562,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { * @covers ::wp_crop_image * @requires extension openssl */ - public function test_wp_crop_image_url_not_exists() { + public function test_wp_crop_image_should_fail_with_wp_error_object_if_url_does_not_exist() { $file = wp_crop_image( 'https://asdftestblog1.files.wordpress.com/2008/04/canoladoesnotexist.jpg', 0, @@ -605,9 +579,15 @@ class Tests_Image_Functions extends WP_UnitTestCase { * @ticket 23325 * @covers ::wp_crop_image */ - public function test_wp_crop_image_error_on_saving() { + public function test_wp_crop_image_should_fail_with_wp_error_object_if_there_was_an_error_on_saving() { WP_Image_Editor_Mock::$save_return = new WP_Error(); - add_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) ); + + add_filter( + 'wp_image_editors', + static function( $editors ) { + return array( 'WP_Image_Editor_Mock' ); + } + ); $file = wp_crop_image( DIR_TESTDATA . '/images/canola.jpg', @@ -620,15 +600,35 @@ class Tests_Image_Functions extends WP_UnitTestCase { ); $this->assertInstanceOf( 'WP_Error', $file ); - remove_filter( 'wp_image_editors', array( $this, 'mock_image_editor' ) ); WP_Image_Editor_Mock::$save_return = array(); } /** - * @see test_wp_crop_image_error_on_saving() + * @ticket 55403 + * @covers ::wp_crop_image */ - public function mock_image_editor( $editors ) { - return array( 'WP_Image_Editor_Mock' ); + public function test_wp_crop_image_should_return_correct_file_extension_if_output_format_was_modified() { + add_filter( + 'image_editor_output_format', + static function() { + return array_fill_keys( array( 'image/jpg', 'image/jpeg', 'image/png' ), 'image/webp' ); + } + ); + + $file = wp_crop_image( + DIR_TESTDATA . '/images/canola.jpg', + 0, + 0, + 100, + 100, + 100, + 100 + ); + + $this->assertNotWPError( $file, 'Cropping the image resulted in a WP_Error.' ); + $this->assertFileExists( $file, "The file $file does not exist." ); + + unlink( $file ); } /**