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

Reply to filter 'with' anything #156

Open
Flynsarmy opened this issue Aug 19, 2020 · 1 comment
Open

Reply to filter 'with' anything #156

Flynsarmy opened this issue Aug 19, 2020 · 1 comment
Assignees

Comments

@Flynsarmy
Copy link

Is your feature request related to a problem? Please describe.
If I have a pre-defined array as my first filter argument in some method, there's no easy way to write a test for how the method responds when the values this method passes to the filter change. Here's an example.

I have a static method that calls apply_filters with a large pre-defined array as its first argument:

$extensions_to_ignore = [
    '.bat',
    '.crt',
    '.DS_Store',
    ...
];

$extensions_to_ignore = apply_filters('extensions_to_ignore', $extensions_to_ignore);

When writing a test for the method this filter takes place in, I need to write something like so:

// with() array must always match FilesHelper::extensionCheck()'s $extensions_to_ignore array
\WP_Mock::onFilter( 'extensions_to_ignore' )->with([
    '.bat',
    '.crt',
    '.DS_Store',
    ...
])->reply(['.unknown']);
// We've disallowed .unknown - test it
$expected = false;
$actual = FilesHelper::extensionCheck( "/path/to/foo.unknown" );
$this->assertEquals( $expected, $actual );

The problem occurs in that my test needs to always have an exact clone of the $extensions_to_ignore array from my application's method. If I ever change the application's array, this test breaks even though I don't care about the input from the filter.

Describe the solution you'd like
I'd like a way to create an onFilter that takes anything and responds with what i want. Something like:

\WP_Mock::onFilter( 'extensions_to_ignore' )->reply(['.unknown']);

^ This would accept any input to the extensions_to_ignore filter and return ['.unknown'].

Describe alternatives you've considered
Maybe something like this?

\WP_Mock::onFilter( 'extensions_to_ignore' )->withAnything()->reply(['.unknown']);
@rosswintle
Copy link

Just want to add a vote for this. If I have time I'll try to put together a PR, but please don't wait for me to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants