From e7a9d910209083171c8a7c5a8ae18db87f6bccca Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 19 Jun 2022 15:19:17 +0000 Subject: [PATCH] Tests: Refactor `Tests_Image_Functions::test_is_image_negative()` 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: * Adding a `@covers` annotation. Follow-up to [184/tests], [53495], [53497], [53521], [53523]. Props jrf. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53524 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/image/functions.php | 28 ++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/image/functions.php b/tests/phpunit/tests/image/functions.php index 6155946128..d652475936 100644 --- a/tests/phpunit/tests/image/functions.php +++ b/tests/phpunit/tests/image/functions.php @@ -79,7 +79,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { } /** - * Data Provider. + * Data provider. * * @return array */ @@ -107,7 +107,27 @@ class Tests_Image_Functions extends WP_UnitTestCase { return $this->text_array_to_dataprovider( $files ); } - public function test_is_image_negative() { + /** + * @dataProvider data_is_image_negative + * + * @covers ::file_is_valid_image + * @covers ::wp_getimagesize + * + * @param string $file File name. + */ + public function test_is_image_negative( $file ) { + $this->assertFalse( + file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), + "file_is_valid_image( '$file' ) should return false" + ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_is_image_negative() { // These are actually image files but aren't recognized or usable by PHP. $files = array( 'test-image.pct', @@ -115,9 +135,7 @@ class Tests_Image_Functions extends WP_UnitTestCase { 'test-image.sgi', ); - foreach ( $files as $file ) { - $this->assertFalse( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return false" ); - } + return $this->text_array_to_dataprovider( $files ); } public function test_is_displayable_image_positive() {