Skip to content
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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 {
Copy link
Contributor

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

// 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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
Squiz.Functions.MultiLineFunctionDeclaration.UseOneParamPerLine
Multi-line use declarations must define one parameter per line

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 ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[phpcs] reported by reviewdog 🐶
Squiz.Functions.MultiLineFunctionDeclaration.UseOneParamPerLine
Multi-line use declarations must define one parameter per line

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
);
}
}