From f56c5575258aaa920acd18b87986f4011af1a04e Mon Sep 17 00:00:00 2001 From: Brian Henry Date: Thu, 27 Jun 2024 15:51:56 -0700 Subject: [PATCH] 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(); + } +} +```