From ac776c335f5ce9f197ab0c68459e8f8fe5853eb6 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 25 Jun 2024 22:26:39 -0700 Subject: [PATCH 01/14] Add convenience functions for `:: expectActionRemoved()` `::expectFilterRemoved()` --- php/WP_Mock.php | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 49ef1f3..cb3cfbb 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -546,4 +546,50 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener { return static::$deprecatedMethodListener; } + + /** + * Adds an expectation that an action should be removed. + * + * @param string $action the action hook name + * @param string|callable-string|callable|Type $callback the callable to be removed + * @param ?int $priority the priority it should be registered at + * + * @return void + * @throws InvalidArgumentException + */ + public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : void + { + self::userFunction( + 'remove_action', + array( + 'args' => is_int($priority) + ? array($action, $callback, $priority) + : array($action, $callback), + 'times' => 1, + ) + ); + } + + /** + * Adds an expectation that a filter should be removed. + * + * @param string $action the filter name + * @param string|callable-string|callable|Type $callback the callable to be removed + * @param ?int $priority the registered priority + * + * @return void + * @throws InvalidArgumentException + */ + public static function expectFilterRemoved(string $action, $callback, ?int $priority = null) : void + { + self::userFunction( + 'remove_filter', + array( + 'args' => is_int($priority) + ? array($action, $callback, $priority) + : array($action, $callback), + 'times' => 1, + ) + ); + } } From 67017782c351c0fceb9d74c53545cb424e1566a1 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Tue, 25 Jun 2024 22:38:45 -0700 Subject: [PATCH 02/14] Rename `$action` to `$filter` in filter related function --- php/WP_Mock.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index cb3cfbb..c8e7a46 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -573,21 +573,21 @@ public static function expectActionRemoved(string $action, $callback, ?int $prio /** * Adds an expectation that a filter should be removed. * - * @param string $action the filter name + * @param string $filter the filter name * @param string|callable-string|callable|Type $callback the callable to be removed * @param ?int $priority the registered priority * * @return void * @throws InvalidArgumentException */ - public static function expectFilterRemoved(string $action, $callback, ?int $priority = null) : void + public static function expectFilterRemoved(string $filter, $callback, ?int $priority = null) : void { self::userFunction( 'remove_filter', array( 'args' => is_int($priority) - ? array($action, $callback, $priority) - : array($action, $callback), + ? array($filter, $callback, $priority) + : array($filter, $callback), 'times' => 1, ) ); From e0aa1ddc44d8cde0c4fad9680e8a0f72e636bcba Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 26 Jun 2024 18:50:42 -0700 Subject: [PATCH 03/14] Add expectHookRemoved tests --- php/WP_Mock.php | 8 ++------ tests/Integration/WP_MockTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index c8e7a46..31f32de 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -562,9 +562,7 @@ public static function expectActionRemoved(string $action, $callback, ?int $prio self::userFunction( 'remove_action', array( - 'args' => is_int($priority) - ? array($action, $callback, $priority) - : array($action, $callback), + 'args' => array_filter(func_get_args()), 'times' => 1, ) ); @@ -585,9 +583,7 @@ public static function expectFilterRemoved(string $filter, $callback, ?int $prio self::userFunction( 'remove_filter', array( - 'args' => is_int($priority) - ? array($filter, $callback, $priority) - : array($filter, $callback), + 'args' => array_filter(func_get_args()), 'times' => 1, ) ); diff --git a/tests/Integration/WP_MockTest.php b/tests/Integration/WP_MockTest.php index 04461fb..ad5f6e1 100644 --- a/tests/Integration/WP_MockTest.php +++ b/tests/Integration/WP_MockTest.php @@ -336,4 +336,29 @@ public function testCanExpectHooksNotAdded() : void $this->assertConditionsMet(); } + + /** + * @covers \WP_Mock::expectActionRemoved() + * @covers \WP_Mock::expectFilterRemoved() + * + * @return void + * @throws Exception + */ + public function testCanExpectHooksRemoved() : void + { + WP_Mock::expectActionRemoved('wpMockTestActionWithCallbackAndPriority', 'wpMockTestFunction', 10); + WP_Mock::expectFilterRemoved('wpMockTestFilterWithCallbackAndPriority', 'wpMockTestFunction', 10); + + WP_Mock::expectActionRemoved('wpMockTestActionWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + WP_Mock::expectFilterRemoved('wpMockTestFilterWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + + remove_action('wpMockTestActionWithCallbackAndPriority', 'wpMockTestFunction', 10); + remove_filter('wpMockTestFilterWithCallbackAndPriority', 'wpMockTestFunction', 10); + + remove_action('wpMockTestActionWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + remove_filter('wpMockTestFilterWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + + $this->assertConditionsMet(); + } + } } From 22898a2cefa40e294a47508489c88284026345b7 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 26 Jun 2024 19:02:02 -0700 Subject: [PATCH 04/14] Add expectHookNotRemoved functions --- php/WP_Mock.php | 42 +++++++++++++++++++++++++++++++ tests/Integration/WP_MockTest.php | 17 +++++++++++++ 2 files changed, 59 insertions(+) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 31f32de..522de6f 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -568,6 +568,27 @@ public static function expectActionRemoved(string $action, $callback, ?int $prio ); } + /** + * Adds an expectation that an action should not be removed. + * + * @param string $action the action hook name + * @param null|string|callable-string|callable|Type $callback the callable to be removed + * @param ?int $priority optional priority for the registered callback that is being removed + * + * @return void + * @throws InvalidArgumentException + */ + public static function expectActionNotRemoved(string $action, $callback, $priority = null) : void + { + self::userFunction( + 'remove_action', + array( + 'args' => array_filter(func_get_args()), + 'times' => 0, + ) + ); + } + /** * Adds an expectation that a filter should be removed. * @@ -588,4 +609,25 @@ public static function expectFilterRemoved(string $filter, $callback, ?int $prio ) ); } + + /** + * Adds an expectation that a filter should not be removed. + * + * @param string $filter the filter name + * @param null|string|callable-string|callable|Type $callback the callable to be removed + * @param ?int $priority optional priority for the registered callback that is being removed + * + * @return void + * @throws InvalidArgumentException + */ + public static function expectFilterNotRemoved(string $filter, $callback, $priority = null) : void + { + self::userFunction( + 'remove_filter', + array( + 'args' => array_filter(func_get_args()), + 'times' => 0, + ) + ); + } } diff --git a/tests/Integration/WP_MockTest.php b/tests/Integration/WP_MockTest.php index ad5f6e1..efa3abc 100644 --- a/tests/Integration/WP_MockTest.php +++ b/tests/Integration/WP_MockTest.php @@ -360,5 +360,22 @@ public function testCanExpectHooksRemoved() : void $this->assertConditionsMet(); } + + /** + * @covers \WP_Mock::expectActionNotRemoved() + * @covers \WP_Mock::expectFilterNotRemoved() + * + * @return void + * @throws Exception + */ + public function testCanExpectHooksNotRemoved() : void + { + WP_Mock::expectActionNotRemoved('wpMockTestActionWithCallbackAndPriority', 'wpMockTestFunction', 10); + WP_Mock::expectFilterNotRemoved('wpMockTestFilterWithCallbackAndPriority', 'wpMockTestFunction', 10); + + WP_Mock::expectActionNotRemoved('wpMockTestActionWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + WP_Mock::expectFilterNotRemoved('wpMockTestFilterWithCallbackAndDefaultPriority', 'wpMockTestFunction'); + + $this->assertConditionsMet(); } } From 7d01fcb251bd6be87396602422d13a810ec58b4b Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 26 Jun 2024 19:10:47 -0700 Subject: [PATCH 05/14] Remove strict typing to allow `::type( 'int' )` `\WP_Mock\Functions::type( 'int' )` --- php/WP_Mock.php | 12 ++++++------ tests/Integration/WP_MockTest.php | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 522de6f..5f70ea2 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -552,12 +552,12 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener * * @param string $action the action hook name * @param string|callable-string|callable|Type $callback the callable to be removed - * @param ?int $priority the priority it should be registered at + * @param ?int|Type $priority the priority it should be registered at * * @return void * @throws InvalidArgumentException */ - public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : void + public static function expectActionRemoved(string $action, $callback, $priority = null) : void { self::userFunction( 'remove_action', @@ -573,7 +573,7 @@ public static function expectActionRemoved(string $action, $callback, ?int $prio * * @param string $action the action hook name * @param null|string|callable-string|callable|Type $callback the callable to be removed - * @param ?int $priority optional priority for the registered callback that is being removed + * @param ?int|Type $priority optional priority for the registered callback that is being removed * * @return void * @throws InvalidArgumentException @@ -594,12 +594,12 @@ public static function expectActionNotRemoved(string $action, $callback, $priori * * @param string $filter the filter name * @param string|callable-string|callable|Type $callback the callable to be removed - * @param ?int $priority the registered priority + * @param ?int|Type $priority the registered priority * * @return void * @throws InvalidArgumentException */ - public static function expectFilterRemoved(string $filter, $callback, ?int $priority = null) : void + public static function expectFilterRemoved(string $filter, $callback, $priority = null) : void { self::userFunction( 'remove_filter', @@ -615,7 +615,7 @@ public static function expectFilterRemoved(string $filter, $callback, ?int $prio * * @param string $filter the filter name * @param null|string|callable-string|callable|Type $callback the callable to be removed - * @param ?int $priority optional priority for the registered callback that is being removed + * @param ?int|Type $priority optional priority for the registered callback that is being removed * * @return void * @throws InvalidArgumentException diff --git a/tests/Integration/WP_MockTest.php b/tests/Integration/WP_MockTest.php index efa3abc..a13a512 100644 --- a/tests/Integration/WP_MockTest.php +++ b/tests/Integration/WP_MockTest.php @@ -349,12 +349,26 @@ public function testCanExpectHooksRemoved() : void WP_Mock::expectActionRemoved('wpMockTestActionWithCallbackAndPriority', 'wpMockTestFunction', 10); WP_Mock::expectFilterRemoved('wpMockTestFilterWithCallbackAndPriority', 'wpMockTestFunction', 10); + WP_Mock::expectActionRemoved( + 'wpMockTestActionWithCallbackAndAnyPriority', + 'wpMockTestFunction', + \WP_Mock\Functions::type('int') + ); + WP_Mock::expectFilterRemoved( + 'wpMockTestFilterWithCallbackAndAnyPriority', + 'wpMockTestFunction', + \WP_Mock\Functions::type('int') + ); + WP_Mock::expectActionRemoved('wpMockTestActionWithCallbackAndDefaultPriority', 'wpMockTestFunction'); WP_Mock::expectFilterRemoved('wpMockTestFilterWithCallbackAndDefaultPriority', 'wpMockTestFunction'); remove_action('wpMockTestActionWithCallbackAndPriority', 'wpMockTestFunction', 10); remove_filter('wpMockTestFilterWithCallbackAndPriority', 'wpMockTestFunction', 10); + remove_action('wpMockTestActionWithCallbackAndAnyPriority', 'wpMockTestFunction', rand(1,100)); + remove_filter('wpMockTestFilterWithCallbackAndAnyPriority', 'wpMockTestFunction', rand(1,100)); + remove_action('wpMockTestActionWithCallbackAndDefaultPriority', 'wpMockTestFunction'); remove_filter('wpMockTestFilterWithCallbackAndDefaultPriority', 'wpMockTestFunction'); From cd4cff50eca56e67491aafbd772f0e7e988bb388 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Wed, 26 Jun 2024 19:14:29 -0700 Subject: [PATCH 06/14] update functions' type annotations --- php/WP_Mock.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 5f70ea2..e73a5f9 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -552,7 +552,7 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener * * @param string $action the action hook name * @param string|callable-string|callable|Type $callback the callable to be removed - * @param ?int|Type $priority the priority it should be registered at + * @param int|Type|null $priority the priority it should be registered at * * @return void * @throws InvalidArgumentException @@ -573,7 +573,7 @@ public static function expectActionRemoved(string $action, $callback, $priority * * @param string $action the action hook name * @param null|string|callable-string|callable|Type $callback the callable to be removed - * @param ?int|Type $priority optional priority for the registered callback that is being removed + * @param int|Type|null $priority optional priority for the registered callback that is being removed * * @return void * @throws InvalidArgumentException @@ -594,7 +594,7 @@ public static function expectActionNotRemoved(string $action, $callback, $priori * * @param string $filter the filter name * @param string|callable-string|callable|Type $callback the callable to be removed - * @param ?int|Type $priority the registered priority + * @param int|Type|null $priority the registered priority * * @return void * @throws InvalidArgumentException @@ -615,7 +615,7 @@ public static function expectFilterRemoved(string $filter, $callback, $priority * * @param string $filter the filter name * @param null|string|callable-string|callable|Type $callback the callable to be removed - * @param ?int|Type $priority optional priority for the registered callback that is being removed + * @param int|Type|null $priority optional priority for the registered callback that is being removed * * @return void * @throws InvalidArgumentException From f56c5575258aaa920acd18b87986f4011af1a04e Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 27 Jun 2024 15:51:56 -0700 Subject: [PATCH 07/14] Add doc: Asserting that actions and filters have been removed --- .../mocking-wp-action-and-filter-hooks.md | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/usage/mocking-wp-action-and-filter-hooks.md b/docs/usage/mocking-wp-action-and-filter-hooks.md index fde8a9b..973ec40 100644 --- a/docs/usage/mocking-wp-action-and-filter-hooks.md +++ b/docs/usage/mocking-wp-action-and-filter-hooks.md @@ -140,4 +140,25 @@ final class MyClassTest extends TestCase $this->assertEquals('Default', (new MyClass())->filterContent()); } } -``` \ No newline at end of file +``` + +## Asserting that actions and filters have been removed + +Similarly, we can test that actions and filters are removed when expected, e.g.removing another plugin's admin notice. This is done using `WP_Mock::expectActionRemoved()` and `WP_Mock::expectFilterRemoved()`. Or conversely, we can confirm that they have _not_ been removed using `WP_Mock::expectActionNotRemoved()` and `WP_Mock::expectFilterNotRemoved()`. The latter functions are useful where a function being tested returns early in some scenarios, and we want to ensure that the hooks are not removed in that case. + +```php +use MyPlugin\MyClass; +use WP_Mock\Tools\TestCase as TestCase; + +final class MyClassTest extends TestCase +{ + public function testRemoveAction() : void + { + $classInstance = new MyClass(); + + WP_Mock::expectActionRemoved('admin_notices', 'invasive_admin_notice'); + + $classInstance->removeInvasiveAdminNotice(); + } +} +``` From e8bf51fe6370ed3114b021cdb15844f6b64ffe87 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 16:16:29 -0800 Subject: [PATCH 08/14] Update php/WP_Mock.php Co-authored-by: Nabeel Molham <72820458+nmolham-godaddy@users.noreply.github.com> --- php/WP_Mock.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index e73a5f9..da6d309 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -554,10 +554,10 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener * @param string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority the priority it should be registered at * - * @return void + * @return Mockery\Expectation * @throws InvalidArgumentException */ - public static function expectActionRemoved(string $action, $callback, $priority = null) : void + public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : Mockery\Expectation { self::userFunction( 'remove_action', From 9cf0f35984f5ca7144975f327c9b0e6431246c7f Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 16:16:57 -0800 Subject: [PATCH 09/14] Update php/WP_Mock.php Co-authored-by: Nabeel Molham <72820458+nmolham-godaddy@users.noreply.github.com> --- php/WP_Mock.php | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index da6d309..169e1de 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -559,13 +559,15 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener */ public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : Mockery\Expectation { - self::userFunction( - 'remove_action', - array( - 'args' => array_filter(func_get_args()), - 'times' => 1, - ) - ); + $args = [$action, $callback]; + if ($priority) { + $args[] = $priority; + } + + return self::userFunction('remove_action', [ + 'args' => $args, + 'times' => 1, + ]); } /** From 9d326338ba0e88a06e1c3fc1ea7a5771bb4d80f7 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 16:59:08 -0800 Subject: [PATCH 10/14] Correct PhpDoc return to match `::setUpMock()`'s `Mockery\Expectation|Mockery\CompositeExpectation` --- php/WP_Mock/Functions.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/php/WP_Mock/Functions.php b/php/WP_Mock/Functions.php index 6a5a77d..d03f607 100644 --- a/php/WP_Mock/Functions.php +++ b/php/WP_Mock/Functions.php @@ -92,7 +92,7 @@ public function flush(): void * * @param string|callable-string $function function name * @param array $args optional arguments - * @return Mockery\Expectation + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ public function register(string $function, array $args = []) @@ -110,7 +110,7 @@ public function register(string $function, array $args = []) /** @var callable-string $method */ $method = preg_replace('/\\\\+/', '_', $function); - /** @var Mockery\Expectation $expectation */ + /** @var Mockery\Expectation|Mockery\CompositeExpectation $expectation */ $expectation = $this->setUpMock($mock, $method, $args); Handler::registerHandler($function, [$mock, $method]); From 0acdb11e0ffd91ff621de63d6a4ba1b50d8ab722 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 16:59:26 -0800 Subject: [PATCH 11/14] Apply PR review suggestions --- php/WP_Mock.php | 68 +++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 31 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 169e1de..28673f9 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -439,7 +439,7 @@ public static function assertHooksAdded() : void * * @param string $function function name * @param mixed[] $args optional arguments to set expectations - * @return Mockery\Expectation + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ public static function userFunction(string $function, array $args = []) @@ -554,13 +554,13 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener * @param string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority the priority it should be registered at * - * @return Mockery\Expectation + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ - public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : Mockery\Expectation + public static function expectActionRemoved(string $action, $callback, $priority = null) { $args = [$action, $callback]; - if ($priority) { + if (!is_null($priority)) { $args[] = $priority; } @@ -577,18 +577,20 @@ public static function expectActionRemoved(string $action, $callback, ?int $prio * @param null|string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority optional priority for the registered callback that is being removed * - * @return void + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ - public static function expectActionNotRemoved(string $action, $callback, $priority = null) : void + public static function expectActionNotRemoved(string $action, $callback, $priority = null) { - self::userFunction( - 'remove_action', - array( - 'args' => array_filter(func_get_args()), - 'times' => 0, - ) - ); + $args = [$action, $callback]; + if (!is_null($priority)) { + $args[] = $priority; + } + + return self::userFunction('remove_action',[ + 'args' => $args, + 'times' => 0, + ]); } /** @@ -598,18 +600,20 @@ public static function expectActionNotRemoved(string $action, $callback, $priori * @param string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority the registered priority * - * @return void + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ - public static function expectFilterRemoved(string $filter, $callback, $priority = null) : void + public static function expectFilterRemoved(string $filter, $callback, $priority = null) { - self::userFunction( - 'remove_filter', - array( - 'args' => array_filter(func_get_args()), - 'times' => 1, - ) - ); + $args = [$filter, $callback]; + if (!is_null($priority)) { + $args[] = $priority; + } + + return self::userFunction('remove_filter',[ + 'args' => $args, + 'times' => 1, + ]); } /** @@ -619,17 +623,19 @@ public static function expectFilterRemoved(string $filter, $callback, $priority * @param null|string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority optional priority for the registered callback that is being removed * - * @return void + * @return Mockery\Expectation|Mockery\CompositeExpectation * @throws InvalidArgumentException */ - public static function expectFilterNotRemoved(string $filter, $callback, $priority = null) : void + public static function expectFilterNotRemoved(string $filter, $callback, $priority = null) { - self::userFunction( - 'remove_filter', - array( - 'args' => array_filter(func_get_args()), - 'times' => 0, - ) - ); + $args = [$filter, $callback]; + if (!is_null($priority)) { + $args[] = $priority; + } + + return self::userFunction('remove_filter',[ + 'args' => $args, + 'times' => 0, + ]); } } From 1c4ce4a0c69808080db718fbd80b3550da194976 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 16:59:50 -0800 Subject: [PATCH 12/14] Add `\WP_Mock\Functions::type( 'int' )` suggestion to `expectFilterNotRemoved()` --- docs/usage/mocking-wp-action-and-filter-hooks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/usage/mocking-wp-action-and-filter-hooks.md b/docs/usage/mocking-wp-action-and-filter-hooks.md index 973ec40..3b680e0 100644 --- a/docs/usage/mocking-wp-action-and-filter-hooks.md +++ b/docs/usage/mocking-wp-action-and-filter-hooks.md @@ -144,7 +144,7 @@ final class MyClassTest extends TestCase ## Asserting that actions and filters have been removed -Similarly, we can test that actions and filters are removed when expected, e.g.removing another plugin's admin notice. This is done using `WP_Mock::expectActionRemoved()` and `WP_Mock::expectFilterRemoved()`. Or conversely, we can confirm that they have _not_ been removed using `WP_Mock::expectActionNotRemoved()` and `WP_Mock::expectFilterNotRemoved()`. The latter functions are useful where a function being tested returns early in some scenarios, and we want to ensure that the hooks are not removed in that case. +Similarly, we can test that actions and filters are removed when expected, e.g.removing another plugin's admin notice. This is done using `WP_Mock::expectActionRemoved()` and `WP_Mock::expectFilterRemoved()`. Or conversely, we can confirm that they have _not_ been removed using `WP_Mock::expectActionNotRemoved()` and `WP_Mock::expectFilterNotRemoved()`. The latter functions are useful where a function being tested returns early in some scenarios, and we want to ensure that the hooks are not removed in that case. NB: omitting the priority does not catch any uses of `remove_action()` that use an explicit priority, for that case consider using `\WP_Mock\Functions::type( 'int' )`. ```php use MyPlugin\MyClass; From ac49b27707de02321b04c241d2aeb184f16804b3 Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 23:05:32 -0800 Subject: [PATCH 13/14] PHPStan --- php/WP_Mock.php | 17 +++++++++-------- php/WP_Mock/Functions.php | 4 ++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 28673f9..423610f 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -8,6 +8,7 @@ */ use Mockery\Exception as MockeryException; +use Mockery\ExpectationInterface; use WP_Mock\DeprecatedMethodListener; use WP_Mock\Functions\Handler; use WP_Mock\Matcher\FuzzyObject; @@ -439,7 +440,7 @@ public static function assertHooksAdded() : void * * @param string $function function name * @param mixed[] $args optional arguments to set expectations - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return Mockery\Expectation * @throws InvalidArgumentException */ public static function userFunction(string $function, array $args = []) @@ -458,7 +459,7 @@ public static function userFunction(string $function, array $args = []) * * @param string $function function name * @param mixed[]|scalar $args optional arguments - * @return Mockery\Expectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function echoFunction(string $function, $args = []) @@ -483,7 +484,7 @@ public static function echoFunction(string $function, $args = []) * * @param string $function function name * @param mixed[]|scalar $args function arguments (optional) - * @return Mockery\Expectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function passthruFunction(string $function, $args = []) @@ -505,7 +506,7 @@ public static function passthruFunction(string $function, $args = []) * @param string|callable-string $function function to alias * @param string|callable-string $aliasFunction actual function * @param mixed[]|scalar $args optional arguments - * @return Mockery\Expectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function alias(string $function, string $aliasFunction, $args = []) @@ -554,7 +555,7 @@ public static function getDeprecatedMethodListener(): DeprecatedMethodListener * @param string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority the priority it should be registered at * - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function expectActionRemoved(string $action, $callback, $priority = null) @@ -577,7 +578,7 @@ public static function expectActionRemoved(string $action, $callback, $priority * @param null|string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority optional priority for the registered callback that is being removed * - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function expectActionNotRemoved(string $action, $callback, $priority = null) @@ -600,7 +601,7 @@ public static function expectActionNotRemoved(string $action, $callback, $priori * @param string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority the registered priority * - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function expectFilterRemoved(string $filter, $callback, $priority = null) @@ -623,7 +624,7 @@ public static function expectFilterRemoved(string $filter, $callback, $priority * @param null|string|callable-string|callable|Type $callback the callable to be removed * @param int|Type|null $priority optional priority for the registered callback that is being removed * - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return ExpectationInterface * @throws InvalidArgumentException */ public static function expectFilterNotRemoved(string $filter, $callback, $priority = null) diff --git a/php/WP_Mock/Functions.php b/php/WP_Mock/Functions.php index 5fc460e..ddf5cf6 100644 --- a/php/WP_Mock/Functions.php +++ b/php/WP_Mock/Functions.php @@ -92,7 +92,7 @@ public function flush(): void * * @param string|callable-string $function function name * @param array $args optional arguments - * @return Mockery\Expectation|Mockery\CompositeExpectation + * @return Mockery\Expectation * @throws InvalidArgumentException */ public function register(string $function, array $args = []) @@ -110,7 +110,7 @@ public function register(string $function, array $args = []) /** @var callable-string $method */ $method = preg_replace('/\\\\+/', '_', $function); - /** @var Mockery\Expectation|Mockery\CompositeExpectation $expectation */ + /** @var Mockery\Expectation $expectation */ $expectation = $this->setUpMock($mock, $method, $args); Handler::registerHandler($function, [$mock, $method]); From d6e40f7d932f1a355dcb5815eb5f6fad8d7224cb Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 15 Jan 2026 23:06:52 -0800 Subject: [PATCH 14/14] Undo unrelated type changes --- php/WP_Mock.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/php/WP_Mock.php b/php/WP_Mock.php index 423610f..f654949 100644 --- a/php/WP_Mock.php +++ b/php/WP_Mock.php @@ -459,7 +459,7 @@ public static function userFunction(string $function, array $args = []) * * @param string $function function name * @param mixed[]|scalar $args optional arguments - * @return ExpectationInterface + * @return Mockery\Expectation * @throws InvalidArgumentException */ public static function echoFunction(string $function, $args = []) @@ -484,7 +484,7 @@ public static function echoFunction(string $function, $args = []) * * @param string $function function name * @param mixed[]|scalar $args function arguments (optional) - * @return ExpectationInterface + * @return Mockery\Expectation * @throws InvalidArgumentException */ public static function passthruFunction(string $function, $args = []) @@ -506,7 +506,7 @@ public static function passthruFunction(string $function, $args = []) * @param string|callable-string $function function to alias * @param string|callable-string $aliasFunction actual function * @param mixed[]|scalar $args optional arguments - * @return ExpectationInterface + * @return Mockery\Expectation * @throws InvalidArgumentException */ public static function alias(string $function, string $aliasFunction, $args = [])