From 1e48121fd6c9ccc13a13c0aee812e59cd4456b63 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Mon, 20 Jun 2022 19:34:54 +0000 Subject: [PATCH] Tests: Refactor `Tests_Image_Functions::test_load_directory()` to split the tests and use a data provider. This one test was testing three different situations. When one assertion fails, the rest of the test would not be executed, so this leads to hiding one error behind another. Splitting the test into three distinct test methods still allows for testing each situation, but tests each one in isolation and won't hide errors. The third part of the test, dealing with image editor engine classes, will also now use a data provider. Using a data provider has a number of advantages: 1. If the first test case fails, it won't prevent the other test cases from being tested. 2. The output from PHPUnit will be more descriptive in case of failure when using a data provider. 3. Using named test cases in the data provider will also make the `--testdox` output much more descriptive and informative. The actual cases being tested, or the test itself have not been changed. Includes: * Adding `@covers` annotations. * Adding a failure message to each assertion when multiple assertions are used in the test. * Reusing an existing data provider with the available image editor engine classes. Follow-up to [1061/tests], [53495], [53497], [53521], [53523], [53524], [53525], [53526], [53529], [53530], [53531]. Props jrf. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53537 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/image/functions.php | 71 +++++++++++++++---------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 121bf653be..7db3568488 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -28,7 +28,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { } /** - * Get the available image editor engine class(es). + * Get the available image editor engine classes. * * @return string[] Available image editor classes; empty array when none are available. */ @@ -45,6 +45,15 @@ class Tests_Image_Functions extends WP_UnitTestCase { return $classes; } + /** + * Data provider with the available image editor engine classes. + * + * @return array + */ + public function data_image_editor_engine_classes() { + return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() ); + } + /** * Get the MIME type of a file * @@ -321,7 +330,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { /** * Test that a passed mime type overrides the extension in the filename when saving an image. * - * @dataProvider data_mime_overrides_filename_when_saving_an_image + * @dataProvider data_image_editor_engine_classes * * @ticket 6821 * @covers WP_Image_Editor::get_mime_type @@ -352,15 +361,6 @@ class Tests_Image_Functions extends WP_UnitTestCase { unset( $img ); } - /** - * Data provider. - * - * @return array - */ - public function data_mime_overrides_filename_when_saving_an_image() { - return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() ); - } - /** * Test that mime types are correctly inferred from file extensions when saving an image. * @@ -441,30 +441,45 @@ class Tests_Image_Functions extends WP_UnitTestCase { } /** - * Try loading a directory + * Test that the deprecated wp_load_image() function fails when loading a directory. * * @ticket 17814 + * @covers ::wp_load_image * @expectedDeprecated wp_load_image */ - public function test_load_directory() { + public function test_wp_load_image_should_fail_with_error_message_when_loading_a_directory() { + $editor = wp_load_image( DIR_TESTDATA ); + $this->assertIsString( $editor ); + } - // First, test with deprecated wp_load_image function. - $editor1 = wp_load_image( DIR_TESTDATA ); - $this->assertIsString( $editor1 ); + /** + * Test that the wp_get_image_editor() function fails when loading a directory. + * + * @ticket 17814 + * @covers ::wp_get_image_editor + */ + public function test_wp_get_image_editor_should_fail_with_wp_error_object_when_loading_a_directory() { + $editor = wp_get_image_editor( DIR_TESTDATA ); + $this->assertInstanceOf( 'WP_Error', $editor ); + } - $editor2 = wp_get_image_editor( DIR_TESTDATA ); - $this->assertInstanceOf( 'WP_Error', $editor2 ); + /** + * Test that the load() method in an image editor class fails when loading a directory. + * + * @dataProvider data_image_editor_engine_classes + * + * @ticket 17814 + * @covers WP_Image_Editor_GD::load + * @covers WP_Image_Editor_Imagick::load + * + * @param string $class_name Name of the image editor engine class to be tested. + */ + public function test_image_editor_classes_should_fail_with_wp_error_object_when_loading_a_directory( $class_name ) { + $editor = new $class_name( DIR_TESTDATA ); + $loaded = $editor->load(); - $classes = $this->get_image_editor_engine_classes(); - - // Then, test with editors. - foreach ( $classes as $class ) { - $editor = new $class( DIR_TESTDATA ); - $loaded = $editor->load(); - - $this->assertInstanceOf( 'WP_Error', $loaded ); - $this->assertSame( 'error_loading_image', $loaded->get_error_code() ); - } + $this->assertInstanceOf( 'WP_Error', $loaded, 'Loading a directory did not result in a WP_Error.' ); + $this->assertSame( 'error_loading_image', $loaded->get_error_code(), 'Error code from WP_Error did not match expectation.' ); } /**