Tests: Refactor Tests_Image_Functions::test_is_image_positive() to 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. 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].

Props jrf.
See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53523 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-06-19 15:13:16 +00:00
parent e44c94abc5
commit 4e0f290f2c

View File

@ -63,7 +63,27 @@ class Tests_Image_Functions extends WP_UnitTestCase {
return $mime_type; return $mime_type;
} }
public function test_is_image_positive() { /**
* @dataProvider data_is_image_positive
*
* @covers ::file_is_valid_image
* @covers ::wp_getimagesize
*
* @param string $file File name.
*/
public function test_is_image_positive( $file ) {
$this->assertTrue(
file_is_valid_image( DIR_TESTDATA . '/images/' . $file ),
"file_is_valid_image( '$file' ) should return true"
);
}
/**
* Data Provider.
*
* @return array
*/
public function data_is_image_positive() {
// These are all image files recognized by PHP. // These are all image files recognized by PHP.
$files = array( $files = array(
'test-image-cmyk.jpg', 'test-image-cmyk.jpg',
@ -77,20 +97,14 @@ class Tests_Image_Functions extends WP_UnitTestCase {
'test-image.psd', 'test-image.psd',
'test-image-zip.tiff', 'test-image-zip.tiff',
'test-image.jpg', 'test-image.jpg',
'test-image.ico',
'webp-animated.webp', 'webp-animated.webp',
'webp-lossless.webp', 'webp-lossless.webp',
'webp-lossy.webp', 'webp-lossy.webp',
'webp-transparent.webp', 'webp-transparent.webp',
); );
// IMAGETYPE_ICO is only defined in PHP 5.3+. return $this->text_array_to_dataprovider( $files );
if ( defined( 'IMAGETYPE_ICO' ) ) {
$files[] = 'test-image.ico';
}
foreach ( $files as $file ) {
$this->assertTrue( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" );
}
} }
public function test_is_image_negative() { public function test_is_image_negative() {