Make WordPress Core


Ignore:
Timestamp:
06/19/2022 03:28:25 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Refactor Tests_Image_Functions::test_is_displayable_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], [53523], [53524].

Props jrf.
See #55652.

File:
1 edited

Legend:

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

    r53524 r53525  
    139139    }
    140140
    141     public function test_is_displayable_image_positive() {
     141    /**
     142     * @dataProvider data_is_displayable_image_positive
     143     *
     144     * @covers ::file_is_displayable_image
     145     *
     146     * @param string $file File name.
     147     */
     148    public function test_is_displayable_image_positive( $file ) {
     149        $this->assertTrue(
     150            file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ),
     151            "file_is_displayable_image( '$file' ) should return true"
     152        );
     153    }
     154
     155    /**
     156     * Data provider.
     157     *
     158     * @return array
     159     */
     160    public function data_is_displayable_image_positive() {
    142161        // These are all usable in typical web browsers.
    143162        $files = array(
     
    145164            'test-image.png',
    146165            'test-image.jpg',
     166            'test-image.ico',
    147167        );
    148168
     
    152172
    153173        if ( ! is_wp_error( $editor ) && $editor->supports_mime_type( 'image/webp' ) ) {
    154             $files = array_merge(
    155                 $files,
    156                 array(
    157                     'webp-animated.webp',
    158                     'webp-lossless.webp',
    159                     'webp-lossy.webp',
    160                     'webp-transparent.webp',
    161                 )
    162             );
    163         }
    164 
    165         // IMAGETYPE_ICO is only defined in PHP 5.3+.
    166         if ( defined( 'IMAGETYPE_ICO' ) ) {
    167             $files[] = 'test-image.ico';
    168         }
    169 
    170         foreach ( $files as $file ) {
    171             $this->assertTrue( file_is_displayable_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" );
    172         }
     174            $files[] = 'webp-animated.webp';
     175            $files[] = 'webp-lossless.webp';
     176            $files[] = 'webp-lossy.webp';
     177            $files[] = 'webp-transparent.webp';
     178        }
     179
     180        return $this->text_array_to_dataprovider( $files );
    173181    }
    174182
Note: See TracChangeset for help on using the changeset viewer.