开发者

Testing 'pluggable' function calls clashes for WordPress and unit testing

WordPress has a group of functions call pluggable functions; basically, they are designed to be overrided for new functionality, but there is a catch - if another plugin define that function first, some other plugin of a lower priority cannot...(or else there will be a fatal error). Hence it is common to have 'include guards' like this

if (!function_exists('wp_new_user_notification'))
{ 
   function wp_new_user_notification($userid, $blah='')
   {
   }
}

This avoids fatal errors, but it leads to plugi开发者_开发技巧ns mysteriously failing...

Usually one try to avoid clashing plugins. One problem, though, is that you have no idea if a plugin clashes, and sometimes it's hard to avoid that for feature-packed plugins.

The question is (sorry for taking so long to set the premise) how do you detect clashes? It is always an invisible failure because the function exists, no error is thrown, but the outcome is "Hey, this plugin's broken!"

I could echo something if a function has been defined, but how do I turn it into a unit test? WordPress would include all plugins' header files whenever a page is load. How do I determine that the pluggable function which Wordpress has loaded is the one I want, programatically?


The only thing that comes to mind is to mark the function declaration failure in your app and test for the marker:

if (!function_exists('wp_new_user_notification'))
{ 
   function wp_new_user_notification($userid, $blah='')
   {
   }
}
else
{
  define('MY_PLUGIN_NEW_USER_NOTIFICATION_DECLARATION_FAILED', true);
}

and then test with defined:

if(defined('MY_PLUGIN_NEW_USER_NOTIFICATION_DECLARATION_FAILED'))
{
 // fail
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜