Changeset 53805
- Timestamp:
- 07/31/2022 04:25:27 PM (3 years ago)
- Location:
- trunk/tests/phpunit
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/includes/utils.php
r52009 r53805 57 57 * $ma = new MockAction(); 58 58 * add_action( 'foo', array( &$ma, 'action' ) ); 59 * 60 * @since UT (3.7.0) 59 61 */ 60 62 class MockAction { … … 64 66 /** 65 67 * PHP5 constructor. 68 * 69 * @since UT (3.7.0) 66 70 */ 67 71 public function __construct( $debug = 0 ) { … … 70 74 } 71 75 76 /** 77 * @since UT (3.7.0) 78 */ 72 79 public function reset() { 73 80 $this->events = array(); 74 81 } 75 82 83 /** 84 * @since UT (3.7.0) 85 */ 76 86 public function current_filter() { 87 global $wp_actions; 88 77 89 if ( is_callable( 'current_filter' ) ) { 78 90 return current_filter(); 79 91 } 80 global $wp_actions; 92 81 93 return end( $wp_actions ); 82 94 } 83 95 96 /** 97 * @since UT (3.7.0) 98 */ 84 99 public function action( $arg ) { 100 $current_filter = $this->current_filter(); 101 85 102 if ( $this->debug ) { 86 dmp( __FUNCTION__, $ this->current_filter());87 } 88 $args = func_get_args(); 103 dmp( __FUNCTION__, $current_filter ); 104 } 105 89 106 $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(), 93 111 ); 112 94 113 return $arg; 95 114 } 96 115 116 /** 117 * @since UT (3.7.0) 118 */ 97 119 public function action2( $arg ) { 120 $current_filter = $this->current_filter(); 121 98 122 if ( $this->debug ) { 99 dmp( __FUNCTION__, $this->current_filter() ); 100 } 101 102 $args = func_get_args(); 123 dmp( __FUNCTION__, $current_filter ); 124 } 125 103 126 $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(), 107 131 ); 132 108 133 return $arg; 109 134 } 110 135 136 /** 137 * @since UT (3.7.0) 138 */ 111 139 public function filter( $arg ) { 140 $current_filter = $this->current_filter(); 141 112 142 if ( $this->debug ) { 113 dmp( __FUNCTION__, $this->current_filter() ); 114 } 115 116 $args = func_get_args(); 143 dmp( __FUNCTION__, $current_filter ); 144 } 145 117 146 $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(), 121 151 ); 152 122 153 return $arg; 123 154 } 124 155 156 /** 157 * @since UT (3.7.0) 158 */ 125 159 public function filter2( $arg ) { 160 $current_filter = $this->current_filter(); 161 126 162 if ( $this->debug ) { 127 dmp( __FUNCTION__, $this->current_filter() ); 128 } 129 130 $args = func_get_args(); 163 dmp( __FUNCTION__, $current_filter ); 164 } 165 131 166 $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(), 135 171 ); 172 136 173 return $arg; 137 174 } 138 175 176 /** 177 * @since UT (3.7.0) 178 */ 139 179 public function filter_append( $arg ) { 180 $current_filter = $this->current_filter(); 181 140 182 if ( $this->debug ) { 141 dmp( __FUNCTION__, $this->current_filter() ); 142 } 143 144 $args = func_get_args(); 183 dmp( __FUNCTION__, $current_filter ); 184 } 185 145 186 $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(), 149 191 ); 192 150 193 return $arg . '_append'; 151 194 } 152 195 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 155 204 if ( $this->debug ) { 156 dmp( __FUNCTION__, $ this->current_filter());205 dmp( __FUNCTION__, $current_filter ); 157 206 } 158 207 159 208 $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, 163 213 ); 164 214 } 165 215 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 */ 167 221 public function get_events() { 168 222 return $this->events; 169 223 } 170 224 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 ) { 174 232 $count = 0; 233 175 234 foreach ( $this->events as $e ) { 176 if ( $e['action'] === $ tag) {235 if ( $e['action'] === $hook_name ) { 177 236 ++$count; 178 237 } 179 238 } 239 180 240 return $count; 181 241 } 242 182 243 return count( $this->events ); 183 244 } 184 245 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 */ 186 267 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 */ 195 276 public function get_args() { 196 277 $out = array(); 278 197 279 foreach ( $this->events as $e ) { 198 280 $out[] = $e['args']; 199 281 } 282 200 283 return $out; 201 284 } -
trunk/tests/phpunit/tests/actions.php
r53804 r53805 21 21 $this->assertSame( 1, $a->get_call_count() ); 22 22 // Only our hook was called. 23 $this->assertSame( array( $hook_name ), $a->get_ tags() );23 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 24 24 25 25 $argsvar = $a->get_args(); … … 40 40 // Make sure our hook was called correctly. 41 41 $this->assertSame( 1, $a->get_call_count() ); 42 $this->assertSame( array( $hook_name ), $a->get_ tags() );42 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 43 43 44 44 // Now remove the action, do it again, and make sure it's not called this time. … … 46 46 do_action( $hook_name ); 47 47 $this->assertSame( 1, $a->get_call_count() ); 48 $this->assertSame( array( $hook_name ), $a->get_ tags() );48 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 49 49 50 50 } … … 216 216 // 'action2' is called first because it has priority 9. 217 217 array( 218 'action' => 'action2', 219 'tag' => $hook_name, 220 'args' => array( '' ), 218 'action' => 'action2', 219 'hook_name' => $hook_name, 220 'tag' => $hook_name, // Back compat. 221 'args' => array( '' ), 221 222 ), 222 223 // 'action' is called second. 223 224 array( 224 'action' => 'action', 225 'tag' => $hook_name, 226 'args' => array( '' ), 225 'action' => 'action', 226 'hook_name' => $hook_name, 227 'tag' => $hook_name, // Back compat. 228 'args' => array( '' ), 227 229 ), 228 230 ); … … 275 277 $this->assertSame( 4, $a->get_call_count() ); 276 278 // Only our hook was called. 277 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_ tags() );279 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() ); 278 280 279 281 remove_action( 'all', array( &$a, 'action' ) ); … … 295 297 // Make sure our hook was called correctly. 296 298 $this->assertSame( 1, $a->get_call_count() ); 297 $this->assertSame( array( $hook_name ), $a->get_ tags() );299 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 298 300 299 301 // Now remove the action, do it again, and make sure it's not called this time. … … 302 304 do_action( $hook_name ); 303 305 $this->assertSame( 1, $a->get_call_count() ); 304 $this->assertSame( array( $hook_name ), $a->get_ tags() );306 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 305 307 } 306 308 -
trunk/tests/phpunit/tests/filters.php
r53804 r53805 19 19 $this->assertSame( 1, $a->get_call_count() ); 20 20 // Only our hook was called. 21 $this->assertSame( array( $hook_name ), $a->get_ tags() );21 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 22 22 23 23 $argsvar = $a->get_args(); … … 36 36 // Make sure our hook was called correctly. 37 37 $this->assertSame( 1, $a->get_call_count() ); 38 $this->assertSame( array( $hook_name ), $a->get_ tags() );38 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 39 39 40 40 // Now remove the filter, do it again, and make sure it's not called this time. … … 42 42 $this->assertSame( $val, apply_filters( $hook_name, $val ) ); 43 43 $this->assertSame( 1, $a->get_call_count() ); 44 $this->assertSame( array( $hook_name ), $a->get_ tags() );44 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 45 45 46 46 } … … 136 136 // 'filter2' is called first because it has priority 9. 137 137 array( 138 'filter' => 'filter2', 139 'tag' => $hook_name, 140 'args' => array( $val ), 138 'filter' => 'filter2', 139 'hook_name' => $hook_name, 140 'tag' => $hook_name, // Back compat. 141 'args' => array( $val ), 141 142 ), 142 143 // 'filter' is called second. 143 144 array( 144 'filter' => 'filter', 145 'tag' => $hook_name, 146 'args' => array( $val ), 145 'filter' => 'filter', 146 'hook_name' => $hook_name, 147 'tag' => $hook_name, // Back compat. 148 'args' => array( $val ), 147 149 ), 148 150 ); … … 193 195 $this->assertSame( 4, $a->get_call_count() ); 194 196 // The right hooks should have been called in order. 195 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_ tags() );197 $this->assertSame( array( $hook_name1, $hook_name2, $hook_name1, $hook_name1 ), $a->get_hook_names() ); 196 198 197 199 remove_filter( 'all', array( $a, 'filterall' ) ); … … 212 214 // Make sure our hook was called correctly. 213 215 $this->assertSame( 1, $a->get_call_count() ); 214 $this->assertSame( array( $hook_name ), $a->get_ tags() );216 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 215 217 216 218 // Now remove the filter, do it again, and make sure it's not called this time. … … 221 223 // Call cound should remain at 1. 222 224 $this->assertSame( 1, $a->get_call_count() ); 223 $this->assertSame( array( $hook_name ), $a->get_ tags() );225 $this->assertSame( array( $hook_name ), $a->get_hook_names() ); 224 226 } 225 227 -
trunk/tests/phpunit/tests/user/author.php
r53147 r53805 174 174 175 175 $this->assertSame( 1, $filter->get_call_count() ); 176 $this->assertSame( array( 'the_author_link' ), $filter->get_ tags() );176 $this->assertSame( array( 'the_author_link' ), $filter->get_hook_names() ); 177 177 } 178 178 }
Note: See TracChangeset
for help on using the changeset viewer.