From ed994e7c7e0cdf961929efb39f51daf297843790 Mon Sep 17 00:00:00 2001
From: Fabian Todt <mail@fabiantodt.at>
Date: Fri, 3 Sep 2021 10:11:46 +0200
Subject: [PATCH] fix MockAction::get_call_count correct usage & for filters
---
tests/phpunit/includes/utils.php | 7 ++++---
tests/phpunit/tests/rest-api/rest-controller.php | 14 +++++++-------
2 files changed, 11 insertions(+), 10 deletions(-)
diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php
index e37c3c15d66..ce6b9a1ed8d 100644
a
|
b
|
function strip_ws( $txt ) { |
58 | 58 | * add_action( 'foo', array( &$ma, 'action' ) ); |
59 | 59 | */ |
60 | 60 | class MockAction { |
| 61 | // stores all called actions/filters in an array, where `action`/`filter` is the callback and `tag` is the called hook |
61 | 62 | public $events; |
62 | 63 | public $debug; |
63 | 64 | |
… |
… |
function get_events() { |
168 | 169 | return $this->events; |
169 | 170 | } |
170 | 171 | |
171 | | // Return a count of the number of times the action was called since the last reset. |
| 172 | // Return a count of the number of times the action or filter ( = tag ) was called since the last reset. |
172 | 173 | function get_call_count( $tag = '' ) { |
173 | 174 | if ( $tag ) { |
174 | 175 | $count = 0; |
175 | 176 | foreach ( $this->events as $e ) { |
176 | | if ( $e['action'] === $tag ) { |
| 177 | if ( $e['tag'] === $tag ) { |
177 | 178 | ++$count; |
178 | 179 | } |
179 | 180 | } |
… |
… |
function get_call_count( $tag = '' ) { |
182 | 183 | return count( $this->events ); |
183 | 184 | } |
184 | 185 | |
185 | | // Return an array of the tags that triggered calls to this action. |
| 186 | // Return an array of the tags (actions or filters) that triggered calls to this action. |
186 | 187 | function get_tags() { |
187 | 188 | $out = array(); |
188 | 189 | foreach ( $this->events as $e ) { |
diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php
index 0af9460aa5d..a87dd331e65 100644
a
|
b
|
public function test_get_fields_for_response_filters_by_context() { |
392 | 392 | |
393 | 393 | $controller->prepare_item_for_response( array(), $request ); |
394 | 394 | |
395 | | $this->assertSame( 0, $listener->get_call_count( $method ) ); |
| 395 | $this->assertSame( 0, $listener->get_call_count() ); |
396 | 396 | |
397 | 397 | $request->set_param( 'context', 'embed' ); |
398 | 398 | |
399 | 399 | $controller->prepare_item_for_response( array(), $request ); |
400 | 400 | |
401 | | $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); |
| 401 | $this->assertGreaterThan( 0, $listener->get_call_count() ); |
402 | 402 | } |
403 | 403 | |
404 | 404 | public function test_filtering_fields_for_response_by_context_returns_fields_with_no_context() { |
… |
… |
public function test_filtering_fields_for_response_by_context_returns_fields_wit |
426 | 426 | |
427 | 427 | $controller->prepare_item_for_response( array(), $request ); |
428 | 428 | |
429 | | $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); |
| 429 | $this->assertGreaterThan( 0, $listener->get_call_count() ); |
430 | 430 | } |
431 | 431 | |
432 | 432 | public function test_filtering_fields_for_response_by_context_returns_fields_with_no_schema() { |
… |
… |
public function test_filtering_fields_for_response_by_context_returns_fields_wit |
451 | 451 | |
452 | 452 | $controller->prepare_item_for_response( array(), $request ); |
453 | 453 | |
454 | | $this->assertGreaterThan( 0, $listener->get_call_count( $method ) ); |
| 454 | $this->assertGreaterThan( 0, $listener->get_call_count() ); |
455 | 455 | } |
456 | 456 | |
457 | 457 | /** |
… |
… |
public function test_add_additional_fields_to_object_respects_fields_param() { |
511 | 511 | |
512 | 512 | $controller->prepare_item_for_response( $item, $request ); |
513 | 513 | |
514 | | $first_call_count = $listener->get_call_count( $method ); |
| 514 | $first_call_count = $listener->get_call_count(); |
515 | 515 | |
516 | 516 | $this->assertGreaterThan( 0, $first_call_count ); |
517 | 517 | |
… |
… |
public function test_add_additional_fields_to_object_respects_fields_param() { |
519 | 519 | |
520 | 520 | $controller->prepare_item_for_response( $item, $request ); |
521 | 521 | |
522 | | $this->assertSame( $first_call_count, $listener->get_call_count( $method ) ); |
| 522 | $this->assertSame( $first_call_count, $listener->get_call_count() ); |
523 | 523 | |
524 | 524 | $request->set_param( '_fields', $field ); |
525 | 525 | |
526 | 526 | $controller->prepare_item_for_response( $item, $request ); |
527 | 527 | |
528 | | $this->assertGreaterThan( $first_call_count, $listener->get_call_count( $method ) ); |
| 528 | $this->assertGreaterThan( $first_call_count, $listener->get_call_count() ); |
529 | 529 | } |
530 | 530 | |
531 | 531 | /** |