Closed acobster closed 1 year ago
@acobster Would you be willing to submit a PR making that change? This will make it easier for us to see what's going on and evaluate the potential impact there ...
Absolutely, I'll get one in soon!
On May 26, 2017 10:04 PM, "Eric Mann" notifications@github.com wrote:
@acobster https://github.com/acobster Would you be willing to submit a PR making that change? This will make it easier for us to see what's going on and evaluate the potential impact there ...
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/10up/wp_mock/issues/89#issuecomment-304428044, or mute the thread https://github.com/notifications/unsubscribe-auth/AATksKDUIVSUcjnMYUjaj4i5uGueB-tVks5r967RgaJpZM4Madz- .
TL;DR: when I call
add_action()
with a closure callback,expectHookAdded()
does not recognize the closure as having been called. Adding a call to$responder->react()
insideexpectHookAdded()
fixes this.Since the real
add_action()
is designed to take any callable, I would expect to be able to callexpectHookAdded
with any valid callable, including a closure.For example, I am trying to unit test a trait that encapsulates adding an admin filter on the Posts listing screen. Specifically, I'm testing the trait's
addAdminFilter()
method. I would expect to be able to do something like this:This throws:
Digging into the
WP_Mock
class, I see that theexpectHookAdded()
code creates an internal mock object and tells it to beintercepted
; then it "performs" the added hook.This works for the basic use-case of declaring a global function, e.g.
foo()
, and then callingadd_action('pre_get_posts', 'foo')
. But in my use-case, I want to encapsulate my callback as a closure, not expose it as a global function. Adding a call to$responder->react()
at the end ofexpectHookAdded()
fixes this issue.I'm not sure if such a change would have other implications. Is it an appropriate solution? Should
expectAction()
also get the same treatment?