Skip to content

Commit

Permalink
Add doc: Asserting that actions and filters have been removed
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHenryIE committed Jun 27, 2024
1 parent cd4cff5 commit f56c557
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion docs/usage/mocking-wp-action-and-filter-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,25 @@ final class MyClassTest extends TestCase
$this->assertEquals('Default', (new MyClass())->filterContent());
}
}
```
```

## 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();
}
}
```

0 comments on commit f56c557

Please sign in to comment.