-
Notifications
You must be signed in to change notification settings - Fork 70
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
Pass in Closure for Hooks #235
Comments
hey @badasswp thanks for bringing this up At the moment it should be possible to write something like WP_Mock::expectFilter('wp_title', function() {
return 'My Title';
}, 10, 2); You can also perform the following assertion: WP_Mock::expectFilterAdded('wp_title', WP_Mock\Functions::type(\Closure::class), 10, 2); I understand that you might be interested perhaps in determining whether the returned value is expected according to the logic applied by the callback. In these circumstances you could perhaps invoke a protected method from the closure, like so: add_filter('wp_title', function() { $this->myProtectedMethod(); }); And then create a proper test for See the documentation for more examples with hooks: https://wp-mock.gitbook.io/documentation/usage/mocking-wp-action-and-filter-hooks Please let me know if this is helpful. |
Thank you for your response @unfulvio-godaddy My main point was around using Closures to dynamically return values multiple times, just like the The previous examples you have described above was a bit close, but doesn't quite capture what I'm trying to achieve. This is what I'm trying to achieve:
|
I see, this unfortunately would require some refactoring or breaking change, because right now the |
Feature request
At the moment we can currently pass a Closure as the return value when mocking functions using the
\WP_Mock::userFunction
like so:This really comes in handy when we are in a loop situation and we need to do this several times.
Proposed solution
Can this same approach be employed for hooks? It would be very handy to do this in one swoop, just like its userFunction counterpart.
The text was updated successfully, but these errors were encountered: