Make WordPress Core


Ignore:
Timestamp:
06/19/2022 05:10:48 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Refactor Tests_Image_Functions::test_inferred_mime_types() 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.
  • Adding a skip annotation for unsupported mime types.
  • Making the test method name more specific.

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

Props jrf.
See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/image/functions.php

    r53530 r53531  
    268268                }
    269269
     270                // Save the file.
    270271                $file = wp_tempnam();
    271272                $ret  = wp_save_image_file( $file, $img, $mime_type, 1 );
    272273
     274                // Make assertions.
    273275                $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' );
    274276                $this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned' );
     
    318320         * Test that a passed mime type overrides the extension in the filename when saving an image.
    319321         *
    320          * @dataProvider data_mime_overrides_filename
     322         * @dataProvider data_mime_overrides_filename_when_saving_an_image
    321323         *
    322324         * @ticket 6821
     
    352354         * @return array
    353355         */
    354         public function data_mime_overrides_filename() {
     356        public function data_mime_overrides_filename_when_saving_an_image() {
    355357                return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() );
    356358        }
    357359
    358360        /**
    359          * Test that mime types are correctly inferred from file extensions
     361         * Test that mime types are correctly inferred from file extensions when saving an image.
     362         *
     363         * @dataProvider data_inferred_mime_types_when_saving_an_image
    360364         *
    361365         * @ticket 6821
     366         * @covers WP_Image_Editor::get_mime_type
     367         * @covers WP_Image_Editor::get_output_format
    362368         * @requires extension fileinfo
    363          */
    364         public function test_inferred_mime_types() {
     369         *
     370         * @param string $class_name Name of the image editor engine class to be tested.
     371         * @param string $extension  File extension.
     372         * @param string $mime_type  The mime type to test.
     373         */
     374        public function test_inferred_mime_types_when_saving_an_image( $class_name, $extension, $mime_type ) {
     375                $img    = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
     376                $loaded = $img->load();
     377
     378                $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
     379                $this->assertNotWPError( $img );
     380
     381                if ( ! $img->supports_mime_type( $mime_type ) ) {
     382                        $this->markTestSkipped(
     383                                sprintf(
     384                                        'The %s mime type is not supported by the %s engine',
     385                                        $mime_type,
     386                                        str_replace( 'WP_Image_Editor_', '', $class_name )
     387                                )
     388                        );
     389                }
     390
     391                // Save the file.
     392                $temp = get_temp_dir();
     393                $file = wp_unique_filename( $temp, uniqid() . ".$extension" );
     394                $ret  = $img->save( trailingslashit( $temp ) . $file );
     395
     396                // Make assertions.
     397                $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' );
     398                $this->assertNotWPError( $ret, 'Image failed to save - WP Error returned' );
     399                $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image was not inferred correctly' );
     400
     401                // Clean up.
     402                unlink( $ret['path'] );
     403                unset( $img );
     404        }
     405
     406        /**
     407         * Data provider.
     408         *
     409         * @return array
     410         */
     411        public function data_inferred_mime_types_when_saving_an_image() {
    365412                $classes = $this->get_image_editor_engine_classes();
    366413
     
    373420                        'png'  => 'image/png',
    374421                        'webp' => 'image/webp',
    375                         'unk'  => 'image/jpeg',   // Default, unknown.
    376                 );
    377 
    378                 // Test each image editor engine.
     422                        'unk'  => 'image/jpeg', // Default, unknown.
     423                );
     424
     425                $data = array();
     426
    379427                foreach ( $classes as $class ) {
    380                         $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    381                         $loaded = $img->load();
    382 
    383                         // Save the image as each file extension, check the mime type.
    384                         $img = wp_get_image_editor( DIR_TESTDATA . '/images/canola.jpg' );
    385                         $this->assertNotWPError( $img );
    386 
    387                         $temp = get_temp_dir();
    388428                        foreach ( $mime_types as $ext => $mime_type ) {
    389                                 if ( ! $img->supports_mime_type( $mime_type ) ) {
    390                                         continue;
    391                                 }
    392 
    393                                 $file = wp_unique_filename( $temp, uniqid() . ".$ext" );
    394                                 $ret  = $img->save( trailingslashit( $temp ) . $file );
    395                                 $this->assertNotEmpty( $ret );
    396                                 $this->assertNotWPError( $ret );
    397                                 $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ) );
    398                                 unlink( $ret['path'] );
     429                                $data[ $class . '; Extension: ' . $ext . '; Mime type: ' . $mime_type ] = array(
     430                                        'class_name' => $class,
     431                                        'extension'  => $ext,
     432                                        'mime_type'  => $mime_type,
     433                                );
    399434                        }
    400 
    401                         // Clean up.
    402                         unset( $img );
    403                 }
     435                }
     436
     437                return $data;
    404438        }
    405439
Note: See TracChangeset for help on using the changeset viewer.