Make WordPress Core

Changeset 53637


Ignore:
Timestamp:
07/03/2022 04:39:19 PM (3 years ago)
Author:
SergeyBiryukov
Message:

Build/Test Tools: Include the actual _doing_it_wrong() message or deprecation notice in the output.

This aims to provide better context and more details if an unexpected _doing_it_wrong() message or deprecation notice is encountered during a test run.

Previously, this would display a message like Unexpected incorrect usage notice for [...], but without any further details, making it harder to track down the actual issue.

Follow-up to [25402], [25408], [25785], [37861], [51872].

See #55652.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/includes/abstract-testcase.php

    r53545 r53637  
    503503        foreach ( array( 'class', 'method' ) as $depth ) {
    504504            if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) ) {
    505                 $this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] );
    506             }
     505                $this->expected_deprecated = array_merge(
     506                    $this->expected_deprecated,
     507                    $annotations[ $depth ]['expectedDeprecated']
     508                );
     509            }
     510
    507511            if ( ! empty( $annotations[ $depth ]['expectedIncorrectUsage'] ) ) {
    508                 $this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $annotations[ $depth ]['expectedIncorrectUsage'] );
    509             }
    510         }
    511         add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
    512         add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
    513         add_action( 'deprecated_file_included', array( $this, 'deprecated_function_run' ) );
    514         add_action( 'deprecated_hook_run', array( $this, 'deprecated_function_run' ) );
    515         add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ) );
     512                $this->expected_doing_it_wrong = array_merge(
     513                    $this->expected_doing_it_wrong,
     514                    $annotations[ $depth ]['expectedIncorrectUsage']
     515                );
     516            }
     517        }
     518
     519        add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ), 10, 3 );
     520        add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ), 10, 3 );
     521        add_action( 'deprecated_file_included', array( $this, 'deprecated_function_run' ), 10, 4 );
     522        add_action( 'deprecated_hook_run', array( $this, 'deprecated_function_run' ), 10, 4 );
     523        add_action( 'doing_it_wrong_run', array( $this, 'doing_it_wrong_run' ), 10, 3 );
     524
    516525        add_action( 'deprecated_function_trigger_error', '__return_false' );
    517526        add_action( 'deprecated_argument_trigger_error', '__return_false' );
     
    529538        $errors = array();
    530539
    531         $not_caught_deprecated = array_diff( $this->expected_deprecated, $this->caught_deprecated );
     540        $not_caught_deprecated = array_diff(
     541            $this->expected_deprecated,
     542            array_keys( $this->caught_deprecated )
     543        );
     544
    532545        foreach ( $not_caught_deprecated as $not_caught ) {
    533             $errors[] = "Failed to assert that $not_caught triggered a deprecated notice";
    534         }
    535 
    536         $unexpected_deprecated = array_diff( $this->caught_deprecated, $this->expected_deprecated );
     546            $errors[] = "Failed to assert that $not_caught triggered a deprecation notice";
     547        }
     548
     549        $unexpected_deprecated = array_diff(
     550            array_keys( $this->caught_deprecated ),
     551            $this->expected_deprecated
     552        );
     553
    537554        foreach ( $unexpected_deprecated as $unexpected ) {
    538             $errors[] = "Unexpected deprecated notice for $unexpected";
    539         }
    540 
    541         $not_caught_doing_it_wrong = array_diff( $this->expected_doing_it_wrong, $this->caught_doing_it_wrong );
     555            $errors[] = "Unexpected deprecation notice for $unexpected";
     556            $errors[] = $this->caught_deprecated[ $unexpected ];
     557        }
     558
     559        $not_caught_doing_it_wrong = array_diff(
     560            $this->expected_doing_it_wrong,
     561            array_keys( $this->caught_doing_it_wrong )
     562        );
     563
    542564        foreach ( $not_caught_doing_it_wrong as $not_caught ) {
    543565            $errors[] = "Failed to assert that $not_caught triggered an incorrect usage notice";
    544566        }
    545567
    546         $unexpected_doing_it_wrong = array_diff( $this->caught_doing_it_wrong, $this->expected_doing_it_wrong );
     568        $unexpected_doing_it_wrong = array_diff(
     569            array_keys( $this->caught_doing_it_wrong ),
     570            $this->expected_doing_it_wrong
     571        );
     572
    547573        foreach ( $unexpected_doing_it_wrong as $unexpected ) {
    548574            $errors[] = "Unexpected incorrect usage notice for $unexpected";
     575            $errors[] = $this->caught_doing_it_wrong[ $unexpected ];
    549576        }
    550577
     
    619646     * Adds a deprecated function to the list of caught deprecated calls.
    620647     *
    621      * @param string $function The deprecated function.
    622      */
    623     public function deprecated_function_run( $function ) {
    624         if ( ! in_array( $function, $this->caught_deprecated, true ) ) {
    625             $this->caught_deprecated[] = $function;
     648     * @param string $function    The deprecated function.
     649     * @param string $replacement The function that should have been called.
     650     * @param string $version     The version of WordPress that deprecated the function.
     651     * @param string $message     Optional. A message regarding the change.
     652     */
     653    public function deprecated_function_run( $function, $replacement, $version, $message = '' ) {
     654        if ( ! isset( $this->caught_deprecated[ $function ] ) ) {
     655            switch ( current_action() ) {
     656                case 'deprecated_function_run':
     657                    if ( $replacement ) {
     658                        $message = sprintf(
     659                            'Function %1$s is deprecated since version %2$s! Use %3$s instead.',
     660                            $function,
     661                            $version,
     662                            $replacement
     663                        );
     664                    } else {
     665                        $message = sprintf(
     666                            'Function %1$s is deprecated since version %2$s with no alternative available.',
     667                            $function,
     668                            $version
     669                        );
     670                    }
     671                    break;
     672
     673                case 'deprecated_argument_run':
     674                    if ( $replacement ) {
     675                        $message = sprintf(
     676                            'Function %1$s was called with an argument that is deprecated since version %2$s! %3$s',
     677                            $function,
     678                            $version,
     679                            $replacement
     680                        );
     681                    } else {
     682                        $message = sprintf(
     683                            'Function %1$s was called with an argument that is deprecated since version %2$s with no alternative available.',
     684                            $function,
     685                            $version
     686                        );
     687                    }
     688                    break;
     689
     690                case 'deprecated_file_included':
     691                    if ( $replacement ) {
     692                        $message = sprintf(
     693                            'File %1$s is deprecated since version %2$s! Use %3$s instead.',
     694                            $function,
     695                            $version,
     696                            $replacement
     697                        ) . ' ' . $message;
     698                    } else {
     699                        $message = sprintf(
     700                            'File %1$s is deprecated since version %2$s with no alternative available.',
     701                            $function,
     702                            $version
     703                        ) . ' ' . $message;
     704                    }
     705                    break;
     706
     707                case 'deprecated_hook_run':
     708                    if ( $replacement ) {
     709                        $message = sprintf(
     710                            'Hook %1$s is deprecated since version %2$s! Use %3$s instead.',
     711                            $function,
     712                            $version,
     713                            $replacement
     714                        ) . ' ' . $message;
     715                    } else {
     716                        $message = sprintf(
     717                            'Hook %1$s is deprecated since version %2$s with no alternative available.',
     718                            $function,
     719                            $version
     720                        ) . ' ' . $message;
     721                    }
     722                    break;
     723            }
     724
     725            $this->caught_deprecated[ $function ] = $message;
    626726        }
    627727    }
     
    631731     *
    632732     * @param string $function The function to add.
    633      */
    634     public function doing_it_wrong_run( $function ) {
    635         if ( ! in_array( $function, $this->caught_doing_it_wrong, true ) ) {
    636             $this->caught_doing_it_wrong[] = $function;
     733     * @param string $message  A message explaining what has been done incorrectly.
     734     * @param string $version  The version of WordPress where the message was added.
     735     */
     736    public function doing_it_wrong_run( $function, $message, $version ) {
     737        if ( ! isset( $this->caught_doing_it_wrong[ $function ] ) ) {
     738            if ( $version ) {
     739                $message .= ' ' . sprintf( '(This message was added in version %s.)', $version );
     740            }
     741
     742            $this->caught_doing_it_wrong[ $function ] = $message;
    637743        }
    638744    }
Note: See TracChangeset for help on using the changeset viewer.