Make WordPress Core

Changeset 53530


Ignore:
Timestamp:
06/19/2022 04:51:49 PM (4 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

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

    r53529 r53530  
    316316
    317317        /**
    318          * Test that a passed mime type overrides the extension in the filename
     318         * Test that a passed mime type overrides the extension in the filename when saving an image.
     319         *
     320         * @dataProvider data_mime_overrides_filename
    319321         *
    320322         * @ticket 6821
     323         * @covers WP_Image_Editor::get_mime_type
     324         * @covers WP_Image_Editor::get_output_format
    321325         * @requires extension fileinfo
    322          */
    323         public function test_mime_overrides_filename() {
    324                 $classes = $this->get_image_editor_engine_classes();
    325 
    326                 // Test each image editor engine.
    327                 foreach ( $classes as $class ) {
    328                         $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    329                         $loaded = $img->load();
    330 
    331                         // Save the file.
    332                         $mime_type = 'image/gif';
    333                         $file      = wp_tempnam( 'tmp.jpg' );
    334                         $ret       = $img->save( $file, $mime_type );
    335 
    336                         // Make assertions.
    337                         $this->assertNotEmpty( $ret );
    338                         $this->assertNotWPError( $ret );
    339                         $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ) );
    340 
    341                         // Clean up.
    342                         unlink( $file );
    343                         unlink( $ret['path'] );
    344                         unset( $img );
    345                 }
     326         *
     327         * @param string $class_name Name of the image editor engine class to be tested.
     328         */
     329        public function test_mime_overrides_filename_when_saving_an_image( $class_name ) {
     330                $img    = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
     331                $loaded = $img->load();
     332
     333                // Save the file.
     334                $mime_type = 'image/gif';
     335                $file      = wp_tempnam( 'tmp.jpg' );
     336                $ret       = $img->save( $file, $mime_type );
     337
     338                // Make assertions.
     339                $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' );
     340                $this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned' );
     341                $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image did not override file name' );
     342
     343                // Clean up.
     344                unlink( $file );
     345                unlink( $ret['path'] );
     346                unset( $img );
     347        }
     348
     349        /**
     350         * Data provider.
     351         *
     352         * @return array
     353         */
     354        public function data_mime_overrides_filename() {
     355                return $this->text_array_to_dataprovider( $this->get_image_editor_engine_classes() );
    346356        }
    347357
Note: See TracChangeset for help on using the changeset viewer.