Make WordPress Core


Ignore:
Timestamp:
07/31/2022 04:25:27 PM (4 years ago)
Author:
SergeyBiryukov
Message:

Tests: Update the terminology used for action or filter names in MockAction class.

This replaces the "tag" wording with "hook name" where appropriate, to match the core function signatures.

Includes:

  • Introducing a ::get_hook_names() method instead of ::get_tags(), keeping the latter as an alias.
  • Adding a hook_name key to the ::$events property, keeping tag for backward compatibility for now.
  • Adding missing @since tags for class methods.

Follow-up to [24/tests], [62/tests], [70/tests], [50807], [53804].

See #55652.

File:
1 edited

Legend:

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

    r52009 r53805  
    5757 *     $ma = new MockAction();
    5858 *     add_action( 'foo', array( &$ma, 'action' ) );
     59 *
     60 * @since UT (3.7.0)
    5961 */
    6062class MockAction {
    … …  
    6466        /**
    6567         * PHP5 constructor.
     68         *
     69         * @since UT (3.7.0)
    6670         */
    6771        public function __construct( $debug = 0 ) {
    … …  
    7074        }
    7175
     76        /**
     77         * @since UT (3.7.0)
     78         */
    7279        public function reset() {
    7380                $this->events = array();
    7481        }
    7582
     83        /**
     84         * @since UT (3.7.0)
     85         */
    7686        public function current_filter() {
     87                global $wp_actions;
     88
    7789                if ( is_callable( 'current_filter' ) ) {
    7890                        return current_filter();
    7991                }
    80                 global $wp_actions;
     92
    8193                return end( $wp_actions );
    8294        }
    8395
     96        /**
     97         * @since UT (3.7.0)
     98         */
    8499        public function action( $arg ) {
     100                $current_filter = $this->current_filter();
     101
    85102                if ( $this->debug ) {
    86                         dmp( __FUNCTION__, $this->current_filter() );
    87                 }
    88                 $args           = func_get_args();
     103                        dmp( __FUNCTION__, $current_filter );
     104                }
     105
    89106                $this->events[] = array(
    90                         'action' => __FUNCTION__,
    91                         'tag'    => $this->current_filter(),
    92                         'args'   => $args,
     107                        'action'    => __FUNCTION__,
     108                        'hook_name' => $current_filter,
     109                        'tag'       => $current_filter, // Back compat.
     110                        'args'      => func_get_args(),
    93111                );
     112
    94113                return $arg;
    95114        }
    96115
     116        /**
     117         * @since UT (3.7.0)
     118         */
    97119        public function action2( $arg ) {
     120                $current_filter = $this->current_filter();
     121
    98122                if ( $this->debug ) {
    99                         dmp( __FUNCTION__, $this->current_filter() );
    100                 }
    101 
    102                 $args           = func_get_args();
     123                        dmp( __FUNCTION__, $current_filter );
     124                }
     125
    103126                $this->events[] = array(
    104                         'action' => __FUNCTION__,
    105                         'tag'    => $this->current_filter(),
    106                         'args'   => $args,
     127                        'action'    => __FUNCTION__,
     128                        'hook_name' => $current_filter,
     129                        'tag'       => $current_filter, // Back compat.
     130                        'args'      => func_get_args(),
    107131                );
     132
    108133                return $arg;
    109134        }
    110135
     136        /**
     137         * @since UT (3.7.0)
     138         */
    111139        public function filter( $arg ) {
     140                $current_filter = $this->current_filter();
     141
    112142                if ( $this->debug ) {
    113                         dmp( __FUNCTION__, $this->current_filter() );
    114                 }
    115 
    116                 $args           = func_get_args();
     143                        dmp( __FUNCTION__, $current_filter );
     144                }
     145
    117146                $this->events[] = array(
    118                         'filter' => __FUNCTION__,
    119                         'tag'    => $this->current_filter(),
    120                         'args'   => $args,
     147                        'filter'    => __FUNCTION__,
     148                        'hook_name' => $current_filter,
     149                        'tag'       => $current_filter, // Back compat.
     150                        'args'      => func_get_args(),
    121151                );
     152
    122153                return $arg;
    123154        }
    124155
     156        /**
     157         * @since UT (3.7.0)
     158         */
    125159        public function filter2( $arg ) {
     160                $current_filter = $this->current_filter();
     161
    126162                if ( $this->debug ) {
    127                         dmp( __FUNCTION__, $this->current_filter() );
    128                 }
    129 
    130                 $args           = func_get_args();
     163                        dmp( __FUNCTION__, $current_filter );
     164                }
     165
    131166                $this->events[] = array(
    132                         'filter' => __FUNCTION__,
    133                         'tag'    => $this->current_filter(),
    134                         'args'   => $args,
     167                        'filter'    => __FUNCTION__,
     168                        'hook_name' => $current_filter,
     169                        'tag'       => $current_filter, // Back compat.
     170                        'args'      => func_get_args(),
    135171                );
     172
    136173                return $arg;
    137174        }
    138175
     176        /**
     177         * @since UT (3.7.0)
     178         */
    139179        public function filter_append( $arg ) {
     180                $current_filter = $this->current_filter();
     181
    140182                if ( $this->debug ) {
    141                         dmp( __FUNCTION__, $this->current_filter() );
    142                 }
    143 
    144                 $args           = func_get_args();
     183                        dmp( __FUNCTION__, $current_filter );
     184                }
     185
    145186                $this->events[] = array(
    146                         'filter' => __FUNCTION__,
    147                         'tag'    => $this->current_filter(),
    148                         'args'   => $args,
     187                        'filter'    => __FUNCTION__,
     188                        'hook_name' => $current_filter,
     189                        'tag'       => $current_filter, // Back compat.
     190                        'args'      => func_get_args(),
    149191                );
     192
    150193                return $arg . '_append';
    151194        }
    152195
    153         public function filterall( $tag, ...$args ) {
    154                 // This one doesn't return the result, so it's safe to use with the new 'all' filter.
     196        /**
     197         * Does not return the result, so it's safe to use with the 'all' filter.
     198         *
     199         * @since UT (3.7.0)
     200         */
     201        public function filterall( $hook_name, ...$args ) {
     202                $current_filter = $this->current_filter();
     203
    155204                if ( $this->debug ) {
    156                         dmp( __FUNCTION__, $this->current_filter() );
     205                        dmp( __FUNCTION__, $current_filter );
    157206                }
    158207
    159208                $this->events[] = array(
    160                         'filter' => __FUNCTION__,
    161                         'tag'    => $tag,
    162                         'args'   => $args,
     209                        'filter'    => __FUNCTION__,
     210                        'hook_name' => $hook_name,
     211                        'tag'       => $hook_name, // Back compat.
     212                        'args'      => $args,
    163213                );
    164214        }
    165215
    166         // Return a list of all the actions, tags and args.
     216        /**
     217         * Returns a list of all the actions, hook names and args.
     218         *
     219         * @since UT (3.7.0)
     220         */
    167221        public function get_events() {
    168222                return $this->events;
    169223        }
    170224
    171         // Return a count of the number of times the action was called since the last reset.
    172         public function get_call_count( $tag = '' ) {
    173                 if ( $tag ) {
     225        /**
     226         * Returns a count of the number of times the action was called since the last reset.
     227         *
     228         * @since UT (3.7.0)
     229         */
     230        public function get_call_count( $hook_name = '' ) {
     231                if ( $hook_name ) {
    174232                        $count = 0;
     233
    175234                        foreach ( $this->events as $e ) {
    176                                 if ( $e['action'] === $tag ) {
     235                                if ( $e['action'] === $hook_name ) {
    177236                                        ++$count;
    178237                                }
    179238                        }
     239
    180240                        return $count;
    181241                }
     242
    182243                return count( $this->events );
    183244        }
    184245
    185         // Return an array of the tags that triggered calls to this action.
     246        /**
     247         * Returns an array of the hook names that triggered calls to this action.
     248         *
     249         * @since 6.1.0
     250         */
     251        public function get_hook_names() {
     252                $out = array();
     253
     254                foreach ( $this->events as $e ) {
     255                        $out[] = $e['hook_name'];
     256                }
     257
     258                return $out;
     259        }
     260
     261        /**
     262         * Returns an array of the hook names that triggered calls to this action.
     263         *
     264         * @since UT (3.7.0)
     265         * @since 6.1.0 Turned into an alias for ::get_hook_names().
     266         */
    186267        public function get_tags() {
    187                 $out = array();
    188                 foreach ( $this->events as $e ) {
    189                         $out[] = $e['tag'];
    190                 }
    191                 return $out;
    192         }
    193 
    194         // Return an array of args passed in calls to this action.
     268                return $this->get_hook_names();
     269        }
     270
     271        /**
     272         * Returns an array of args passed in calls to this action.
     273         *
     274         * @since UT (3.7.0)
     275         */
    195276        public function get_args() {
    196277                $out = array();
     278
    197279                foreach ( $this->events as $e ) {
    198280                        $out[] = $e['args'];
    199281                }
     282
    200283                return $out;
    201284        }
Note: See TracChangeset for help on using the changeset viewer.