Make WordPress Core

Changeset 53523


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

Tests: Refactor Tests_Image_Functions::test_is_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].

Props jrf.
See #55652.

File:
1 edited

Legend:

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

    r53522 r53523  
    6464        }
    6565
    66         public function test_is_image_positive() {
     66        /**
     67         * @dataProvider data_is_image_positive
     68         *
     69         * @covers ::file_is_valid_image
     70         * @covers ::wp_getimagesize
     71         *
     72         * @param string $file File name.
     73         */
     74        public function test_is_image_positive( $file ) {
     75                $this->assertTrue(
     76                        file_is_valid_image( DIR_TESTDATA . '/images/' . $file ),
     77                        "file_is_valid_image( '$file' ) should return true"
     78                );
     79        }
     80
     81        /**
     82         * Data Provider.
     83         *
     84         * @return array
     85         */
     86        public function data_is_image_positive() {
    6787                // These are all image files recognized by PHP.
    6888                $files = array(
     
    7898                        'test-image-zip.tiff',
    7999                        'test-image.jpg',
     100                        'test-image.ico',
    80101                        'webp-animated.webp',
    81102                        'webp-lossless.webp',
     
    84105                );
    85106
    86                 // IMAGETYPE_ICO is only defined in PHP 5.3+.
    87                 if ( defined( 'IMAGETYPE_ICO' ) ) {
    88                         $files[] = 'test-image.ico';
    89                 }
    90 
    91                 foreach ( $files as $file ) {
    92                         $this->assertTrue( file_is_valid_image( DIR_TESTDATA . '/images/' . $file ), "file_is_valid_image($file) should return true" );
    93                 }
     107                return $this->text_array_to_dataprovider( $files );
    94108        }
    95109
Note: See TracChangeset for help on using the changeset viewer.