Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for withAnyArgs method for onFilter calls. #256

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/usage/mocking-wp-action-and-filter-hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,28 @@ final class MyClassTest extends TestCase
}
```

We can also use the `withAnyArgs` method to test that the filter is being applied. This is particularly useful in test cases where we do not care about the arguments but just its return value. This can be done like so:

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

final class MyClassTest extends TestCase
{
public function testCanFilterContent() : void
{
WP_Mock::onFilter('custom_content_filter')
->withAnyArgs()
->reply('This is filtered');

$content = (new MyClass())->filterContent();

$this->assertEquals('This is filtered', $content);
}
}
```

Alternatively, there is a method `WP_Mock::expectFilter()` that will add a bare assertion that the filter will be applied without changing the value:

```php
Expand Down
21 changes: 21 additions & 0 deletions php/WP_Mock/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
class Filter extends Hook
{
/** @var array<mixed> Collection of filter names mapped to random integers. */
protected static array $filtersWithAnyArgs = [];

/**
* Apply the stored filter.
*
Expand All @@ -18,6 +21,10 @@ class Filter extends Hook
*/
public function apply($args)
{
if (isset(static::$filtersWithAnyArgs[ $this->name ])) {
$args = array_values(static::$filtersWithAnyArgs);
}

if ($args[0] === null && count($args) === 1) {
if (isset($this->processors['argsnull'])) {
return $this->processors['argsnull']->send();
Expand All @@ -42,6 +49,9 @@ public function apply($args)
return call_user_func_array(array($processors, 'send'), $args);
}

/**
* @return Filter_Responder
*/
protected function new_responder()
{
return new Filter_Responder();
Expand All @@ -54,6 +64,17 @@ protected function get_strict_mode_message()
{
return sprintf('Unexpected use of apply_filters for filter %s', $this->name);
}

/**
* @return Action_Responder|Filter_Responder|HookedCallbackResponder
*/
public function withAnyArgs()
{
$random_value = mt_rand();
static::$filtersWithAnyArgs[ $this->name ] = $random_value;

return $this->with($random_value);
}
}

class Filter_Responder
Expand Down
50 changes: 50 additions & 0 deletions tests/Unit/WP_MockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,54 @@ public function testCanGetDeprecatedMethodListener(): void

Mockery::close();
}

/**
* @covers \WP_Mock::onFilter()
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
* @throws Exception|InvalidArgumentException
*/
public function testOnFilterPasses(): void
{
WP_Mock::bootstrap();

/** @phpstan-ignore-next-line */
WP_Mock::onFilter('testFilter')
->with('Original value')
->reply('Filtered value');

$filtered_value = apply_filters('testFilter', 'Original value');

$this->assertSame('Filtered value', $filtered_value);

Mockery::close();
}

/**
* @covers \WP_Mock::onFilter()
*
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* @return void
* @throws Exception|InvalidArgumentException
*/
public function testOnFilterPassesWithAnyArgs(): void
{
WP_Mock::bootstrap();

/** @phpstan-ignore-next-line */
WP_Mock::onFilter('testFilter')
->withAnyArgs()
->reply('Filtered value');

$filtered_value = apply_filters('testFilter', 'Original value');

$this->assertSame('Filtered value', $filtered_value);

Mockery::close();
}
}