Brain-WP/BrainMonkey

WP: adding mocks for some miscellaneous functions

Closed this issue · 1 comments

jrfnl commented

Just wondering if there is interest in the addition of more mocks for some typically, often mocked, WP translation/output escaping functions, as well as some miscellaneous WP functions.

The functions I'm thinking of are:

  • __() etc - to just return the original string. Duh, just discovered the Functions\stubTranslationFunctions() method, so never mind about this.
  • esc_attr(), esc_html() etc - also to just return the original string Duh, just discovered the Functions\stubEscapeFunctions() method, so never mind about this.
  • wp_json_encode() - return the output of json_encode() without further sanity check.
  • wp_slash() - same functionality as WP
  • wp_unslash() - same functionality as WP
  • user_trailingslashit() just trailingslashit()
  • wp_parse_args() and by extension wp_parse_str() - same functionality as WP

Please let me know if any (or all) of these peek your interest.

Thanks @jrfnl

I would say that:

  • wp_json_encode()
  • user_trailingslashit()

could be added to the generic helper functions that are Mocked by default.

I normally put there "logicless" functions that can be written in one/two lines.

I think wp_slash() and wp_unslash(), would also be a nice addition, but considering in WP they support recursive array processing (and wp_unslash() even recusrive object processing), as well as single string, they would not be completely logicless nor one line. Not sure about those.

Adding wp_parse_args() and wp_parse_str() is something I considered as well, the reasons I did not add the stub for them is that wp_parse_str() triggers a filter, which means that by adding a stub that do not trigger any filter I could break existing tests.

On top of that, If I would ship a stub for these functions, it would use Functions\when. I personally find myself using Functions\expect with wp_parse_args to both overcome the fact that function is not defined and also add expectation for it to be called. But when and expect can't be used in same test for same function, meaning that any test currently using expect would fail.

That means that if will be convinced of adding a stub for those functions that should be opt-in, in the same fashion stub for escaping and translations functions are shipped now (for the same reason). Having those stubs opt-in is maybe enough also to overcome the issue of wp_parse_str() not firing a filter.

What do you think?