Make WordPress Core

Changeset 25408


Ignore:
Timestamp:
09/12/2013 06:37:00 PM (11 years ago)
Author:
nacin
Message:

Test framework: Introduce the annotation @expectedDeprecated, modeled after PHPUnit's @expectedException.

It works for both functions and arguments (using the value of the first argument passed to _deprecated_function() or _deprecated_argument(), which is typically the function name). It asserts both ways:

  • If specified, those deprecated notices must be caught, or the test fails.
  • If not specified, any other deprecated notices cause the test to fail.

Works regardless of WP_DEBUG.
see #25282.

File:
1 edited

Legend:

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

    r25402 r25408  
    77
    88    protected static $forced_tickets = array();
    9     protected $deprecated_functions = array();
     9    protected $expected_deprecated = array();
     10    protected $caught_deprecated = array();
    1011
    1112    /**
     
    2526        $this->clean_up_global_scope();
    2627        $this->start_transaction();
     28        $this->expectDeprecated();
    2729        add_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
    28 
    29         if ( ! empty( $this->deprecated_functions ) )
    30             add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
    3130    }
    3231
    3332    function tearDown() {
    3433        global $wpdb;
     34        $this->expectedDeprecated();
    3535        $wpdb->query( 'ROLLBACK' );
    3636        remove_filter( 'dbdelta_create_queries', array( $this, '_create_temporary_tables' ) );
    3737        remove_filter( 'query', array( $this, '_drop_temporary_tables' ) );
    3838        remove_filter( 'wp_die_handler', array( $this, 'get_wp_die_handler' ) );
    39         if ( ! empty( $this->deprecated_functions ) )
    40             remove_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
    41     }
    42 
    43     function deprecated_function_run( $function ) {
    44         if ( in_array( $function, $this->deprecated_functions ) )
    45             add_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) );
    46     }
    47 
    48     function deprecated_function_trigger_error() {
    49         remove_filter( 'deprecated_function_trigger_error', array( $this, 'deprecated_function_trigger_error' ) );
    50         return false;
    5139    }
    5240
     
    9583    function wp_die_handler( $message ) {
    9684        throw new WPDieException( $message );
     85    }
     86
     87    function expectDeprecated() {
     88        $annotations = $this->getAnnotations();
     89        foreach ( array( 'class', 'method' ) as $depth ) {
     90            if ( ! empty( $annotations[ $depth ]['expectedDeprecated'] ) )
     91                $this->expected_deprecated = array_merge( $this->expected_deprecated, $annotations[ $depth ]['expectedDeprecated'] );
     92        }
     93        add_action( 'deprecated_function_run', array( $this, 'deprecated_function_run' ) );
     94        add_action( 'deprecated_argument_run', array( $this, 'deprecated_function_run' ) );
     95        add_action( 'deprecated_function_trigger_error', '__return_false' );
     96        add_action( 'deprecated_argument_trigger_error', '__return_false' );
     97    }
     98
     99    function expectedDeprecated() {
     100        $not_caught_deprecated = array_diff( $this->expected_deprecated, $this->caught_deprecated );
     101        foreach ( $not_caught_deprecated as $not_caught ) {
     102            $this->fail( "Failed to assert that $not_caught triggered a deprecated notice" );
     103        }
     104
     105        $unexpected_deprecated = array_diff( $this->caught_deprecated, $this->expected_deprecated );
     106        foreach ( $unexpected_deprecated as $unexpected ) {
     107            $this->fail( "Unexpected deprecated notice for $unexpected" );
     108        }
     109    }
     110
     111    function deprecated_function_run( $function ) {
     112        if ( ! in_array( $function, $this->caught_deprecated ) )
     113            $this->caught_deprecated[] = $function;
    97114    }
    98115
Note: See TracChangeset for help on using the changeset viewer.