From 59aca3f427c2d2549d193f571cdf00741323502a Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Mon, 6 Jan 2025 13:51:30 +0200 Subject: [PATCH 01/11] Fix container registration actions to only fire once or not at all when inactive controller --- src/Common/Contracts/Container.php | 75 +++++++++++-------- .../Provider/AlreadyRegisteredException.php | 23 ++++++ src/Common/Contracts/Provider/Controller.php | 20 ++++- .../Provider/ControllerInactiveException.php | 23 ++++++ 4 files changed, 106 insertions(+), 35 deletions(-) create mode 100644 src/Common/Contracts/Provider/AlreadyRegisteredException.php create mode 100644 src/Common/Contracts/Provider/ControllerInactiveException.php diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index 903f101650..192acadc4f 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -6,6 +6,8 @@ use TEC\Common\Exceptions\Not_Bound_Exception; use TEC\Common\lucatume\DI52\Container as DI52_Container; +use TEC\Common\Contracts\Provider\AlreadyRegisteredException; +use TEC\Common\Contracts\Provider\ControllerInactiveException; class Container extends DI52_Container implements ContainerInterface { /** @@ -32,6 +34,7 @@ public function get( $id ) { * Overrides the parent method to fire an action when a service provider is registered. * * @since 5.1.4 + * @since TBD - Ensure registration actions are fired only once and ONLY for active controllers. * * @param string $serviceProviderClass The service provider class name. * @param string ...$alias Optional. The alias(es) to register the service provider with. @@ -42,43 +45,51 @@ public function get( $id ) { * does not provide a set of deferred registrations. */ public function register( $serviceProviderClass, ...$alias ) { - // Register the provider with the parent container. - parent::register( $serviceProviderClass, ...$alias ); + try { + // Register the provider with the parent container. + parent::register( $serviceProviderClass, ...$alias ); + + /** + * Fires when a service provider is registered by the container. + * + * @since 5.1.4 + * + * @param string $serviceProviderClass The service provider class name. + * @param array $alias The alias(es) the service provider was registered with. + */ + do_action( 'tec_container_registered_provider', $serviceProviderClass, $alias ); - /** - * Fires when a service provider is registered by the container. - * - * @since 5.1.4 - * - * @param string $serviceProviderClass The service provider class name. - * @param array $alias The alias(es) the service provider was registered with. - */ - do_action( 'tec_container_registered_provider', $serviceProviderClass, $alias ); + /** + * Fires a class-specific action when a service provider is registered by the container. + * + * @since 5.1.4 + * + * @param array $alias The alias(es) the service provider was registered with. + */ + do_action( 'tec_container_registered_provider_' . $serviceProviderClass, $alias ); - /** - * Fires a class-specific action when a service provider is registered by the container. - * - * @since 5.1.4 - * - * @param array $alias The alias(es) the service provider was registered with. - */ - do_action( 'tec_container_registered_provider_' . $serviceProviderClass, $alias ); + if ( + // Back compat with older definition of Service Provider. + ! property_exists( $serviceProviderClass, 'registration_action' ) + // New definition of Service Provider: default action is empty. + || empty( $serviceProviderClass::$registration_action ) + ) { + return; + } - if ( - // Back compat with older definition of Service Provider. - ! property_exists( $serviceProviderClass, 'registration_action' ) - // New definition of Service Provider: default action is empty. - || empty( $serviceProviderClass::$registration_action ) - ) { + /** + * Fires a custom action defined by the Service Provider when it's registered. + * + * @since 5.1.4 + */ + do_action( $serviceProviderClass::$registration_action, $serviceProviderClass, $alias ); + } catch ( AlreadyRegisteredException $registered_exception ) { + // If the container is registered already, DO NOT fire registration actions again. Instead silently return. + return; + } catch ( ControllerInactiveException $inactive_exception ) { + // If the controller is inactive, DO NOT fire registration actions AT ALL. Instead silently return. return; } - - /** - * Fires a custom action defined by the Service Provider when it's registered. - * - * @since 5.1.4 - */ - do_action( $serviceProviderClass::$registration_action, $serviceProviderClass, $alias ); } /** diff --git a/src/Common/Contracts/Provider/AlreadyRegisteredException.php b/src/Common/Contracts/Provider/AlreadyRegisteredException.php new file mode 100644 index 0000000000..51cdfc4b34 --- /dev/null +++ b/src/Common/Contracts/Provider/AlreadyRegisteredException.php @@ -0,0 +1,23 @@ +is_active() ) { - return; + throw new ControllerInactiveException( + sprintf( + 'The controller %s is inactive.', + static::class + ) + ); } $this->container->setVar( static::class . '_registered', true ); diff --git a/src/Common/Contracts/Provider/ControllerInactiveException.php b/src/Common/Contracts/Provider/ControllerInactiveException.php new file mode 100644 index 0000000000..3611405e99 --- /dev/null +++ b/src/Common/Contracts/Provider/ControllerInactiveException.php @@ -0,0 +1,23 @@ + Date: Wed, 8 Jan 2025 13:27:58 -0500 Subject: [PATCH 02/11] variable name snake case --- src/Common/Contracts/Container.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index 192acadc4f..1c8f48e05c 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -36,28 +36,28 @@ public function get( $id ) { * @since 5.1.4 * @since TBD - Ensure registration actions are fired only once and ONLY for active controllers. * - * @param string $serviceProviderClass The service provider class name. - * @param string ...$alias Optional. The alias(es) to register the service provider with. + * @param string $service_provider_class The service provider class name. + * @param string ...$alias Optional. The alias(es) to register the service provider with. * * @return void * * @throws \TEC\Common\lucatume\DI52\ContainerException If the provider class is marked as deferred but * does not provide a set of deferred registrations. */ - public function register( $serviceProviderClass, ...$alias ) { + public function register( $service_provider_class, ...$alias ) { try { // Register the provider with the parent container. - parent::register( $serviceProviderClass, ...$alias ); + parent::register( $service_provider_class, ...$alias ); /** * Fires when a service provider is registered by the container. * * @since 5.1.4 * - * @param string $serviceProviderClass The service provider class name. - * @param array $alias The alias(es) the service provider was registered with. + * @param string $service_provider_class The service provider class name. + * @param array $alias The alias(es) the service provider was registered with. */ - do_action( 'tec_container_registered_provider', $serviceProviderClass, $alias ); + do_action( 'tec_container_registered_provider', $service_provider_class, $alias ); /** * Fires a class-specific action when a service provider is registered by the container. @@ -66,13 +66,13 @@ public function register( $serviceProviderClass, ...$alias ) { * * @param array $alias The alias(es) the service provider was registered with. */ - do_action( 'tec_container_registered_provider_' . $serviceProviderClass, $alias ); + do_action( 'tec_container_registered_provider_' . $service_provider_class, $alias ); if ( // Back compat with older definition of Service Provider. - ! property_exists( $serviceProviderClass, 'registration_action' ) + ! property_exists( $service_provider_class, 'registration_action' ) // New definition of Service Provider: default action is empty. - || empty( $serviceProviderClass::$registration_action ) + || empty( $service_provider_class::$registration_action ) ) { return; } @@ -82,7 +82,7 @@ public function register( $serviceProviderClass, ...$alias ) { * * @since 5.1.4 */ - do_action( $serviceProviderClass::$registration_action, $serviceProviderClass, $alias ); + do_action( $service_provider_class::$registration_action, $service_provider_class, $alias ); } catch ( AlreadyRegisteredException $registered_exception ) { // If the container is registered already, DO NOT fire registration actions again. Instead silently return. return; From b770d324d52b94abe047ad53d2f19fd24e855748 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 9 Jan 2025 13:53:07 +0200 Subject: [PATCH 03/11] More the hooks outside of try catch block --- src/Common/Contracts/Container.php | 70 +++++++++++++++--------------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index 1c8f48e05c..f7a4efc9bd 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -48,41 +48,6 @@ public function register( $service_provider_class, ...$alias ) { try { // Register the provider with the parent container. parent::register( $service_provider_class, ...$alias ); - - /** - * Fires when a service provider is registered by the container. - * - * @since 5.1.4 - * - * @param string $service_provider_class The service provider class name. - * @param array $alias The alias(es) the service provider was registered with. - */ - do_action( 'tec_container_registered_provider', $service_provider_class, $alias ); - - /** - * Fires a class-specific action when a service provider is registered by the container. - * - * @since 5.1.4 - * - * @param array $alias The alias(es) the service provider was registered with. - */ - do_action( 'tec_container_registered_provider_' . $service_provider_class, $alias ); - - if ( - // Back compat with older definition of Service Provider. - ! property_exists( $service_provider_class, 'registration_action' ) - // New definition of Service Provider: default action is empty. - || empty( $service_provider_class::$registration_action ) - ) { - return; - } - - /** - * Fires a custom action defined by the Service Provider when it's registered. - * - * @since 5.1.4 - */ - do_action( $service_provider_class::$registration_action, $service_provider_class, $alias ); } catch ( AlreadyRegisteredException $registered_exception ) { // If the container is registered already, DO NOT fire registration actions again. Instead silently return. return; @@ -90,6 +55,41 @@ public function register( $service_provider_class, ...$alias ) { // If the controller is inactive, DO NOT fire registration actions AT ALL. Instead silently return. return; } + + /** + * Fires when a service provider is registered by the container. + * + * @since 5.1.4 + * + * @param string $service_provider_class The service provider class name. + * @param array $alias The alias(es) the service provider was registered with. + */ + do_action( 'tec_container_registered_provider', $service_provider_class, $alias ); + + /** + * Fires a class-specific action when a service provider is registered by the container. + * + * @since 5.1.4 + * + * @param array $alias The alias(es) the service provider was registered with. + */ + do_action( 'tec_container_registered_provider_' . $service_provider_class, $alias ); + + if ( + // Back compat with older definition of Service Provider. + ! property_exists( $service_provider_class, 'registration_action' ) + // New definition of Service Provider: default action is empty. + || empty( $service_provider_class::$registration_action ) + ) { + return; + } + + /** + * Fires a custom action defined by the Service Provider when it's registered. + * + * @since 5.1.4 + */ + do_action( $service_provider_class::$registration_action, $service_provider_class, $alias ); } /** From bad3f741c66343074733400f769d087e065e8d2b Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 21 Jan 2025 19:12:53 +0200 Subject: [PATCH 04/11] added changelog --- changelog/fix-container-registration-actions | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog/fix-container-registration-actions diff --git a/changelog/fix-container-registration-actions b/changelog/fix-container-registration-actions new file mode 100644 index 0000000000..6f61aee26f --- /dev/null +++ b/changelog/fix-container-registration-actions @@ -0,0 +1,4 @@ +Significance: minor +Type: fix + +Providers will fire their registration action only once and only if they are active. [TCMN-178] From 59582407e0daac396ea159c1d567aaaea7fab1f8 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Tue, 21 Jan 2025 19:39:54 +0200 Subject: [PATCH 05/11] test coverage for container changes --- .../Common/Contracts/Container_Test.php | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 tests/wpunit/Common/Contracts/Container_Test.php diff --git a/tests/wpunit/Common/Contracts/Container_Test.php b/tests/wpunit/Common/Contracts/Container_Test.php new file mode 100644 index 0000000000..d366dc0444 --- /dev/null +++ b/tests/wpunit/Common/Contracts/Container_Test.php @@ -0,0 +1,91 @@ +cloned_container = clone tribe(); + $this->assertNotSame( $this->cloned_container, tribe() ); + + $localized = $this->cloned_container; + $this->set_fn_return( 'tribe', fn( $service = null ) => $service ? $localized->get( $service ) : $localized, true ); + + $this->assertSame( $this->cloned_container, tribe() ); + } + + /** + * @test + */ + public function it_should_fire_registration_actions_only_once() { + $provider_class_name = Fake_Test_Controller::class; + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( Fake_Test_Controller::$registration_action ) ); + + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 1, did_action( Fake_Test_Controller::$registration_action ) ); + + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 1, did_action( Fake_Test_Controller::$registration_action ) ); + } + + /** + * @test + */ + public function it_should_not_fire_registration_actions_for_inactive() { + $provider_class_name = Fake_Test_Inactive_Controller::class; + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + } +} + +class Fake_Test_Controller extends Controller_Contract { + public static string $registration_action = 'foo_test_action'; + + protected function do_register(): void {} + + public function unregister(): void {} + + public function is_active(): bool { + return true; + } +} + +class Fake_Test_Inactive_Controller extends Fake_Test_Controller { + public function is_active(): bool { + return false; + } +} From c6596c674bb24e28b7050e536ce72a5a63b17d6f Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 23 Jan 2025 15:05:19 +0200 Subject: [PATCH 06/11] refactor code --- src/Common/Contracts/Container.php | 15 ++++-------- .../Provider/AlreadyRegisteredException.php | 23 ------------------- src/Common/Contracts/Provider/Controller.php | 18 ++------------- .../Provider/ControllerInactiveException.php | 23 ------------------- 4 files changed, 7 insertions(+), 72 deletions(-) delete mode 100644 src/Common/Contracts/Provider/AlreadyRegisteredException.php delete mode 100644 src/Common/Contracts/Provider/ControllerInactiveException.php diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index f7a4efc9bd..a0d7b3d882 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -6,8 +6,6 @@ use TEC\Common\Exceptions\Not_Bound_Exception; use TEC\Common\lucatume\DI52\Container as DI52_Container; -use TEC\Common\Contracts\Provider\AlreadyRegisteredException; -use TEC\Common\Contracts\Provider\ControllerInactiveException; class Container extends DI52_Container implements ContainerInterface { /** @@ -45,14 +43,11 @@ public function get( $id ) { * does not provide a set of deferred registrations. */ public function register( $service_provider_class, ...$alias ) { - try { - // Register the provider with the parent container. - parent::register( $service_provider_class, ...$alias ); - } catch ( AlreadyRegisteredException $registered_exception ) { - // If the container is registered already, DO NOT fire registration actions again. Instead silently return. - return; - } catch ( ControllerInactiveException $inactive_exception ) { - // If the controller is inactive, DO NOT fire registration actions AT ALL. Instead silently return. + // Register the provider with the parent container. + parent::register( $service_provider_class, ...$alias ); + + if ( ! $this->getVar( $service_provider_class . '_registered' ) ) { + // If the controller is not registered, DO NOT fire registration actions AT ALL. Instead silently return. return; } diff --git a/src/Common/Contracts/Provider/AlreadyRegisteredException.php b/src/Common/Contracts/Provider/AlreadyRegisteredException.php deleted file mode 100644 index 51cdfc4b34..0000000000 --- a/src/Common/Contracts/Provider/AlreadyRegisteredException.php +++ /dev/null @@ -1,23 +0,0 @@ -is_active() ) { - throw new ControllerInactiveException( - sprintf( - 'The controller %s is inactive.', - static::class - ) - ); + return; } $this->container->setVar( static::class . '_registered', true ); diff --git a/src/Common/Contracts/Provider/ControllerInactiveException.php b/src/Common/Contracts/Provider/ControllerInactiveException.php deleted file mode 100644 index 3611405e99..0000000000 --- a/src/Common/Contracts/Provider/ControllerInactiveException.php +++ /dev/null @@ -1,23 +0,0 @@ - Date: Thu, 23 Jan 2025 15:05:52 +0200 Subject: [PATCH 07/11] remove final keyword --- src/Common/Contracts/Provider/Controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Common/Contracts/Provider/Controller.php b/src/Common/Contracts/Provider/Controller.php index 8d7f32fad5..cacdd8f29c 100644 --- a/src/Common/Contracts/Provider/Controller.php +++ b/src/Common/Contracts/Provider/Controller.php @@ -31,7 +31,7 @@ abstract class Controller extends Service_Provider { * * @return void */ - final public function register() { + public function register() { /* * Look up and set the value in the container request cache to allow building the same Controller * with a **different** container. (e.g. in tests). From 2565ca54afc9389a0163e0c6dd84bae25e151ae5 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 23 Jan 2025 15:10:18 +0200 Subject: [PATCH 08/11] ensure we are bailing for controllers only --- src/Common/Contracts/Container.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index a0d7b3d882..21e2f5dc17 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -4,6 +4,7 @@ use TEC\Common\StellarWP\ContainerContract\ContainerInterface; use TEC\Common\Exceptions\Not_Bound_Exception; +use TEC\Common\Contracts\Provider\Controller; use TEC\Common\lucatume\DI52\Container as DI52_Container; @@ -43,11 +44,18 @@ public function get( $id ) { * does not provide a set of deferred registrations. */ public function register( $service_provider_class, ...$alias ) { + $is_controller = is_a( $service_provider_class, Controller::class, true ); + + if ( $is_controller && $this->getVar( $service_provider_class . '_registered', false ) ) { + // If the controller is already registered, bail. + return; + } + // Register the provider with the parent container. parent::register( $service_provider_class, ...$alias ); - if ( ! $this->getVar( $service_provider_class . '_registered' ) ) { - // If the controller is not registered, DO NOT fire registration actions AT ALL. Instead silently return. + if ( $is_controller && ! $this->getVar( $service_provider_class . '_registered', false ) ) { + // If the controller did not register, DO NOT fire registration actions AT ALL. Instead silently return. return; } From b73e0efbabd786c0a4c412cbcf368c15f0300467 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 23 Jan 2025 15:17:02 +0200 Subject: [PATCH 09/11] phpcs fix --- src/Common/Contracts/Container.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Common/Contracts/Container.php b/src/Common/Contracts/Container.php index 21e2f5dc17..82c5c107bb 100644 --- a/src/Common/Contracts/Container.php +++ b/src/Common/Contracts/Container.php @@ -44,7 +44,8 @@ public function get( $id ) { * does not provide a set of deferred registrations. */ public function register( $service_provider_class, ...$alias ) { - $is_controller = is_a( $service_provider_class, Controller::class, true ); + // Function is_a can be used with strings but instanceof only with objects! + $is_controller = is_a( $service_provider_class, Controller::class, true ); // phpcs:ignore StellarWP.PHP.IsAFunction.Found if ( $is_controller && $this->getVar( $service_provider_class . '_registered', false ) ) { // If the controller is already registered, bail. From 8aabb25e5cd986dba6d38be7712fecf24a6c83f9 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 23 Jan 2025 15:23:47 +0200 Subject: [PATCH 10/11] replace fake class names to anonymous --- .../Common/Contracts/Container_Test.php | 85 ++++++++++++++----- 1 file changed, 62 insertions(+), 23 deletions(-) diff --git a/tests/wpunit/Common/Contracts/Container_Test.php b/tests/wpunit/Common/Contracts/Container_Test.php index d366dc0444..824c6d5177 100644 --- a/tests/wpunit/Common/Contracts/Container_Test.php +++ b/tests/wpunit/Common/Contracts/Container_Test.php @@ -35,57 +35,96 @@ public function prepare() { * @test */ public function it_should_fire_registration_actions_only_once() { - $provider_class_name = Fake_Test_Controller::class; + $provider = new class( $this->cloned_container ) extends Controller_Contract { + public static string $registration_action = 'foo_test_action'; + + protected function do_register(): void {} + + public function unregister(): void {} + + public function is_active(): bool { + return true; + } + }; + + $provider_class_name = $provider::class; $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 0, did_action( Fake_Test_Controller::$registration_action ) ); + $this->assertEquals( 0, did_action( $provider::$registration_action ) ); $this->cloned_container->register( $provider_class_name ); $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 1, did_action( Fake_Test_Controller::$registration_action ) ); + $this->assertEquals( 1, did_action( $provider::$registration_action ) ); $this->cloned_container->register( $provider_class_name ); $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 1, did_action( Fake_Test_Controller::$registration_action ) ); + $this->assertEquals( 1, did_action( $provider::$registration_action ) ); } /** * @test */ - public function it_should_not_fire_registration_actions_for_inactive() { - $provider_class_name = Fake_Test_Inactive_Controller::class; + public function it_should_fire_registration_actions_only_once_even_after_unregistration() { + $provider = new class( $this->cloned_container ) extends Controller_Contract { + public static string $registration_action = 'foo_test_action'; + + protected function do_register(): void {} + + public function unregister(): void {} + + public function is_active(): bool { + return true; + } + }; + + $provider_class_name = $provider::class; $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + $this->assertEquals( 0, did_action( $provider::$registration_action ) ); $this->cloned_container->register( $provider_class_name ); - $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 1, did_action( $provider::$registration_action ) ); + $provider->unregister(); $this->cloned_container->register( $provider_class_name ); - $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); - $this->assertEquals( 0, did_action( Fake_Test_Inactive_Controller::$registration_action ) ); + $this->assertEquals( 1, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 1, did_action( $provider::$registration_action ) ); } -} -class Fake_Test_Controller extends Controller_Contract { - public static string $registration_action = 'foo_test_action'; + /** + * @test + */ + public function it_should_not_fire_registration_actions_for_inactive() { + $provider = new class( $this->cloned_container ) extends Controller_Contract { + public static string $registration_action = 'foo_test_action'; + + protected function do_register(): void {} - protected function do_register(): void {} + public function unregister(): void {} - public function unregister(): void {} + public function is_active(): bool { + return false; + } + }; - public function is_active(): bool { - return true; - } -} + $provider_class_name = $provider::class; + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( $provider::$registration_action ) ); -class Fake_Test_Inactive_Controller extends Fake_Test_Controller { - public function is_active(): bool { - return false; + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( $provider::$registration_action ) ); + + $this->cloned_container->register( $provider_class_name ); + + $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); + $this->assertEquals( 0, did_action( $provider::$registration_action ) ); } } From 0d1be2ad438580e02af53b0a6f43fa725fafa768 Mon Sep 17 00:00:00 2001 From: Dimitrios Pantazis Date: Thu, 23 Jan 2025 15:26:33 +0200 Subject: [PATCH 11/11] move to get_class for php 7.4 --- tests/wpunit/Common/Contracts/Container_Test.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/wpunit/Common/Contracts/Container_Test.php b/tests/wpunit/Common/Contracts/Container_Test.php index 824c6d5177..a506176b14 100644 --- a/tests/wpunit/Common/Contracts/Container_Test.php +++ b/tests/wpunit/Common/Contracts/Container_Test.php @@ -47,7 +47,7 @@ public function is_active(): bool { } }; - $provider_class_name = $provider::class; + $provider_class_name = get_class( $provider ); $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); $this->assertEquals( 0, did_action( $provider::$registration_action ) ); @@ -79,7 +79,7 @@ public function is_active(): bool { } }; - $provider_class_name = $provider::class; + $provider_class_name = get_class( $provider ); $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); $this->assertEquals( 0, did_action( $provider::$registration_action ) ); @@ -112,7 +112,7 @@ public function is_active(): bool { } }; - $provider_class_name = $provider::class; + $provider_class_name = get_class( $provider ); $this->assertEquals( 0, did_action( 'tec_container_registered_provider_' . $provider_class_name ) ); $this->assertEquals( 0, did_action( $provider::$registration_action ) );