-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Controller_Test_Case - ensure PHPUnit invariance #2065
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,15 @@ | |
namespace TEC\Common\Tests\Provider; | ||
|
||
use Codeception\TestCase\WPTestCase; | ||
use RuntimeException; | ||
use TEC\Common\Contracts\Provider\Controller; | ||
use Tribe\Tests\Traits\With_Uopz; | ||
use Tribe__Container as Container; | ||
use TEC\Common\lucatume\DI52\Container as DI52_Container; | ||
use WP_Hook; | ||
|
||
/** | ||
* Class Controller_Test_Case. | ||
* | ||
* @since 5.0.17 | ||
* @since 5.0.17 | ||
* | ||
* @package TEC\Common\Tests\Provider; | ||
* @property string $controller_class The class name of the controller to test. | ||
|
@@ -79,7 +77,7 @@ class Controller_Test_Case extends WPTestCase { | |
* | ||
* @before | ||
*/ | ||
protected function setUpTestCase() { | ||
protected function set_up_controller_test_case(): void { | ||
// Ensure the test case defines the controller class to test. | ||
if ( ! property_exists( $this, 'controller_class' ) ) { | ||
throw new RuntimeException( 'Each Controller test case must define a controller_class property.' ); | ||
|
@@ -136,7 +134,7 @@ static function ( $key = null ) use ( $test_services ) { | |
* | ||
* @after | ||
*/ | ||
protected function tearDownTestCase() { | ||
protected function tear_down_controller_test_case(): void { | ||
// Unregister all the controllers created by the test case. | ||
foreach ( $this->made_controllers as $controller ) { | ||
$controller->unregister(); | ||
|
@@ -159,8 +157,15 @@ protected function tearDownTestCase() { | |
$this->original_services->setVar( $this->controller_class . '_registered', false ); | ||
$this->assertFalse( $this->original_controller::is_registered() ); | ||
|
||
// Re-register the original controller after the Service Locator has been reset. | ||
/* | ||
* Depending on the PHPUnit version, this method might run before of after the `WPTestCase::tearDown` method. | ||
* For this reason here we "anticipate" the hook cleanup call the `tearDown` method would run to restore the | ||
* hooks and go back to the hook initial state, register the controller, and then backup the hooks. | ||
*/ | ||
$this->_restore_hooks(); | ||
$this->original_controller->register(); | ||
$this->_backup_hooks(); | ||
|
||
$this->original_controller = null; | ||
} | ||
|
||
|
@@ -229,47 +234,8 @@ public function should_register_and_unregister_correctly(): void { | |
$added_filters = []; | ||
$controller_class = $this->controller_class; | ||
|
||
$this->set_fn_return( | ||
'add_filter', | ||
function ( | ||
string $tag, | ||
callable $callback, | ||
int $priority = 10, | ||
int $args = 1 | ||
) use ( | ||
$controller_class, | ||
&$added_filters | ||
) { | ||
if ( is_array( $callback ) && $callback[0] instanceof $controller_class ) { | ||
$added_filters[] = [ $tag, $callback, $priority ]; | ||
} | ||
add_filter( $tag, $callback, $priority, $args ); | ||
}, | ||
true | ||
); | ||
$this->set_fn_return( | ||
'remove_filter', | ||
function ( | ||
string $tag, | ||
callable $callback, | ||
int $priority = 10 | ||
) use ( | ||
$controller_class, | ||
&$added_filters | ||
) { | ||
if ( | ||
is_array( $callback ) | ||
&& $callback[0] instanceof $controller_class | ||
) { | ||
$found = array_search( [ $tag, $callback, $priority ], $added_filters, true ); | ||
if ( $found !== false ) { | ||
unset( $added_filters[ $found ] ); | ||
} | ||
} | ||
remove_filter( $tag, $callback, $priority ); | ||
}, | ||
true | ||
); | ||
$this->watch_added_filters( $added_filters ); | ||
$this->watch_removed_filters( $added_filters ); | ||
|
||
$controller->register(); | ||
$controller->unregister(); | ||
|
@@ -278,7 +244,7 @@ function ( | |
0, | ||
$added_filters, | ||
'The controller should have removed all its filters and actions: ' | ||
. PHP_EOL . wp_json_encode( $added_filters, JSON_PRETTY_PRINT ) | ||
. PHP_EOL . wp_json_encode( $this->controller_added_filters, JSON_PRETTY_PRINT ) | ||
); | ||
} | ||
|
||
|
@@ -398,4 +364,55 @@ function ( $tag, $function_to_add, $priority = 10 ) use ( &$hooked ) { | |
// The original controller should not be registered at this point. | ||
$this->assertFalse( $original_controller::is_registered() ); | ||
} | ||
|
||
/** | ||
* Watches, and logs, the filters added by the Controller. | ||
*/ | ||
private function watch_added_filters( array &$added_filters ): void { | ||
$controller_class = $this->controller_class; | ||
|
||
$this->set_fn_return( | ||
'add_filter', | ||
function ( | ||
string $tag, | ||
callable $callback, | ||
int $priority = 10, | ||
int $args = 1 | ||
) use ( $controller_class, &$added_filters ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
if ( is_array( $callback ) && $callback[0] instanceof $controller_class ) { | ||
$added_filters[] = [ $tag, $callback, $priority ]; | ||
} | ||
add_filter( $tag, $callback, $priority, $args ); | ||
}, | ||
true | ||
); | ||
} | ||
|
||
/** | ||
* Watches the filters removed by the Controller. | ||
*/ | ||
private function watch_removed_filters( &$added_filters ): void { | ||
$controller_class = $this->controller_class; | ||
|
||
$this->set_fn_return( | ||
'remove_filter', | ||
function ( | ||
string $tag, | ||
callable $callback, | ||
int $priority = 10 | ||
) use ( $controller_class, &$added_filters ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [phpcs] reported by reviewdog 🐶 |
||
if ( | ||
is_array( $callback ) | ||
&& $callback[0] instanceof $controller_class | ||
) { | ||
$found = array_search( [ $tag, $callback, $priority ], $added_filters, true ); | ||
if ( $found !== false ) { | ||
unset( $added_filters[ $found ] ); | ||
} | ||
} | ||
remove_filter( $tag, $callback, $priority ); | ||
}, | ||
true | ||
); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[phpcs] reported by reviewdog 🐶
Generic.WhiteSpace.ScopeIndent.IncorrectExact
Line indented incorrectly; expected 0 tabs, found 1