Skip to content

Commit

Permalink
test(Controller_Test_Case) ensure invariance across PHPUnit versions
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Apr 2, 2024
1 parent 3f13397 commit d395ff2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 35 deletions.
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
== Changelog ==

= [TBD] TBD =

* Fix - Ensure set up and tear down methods of the `Controller_Test_Case` will work independently of the PHPUnit version.

= [5.2.4] 2024-03-20 =

* Fix - Resolves a PHP 8.2 deprecation error on `Date_Utils` - `PHP Deprecated: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in /.../wp-content/plugins/the-events-calendar/common/src/Tribe/Date_Utils.php on line 256`. [ECP-1620]
Expand Down
104 changes: 69 additions & 35 deletions tests/_support/Provider/Controller_Test_Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -76,10 +74,10 @@ class Controller_Test_Case extends WPTestCase {
* @since TBD
*
* @return void
*
* @before
*/
protected function setUp() {
parent::setUp();

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.' );
Expand Down Expand Up @@ -129,8 +127,10 @@ protected function setUp() {
* @since TBD
*
* @return void
*
* @after
*/
protected function tearDown() {
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();
Expand All @@ -153,8 +153,15 @@ protected function tearDown() {
$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;

parent::tearDown();
Expand Down Expand Up @@ -221,32 +228,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();
Expand All @@ -255,7 +238,7 @@ public function should_register_and_unregister_correctly(): void {
0,
$added_filters,
'The controller should have removed all its filters and actions: '
. PHP_EOL . json_encode( $added_filters, JSON_PRETTY_PRINT )
. PHP_EOL . wp_json_encode( $this->controller_added_filters, JSON_PRETTY_PRINT )
);
}

Expand Down Expand Up @@ -371,4 +354,55 @@ protected function unregister_all_controller_instances( Controller $original_con
// 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 ) {
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 ) {
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
);
}
}

0 comments on commit d395ff2

Please sign in to comment.