Make WordPress Core

Changeset 53529


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

Tests: Refactor Tests_Image_Functions::test_wp_save_image_file() 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 skip annotation for unsupported mime types.
  • Adding a failure message to each assertion.

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

Props jrf.
See #55652.

File:
1 edited

Legend:

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

    r53528 r53529  
    241241
    242242        /**
    243          * Test save image file and mime_types
     243         * Test wp_save_image_file() and mime types.
     244         *
     245         * @dataProvider data_wp_save_image_file
    244246         *
    245247         * @ticket 6821
     248         * @covers ::wp_save_image_file
    246249         * @requires extension fileinfo
    247          */
    248         public function test_wp_save_image_file() {
     250         *
     251         * @param string $class_name Name of the image editor engine class to be tested.
     252         * @param string $mime_type  The mime type to test.
     253         */
     254        public function test_wp_save_image_file( $class_name, $mime_type ) {
     255                require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     256
     257                $img    = new $class_name( DIR_TESTDATA . '/images/canola.jpg' );
     258                $loaded = $img->load();
     259
     260                if ( ! $img->supports_mime_type( $mime_type ) ) {
     261                        $this->markTestSkipped(
     262                                sprintf(
     263                                        'The %s mime type is not supported by the %s engine',
     264                                        $mime_type,
     265                                        str_replace( 'WP_Image_Editor_', '', $class_name )
     266                                )
     267                        );
     268                }
     269
     270                $file = wp_tempnam();
     271                $ret  = wp_save_image_file( $file, $img, $mime_type, 1 );
     272
     273                $this->assertNotEmpty( $ret, 'Image failed to save - "empty" response returned' );
     274                $this->assertNotWPError( $ret, 'Image failed to save - WP_Error returned' );
     275                $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ), 'Mime type of the saved image does not match' );
     276
     277                // Clean up.
     278                unlink( $file );
     279                unlink( $ret['path'] );
     280                unset( $img );
     281        }
     282
     283        /**
     284         * Data provider.
     285         *
     286         * @return array
     287         */
     288        public function data_wp_save_image_file() {
    249289                $classes = $this->get_image_editor_engine_classes();
    250 
    251                 require_once ABSPATH . 'wp-admin/includes/image-edit.php';
    252290
    253291                // Mime types.
     
    260298                // Include WebP in tests when platform supports it.
    261299                if ( function_exists( 'imagewebp' ) ) {
    262                         array_push( $mime_types, 'image/webp' );
    263                 }
    264 
    265                 // Test each image editor engine.
     300                        $mime_types[] = 'image/webp';
     301                }
     302
     303                $data = array();
     304
    266305                foreach ( $classes as $class ) {
    267                         $img    = new $class( DIR_TESTDATA . '/images/canola.jpg' );
    268                         $loaded = $img->load();
    269 
    270                         // Save a file as each mime type, assert it works.
    271306                        foreach ( $mime_types as $mime_type ) {
    272                                 if ( ! $img->supports_mime_type( $mime_type ) ) {
    273                                         continue;
    274                                 }
    275 
    276                                 $file = wp_tempnam();
    277                                 $ret  = wp_save_image_file( $file, $img, $mime_type, 1 );
    278                                 $this->assertNotEmpty( $ret );
    279                                 $this->assertNotWPError( $ret );
    280                                 $this->assertSame( $mime_type, $this->get_mime_type( $ret['path'] ) );
    281 
    282                                 // Clean up.
    283                                 unlink( $file );
    284                                 unlink( $ret['path'] );
     307                                $data[ $class . '; ' . $mime_type ] = array(
     308                                        'class_name' => $class,
     309                                        'mime_type'  => $mime_type,
     310                                );
    285311                        }
    286 
    287                         // Clean up.
    288                         unset( $img );
    289                 }
     312                }
     313
     314                return $data;
    290315        }
    291316
Note: See TracChangeset for help on using the changeset viewer.