Skip to content

Commit

Permalink
chore: update doc usage notes
Browse files Browse the repository at this point in the history
  • Loading branch information
badasswp committed Feb 1, 2025
1 parent 34b2e0f commit 76fdc61
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions docs/usage/mocking-wp-action-and-filter-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,51 @@ final class MyClassTest extends TestCase
$this->assertEquals('Default', (new MyClass())->filterContent());
}
}
```

## Asserting that an object has been passed

To assert that an object has been added as an argument, you can perform assertions referencing the object's class type.

Take the code below, for example:

```php
namespace MyPlugin;

class MyClass
{
public function filterContent() : NewClass
{
return apply_filters('custom_content_filter', new NewClass());
}
}

class NewClass
{
public function __construct()
{
echo 'New Class';
}
}
```

We can do this:

```php
use WP_Mock;
use WP_Mock\Tools\TestCase as TestCase;

use MyPlugin\MyClass;
use MyPlugin\NewClass;

final class MyClassTest extends TestCase
{
public function testAnonymousObject() : void
{
WP_Mock::expectFilter('custom_content_filter', WP_Mock\Functions::type(NewClass::class));

$this->assertInstanceOf(NewClass::class, (new MyClass())->filterContent());
$this->assertConditionsMet();
}
}
```

0 comments on commit 76fdc61

Please sign in to comment.