From 2b9472a6690214608d06d856716fb2f24b04f4e7 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 19 Jun 2022 15:28:25 +0000 Subject: [PATCH] Tests: Refactor `Tests_Image_Functions::test_is_displayable_image_positive()` to use a data provider. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. While the assertion used in this test method ''does'' have a "failure message" (👍), the output from PHPUnit itself will already 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: * Changing the conditional addition of the `.ico` file type to be unconditional, it was only needed for PHP < 5.3. * Adding a `@covers` annotation. Follow-up to [184/tests], [42780], [53495], [53497], [53521], [53523], [53524]. Props jrf. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53525 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/image/functions.php | 44 +++++++++++++++---------- 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index d652475936..b359bcff0a 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -138,12 +138,32 @@ class Tests_Image_Functions extends WP_UnitTestCase { return $this->text_array_to_dataprovider( $files ); } - public function test_is_displayable_image_positive() { + /** + * @dataProvider data_is_displayable_image_positive + * + * @covers ::file_is_displayable_image + * + * @param string $file File name. + */ + public function test_is_displayable_image_positive( $file ) { + $this->assertTrue( + file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), + "file_is_displayable_image( '$file' ) should return true" + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_is_displayable_image_positive() { // These are all usable in typical web browsers. $files = array( 'test-image.gif', 'test-image.png', 'test-image.jpg', + 'test-image.ico', ); // Add WebP images if the image editor supports them. @@ -151,25 +171,13 @@ class Tests_Image_Functions extends WP_UnitTestCase { $editor = wp_get_image_editor( $file ); if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/webp' ) ) { - $files = array_merge( - $files, - array( - 'webp-animated.webp', - 'webp-lossless.webp', - 'webp-lossy.webp', - 'webp-transparent.webp', - ) - ); + $files[] = 'webp-animated.webp'; + $files[] = 'webp-lossless.webp'; + $files[] = 'webp-lossy.webp'; + $files[] = 'webp-transparent.webp'; } - // IMAGETYPE_ICO is only defined in PHP 5.3+. - if ( defined( 'IMAGETYPE_ICO' ) ) { - $files[] = 'test-image.ico'; - } - - foreach ( $files as $file ) { - $this->assertTrue( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" ); - } + return $this->text_array_to_dataprovider( $files ); } public function test_is_displayable_image_negative() {