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 convenience ::expectFilterRemoved() ::expectActionRemoved() Fix #157 #246

Open
wants to merge 9 commits into
base: trunk
Choose a base branch
from

Conversation

BrianHenryIE
Copy link
Contributor

@BrianHenryIE BrianHenryIE commented Jun 26, 2024

Summary

As said in #157:

WP_Mock provides useful helpers for expectFilterAdded and expectFilterNotAdded, and [their] companion actions. However, we have no canonical way of checking remove_action nor remove_filter calls.

See: remove_filter().

Closes: #157

Details

It's really just a convenience function on top of WP_Mock::userFunction().

public static function expectFilterRemoved(string $filter, $callback, $priority = null) : void
{
    self::userFunction(
        'remove_filter'
        array(
            'args'   => array_filter(func_get_args()),
            'times'  => 1,
        )
    );
}

We're using array_filter() there to handle the optional use of $priority in remove_filter().

::expectFilterNotRemoved() is the same with 'times' => 0.

Contributor checklist

  • I agree to follow this project's Code of Conduct.
  • I have updated the documentation accordingly
  • I have added tests to cover changes introduced by this pull request
  • All new and existing tests pass

Testing

Reviewer checklist

  • Code changes review
  • Documentation changes review
  • Unit tests pass
  • Static analysis passes

@coveralls
Copy link

Coverage Status

coverage: 64.889% (-1.6%) from 66.445%
when pulling 6701778 on BrianHenryIE:add/remove-action-remove-filter
into 48b7f22 on 10up:trunk.

@coveralls
Copy link

Coverage Status

coverage: 67.522% (+1.1%) from 66.445%
when pulling 7d01fcb on BrianHenryIE:add/remove-action-remove-filter
into 48b7f22 on 10up:trunk.

@coveralls
Copy link

Coverage Status

coverage: 67.651% (+1.2%) from 66.445%
when pulling cd4cff5 on BrianHenryIE:add/remove-action-remove-filter
into 48b7f22 on 10up:trunk.

@coveralls
Copy link

Coverage Status

coverage: 67.651% (+1.2%) from 66.445%
when pulling f56c557 on BrianHenryIE:add/remove-action-remove-filter
into 48b7f22 on 10up:trunk.

@BrianHenryIE BrianHenryIE marked this pull request as ready for review June 27, 2024 22:54
@nmolham-godaddy nmolham-godaddy self-requested a review July 24, 2024 03:09
@coveralls
Copy link

coveralls commented Jul 24, 2024

Coverage Status

coverage: 67.651% (+1.3%) from 66.312%
when pulling 32b8898 on BrianHenryIE:add/remove-action-remove-filter
into 010d556 on 10up:trunk.

Copy link
Contributor

@nmolham-godaddy nmolham-godaddy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @BrianHenryIE for this nice PR, I have a couple of suggestions below, which I added for one method, but apply for all of the other new methods as well.

Comment on lines +557 to +560
* @return void
* @throws InvalidArgumentException
*/
public static function expectActionRemoved(string $action, $callback, $priority = null) : void
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about returning the expectation once set? incase it was needed to for later. As well as adding nullable-int data type for the $priority argument

Suggested change
* @return void
* @throws InvalidArgumentException
*/
public static function expectActionRemoved(string $action, $callback, $priority = null) : void
* @return Mockery\Expectation
* @throws InvalidArgumentException
*/
public static function expectActionRemoved(string $action, $callback, ?int $priority = null) : Mockery\Expectation

Comment on lines +562 to +568
self::userFunction(
'remove_action',
array(
'args' => array_filter(func_get_args()),
'times' => 1,
)
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better performance, it would be ideal to be more implicit about the aguments instead of reaching for the func_get_args, that way the interpreter won't have to do much assumption work with the data types.

Also, we are trying to move away from the classic array syntax towards.

Suggested change
self::userFunction(
'remove_action',
array(
'args' => array_filter(func_get_args()),
'times' => 1,
)
);
$args = [$action, $callback];
if ($priority) {
$args[] = $priority;
}
return self::userFunction('remove_action', [
'args' => $args,
'times' => 1,
]);

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

Successfully merging this pull request may close these issues.

Add expectHookRemoved and expectHookNotRemoved support
3 participants