Make WordPress Core


Ignore:
Timestamp:
07/13/2021 10:43:42 AM (3 years ago)
Author:
SergeyBiryukov
Message:

Tests: Clean up skipping conditions and requirements for various tests.

This improves the consistency of test skipping and ensures that:

  • The @requires annotations use the right condition and format, and are on the right level (class vs. function).
  • Inline conditions with a markTestSkipped() call are only used when annotations cannot be used.
  • All markTestSkipped() calls contain a verbose explanation of why the test is being skipped.

Props jrf, hellofromTonya.
Fixes #53009.

File:
1 edited

Legend:

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

    r51397 r51415  
    173173     */
    174174    public function test_wp_save_image_file() {
    175         $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    176 
    177         foreach ( $classes as $key => $class ) {
    178             if ( ! call_user_func( array( $class, 'test' ) ) ) {
    179                 // If the image editor isn't available, skip it.
    180                 unset( $classes[ $key ] );
    181             }
    182         }
    183 
    184         if ( ! $classes ) {
    185             $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
    186         }
     175        $classes = $this->get_image_editor_engine_classes();
    187176
    188177        require_once ABSPATH . 'wp-admin/includes/image-edit.php';
     
    234223     */
    235224    public function test_mime_overrides_filename() {
    236         $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    237 
    238         foreach ( $classes as $key => $class ) {
    239             if ( ! call_user_func( array( $class, 'test' ) ) ) {
    240                 // If the image editor isn't available, skip it.
    241                 unset( $classes[ $key ] );
    242             }
    243         }
    244 
    245         if ( ! $classes ) {
    246             $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
    247         }
     225        $classes = $this->get_image_editor_engine_classes();
    248226
    249227        // Test each image editor engine.
     
    276254     */
    277255    public function test_inferred_mime_types() {
    278         $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    279 
    280         foreach ( $classes as $key => $class ) {
    281             if ( ! call_user_func( array( $class, 'test' ) ) ) {
    282                 // If the image editor isn't available, skip it.
    283                 unset( $classes[ $key ] );
    284             }
    285         }
    286 
    287         if ( ! $classes ) {
    288             $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
    289         }
     256        $classes = $this->get_image_editor_engine_classes();
    290257
    291258        // Mime types.
     
    343310        $this->assertInstanceOf( 'WP_Error', $editor2 );
    344311
     312        $classes = $this->get_image_editor_engine_classes();
     313
     314        // Then, test with editors.
     315        foreach ( $classes as $class ) {
     316            $editor = new $class( DIR_TESTDATA );
     317            $loaded = $editor->load();
     318
     319            $this->assertInstanceOf( 'WP_Error', $loaded );
     320            $this->assertSame( 'error_loading_image', $loaded->get_error_code() );
     321        }
     322    }
     323
     324    /**
     325     * Get the available image editor engine class(es).
     326     *
     327     * @return string[] Available image editor classes; empty array when none are avaialble.
     328     */
     329    private function get_image_editor_engine_classes() {
    345330        $classes = array( 'WP_Image_Editor_GD', 'WP_Image_Editor_Imagick' );
    346331
     
    352337        }
    353338
    354         if ( ! $classes ) {
    355             $this->markTestSkipped( sprintf( 'The image editor engine %s is not supported on this system.', 'WP_Image_Editor_GD' ) );
    356         }
    357 
    358         // Then, test with editors.
    359         foreach ( $classes as $class ) {
    360             $editor = new $class( DIR_TESTDATA );
    361             $loaded = $editor->load();
    362 
    363             $this->assertInstanceOf( 'WP_Error', $loaded );
    364             $this->assertSame( 'error_loading_image', $loaded->get_error_code() );
    365         }
     339        if ( empty( $classes ) ) {
     340            $this->markTestSkipped( 'Image editor engines WP_Image_Editor_GD and WP_Image_Editor_Imagick are not supported on this system.' );
     341        }
     342
     343        return $classes;
    366344    }
    367345
Note: See TracChangeset for help on using the changeset viewer.