Tests: Refactor Tests_Image_Functions::test_mime_overrides_filename() 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. 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 a `@covers` annotation.
* Adding a failure message to each assertion.
* Making the test method name more specific.

Follow-up to [1061/tests], [53495], [53497], [53521], [53523], [53524], [53525], [53526], [53529].

Props jrf.
See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53530 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-06-19 16:51:49 +00:00
parent f9eae75f9f
commit 342a457303

View File

@ -315,34 +315,44 @@ class Tests_Image_Functions extends WP_UnitTestCase {
}
/**
* Test that a passed mime type overrides the extension in the filename
* Test that a passed mime type overrides the extension in the filename when saving an image.
*
* @dataProvider data_mime_overrides_filename
*
* @ticket 6821
* @covers WP_Image_Editor::get_mime_type
* @covers WP_Image_Editor::get_output_format
* @requires extension fileinfo
*
* @param string $class_name Name of the image editor engine class to be tested.
*/
public function test_mime_overrides_filename() {
$classes = $this->get_image_editor_engine_classes();
public function test_mime_overrides_filename_when_saving_an_image( $class_name ) {
$img = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
$loaded = $img->load();
// Test each image editor engine.
foreach ( $classes as $class ) {
$img = new $class( DIR_TESTDATA . '/images/canola.jpg' );
$loaded = $img->load();
// Save the file.
$mime_type = 'image/gif';
$file = wp_tempnam( 'tmp.jpg' );
$ret = $img->save( $file, $mime_type );
// Save the file.
$mime_type = 'image/gif';
$file = wp_tempnam( 'tmp.jpg' );
$ret = $img->save( $file, $mime_type );
// Make assertions.
$this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' );
$this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned' );
$this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image did not override file name' );
// Make assertions.
$this->assertNotEmpty( $ret );
$this->assertNotWPError( $ret );
$this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ) );
// Clean up.
unlink( $file );
unlink( $ret['path'] );
unset( $img );
}
// Clean up.
unlink( $file );
unlink( $ret['path'] );
unset( $img );
}
/**
* Data provider.
*
* @return array
*/
public function data_mime_overrides_filename() {
return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() );
}
/**