Skip to content

Commit 8d6c23d

Browse files
Fix: Multiple onFilter call issues related to the withAnyArgs method (#259)
# Summary <!-- Required --> This PR implements a fix for an issue I discovered when using multiple `onFilter` calls with the `withAnyArgs` method. <img width="807" alt="Screenshot 2025-03-12 at 18 47 52" src="https://github.com/user-attachments/assets/3c1bb38a-830a-4a4a-907c-a61b7f0d8e11" /> ### Closes: #258 ## Details <!-- Optional --> While writing some unit test cases using the `withAnyArgs` method, I came across issues for multiple `onFilter` calls introduced into the tests. At the moment, if a single `onFilter` call is used alongside `withAnyArgs`, it works correctly, but for multiple cases, it doesn't correctly pass the tests. The expected behaviour should be that this works for both single and multiple use cases using the `withAnyArgs`. This PR fixes this issue correctly. <img width="587" alt="Screenshot 2025-03-12 at 19 42 19" src="https://github.com/user-attachments/assets/46cfd651-62a7-455b-9b58-257d38a78b84" /> --- Run tests: ```php vendor/bin/phpunit ./tests/Unit/WP_MockTest.php ``` ## Contributor checklist <!-- Required --> - [x] I agree to follow this project's [**Code of Conduct**](https://github.com/10up/.github/blob/trunk/CODE_OF_CONDUCT.md). - [x] I have updated the documentation accordingly - [x] I have added tests to cover changes introduced by this pull request - [x] All new and existing tests pass ## Testing <!-- Required --> ```php public function testMultipleOnFilterPassesWithAnyArgs(): void { WP_Mock::bootstrap(); /** @phpstan-ignore-next-line */ WP_Mock::onFilter('testFilter1') ->withAnyArgs() ->reply('Filtered value 1'); /** @phpstan-ignore-next-line */ WP_Mock::onFilter('testFilter2') ->withAnyArgs() ->reply('Filtered value 2'); /** @phpstan-ignore-next-line */ WP_Mock::onFilter('testFilter3') ->withAnyArgs() ->reply('Filtered value 3'); $filtered_value1 = apply_filters('testFilter1', 'Original value 1'); $filtered_value2 = apply_filters('testFilter2', 'Original value 2'); $filtered_value3 = apply_filters('testFilter3', 'Original value 3'); $this->assertSame('Filtered value 1', $filtered_value1); $this->assertSame('Filtered value 2', $filtered_value2); $this->assertSame('Filtered value 3', $filtered_value3); Mockery::close(); } ``` Run test, it should pass successfully multiple times using the `withAnyArgs` method: ```bash composer run test ``` <!-- List any configuration requirements for testing. --> ### Reviewer checklist <!-- Required --> <!-- The following checklist is for the reviewer: add any steps that may be relevant while reviewing this pull request --> - [x] Code changes review - [ ] Documentation changes review - [x] Unit tests pass - [x] Static analysis passes --------- Co-authored-by: Ashley Gibson <99189195+agibson-godaddy@users.noreply.github.com>
1 parent f25b589 commit 8d6c23d

2 files changed

Lines changed: 42 additions & 3 deletions

File tree

php/WP_Mock/Filter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class Filter extends Hook
1111
{
12-
/** @var array<mixed> Collection of filter names mapped to random integers. */
12+
/** @var array<string, array<int>> Collection of filter names mapped to random integers. */
1313
protected static array $filtersWithAnyArgs = [];
1414

1515
/**
@@ -22,7 +22,7 @@ class Filter extends Hook
2222
public function apply($args)
2323
{
2424
if (isset(static::$filtersWithAnyArgs[ $this->name ])) {
25-
$args = array_values(static::$filtersWithAnyArgs);
25+
$args = static::$filtersWithAnyArgs[ $this->name ];
2626
}
2727

2828
if ($args[0] === null && count($args) === 1) {
@@ -71,7 +71,7 @@ protected function get_strict_mode_message()
7171
public function withAnyArgs()
7272
{
7373
$random_value = mt_rand();
74-
static::$filtersWithAnyArgs[ $this->name ] = $random_value;
74+
static::$filtersWithAnyArgs[ $this->name ] = [ $random_value ];
7575

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

tests/Unit/WP_MockTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,4 +362,43 @@ public function testOnFilterPassesWithAnyArgs(): void
362362

363363
Mockery::close();
364364
}
365+
366+
/**
367+
* @covers \WP_Mock::onFilter()
368+
*
369+
* @runInSeparateProcess
370+
* @preserveGlobalState disabled
371+
*
372+
* @return void
373+
* @throws Exception|InvalidArgumentException
374+
*/
375+
public function testMultipleOnFilterPassesWithAnyArgs(): void
376+
{
377+
WP_Mock::bootstrap();
378+
379+
/** @phpstan-ignore-next-line */
380+
WP_Mock::onFilter('testFilter1')
381+
->withAnyArgs()
382+
->reply('Filtered value 1');
383+
384+
/** @phpstan-ignore-next-line */
385+
WP_Mock::onFilter('testFilter2')
386+
->withAnyArgs()
387+
->reply('Filtered value 2');
388+
389+
/** @phpstan-ignore-next-line */
390+
WP_Mock::onFilter('testFilter3')
391+
->withAnyArgs()
392+
->reply('Filtered value 3');
393+
394+
$filtered_value1 = apply_filters('testFilter1', 'Original value 1');
395+
$filtered_value2 = apply_filters('testFilter2', 'Original value 2');
396+
$filtered_value3 = apply_filters('testFilter3', 'Original value 3');
397+
398+
$this->assertSame('Filtered value 1', $filtered_value1);
399+
$this->assertSame('Filtered value 2', $filtered_value2);
400+
$this->assertSame('Filtered value 3', $filtered_value3);
401+
402+
Mockery::close();
403+
}
365404
}

0 commit comments

Comments
 (0)