5

Using answers on this site and from other resources, I have started writing my next plugin using PHPUnit tests and the WordPress test environment. Distilling down what I have in my plugin's bootstrap.php:

define( 'WP_TESTS_DIR', 'pathToMyWordPressTestsFromSVN');
define( 'TEST_PLUGIN_FILE', 'pathToMyPlugin/myPlugin.php' );
require_once WP_TESTS_DIR . 'includes/functions.php';
require WP_TESTS_DIR . 'includes/bootstrap.php';

function _manually_load_plugin() {
    require TEST_PLUGIN_FILE;
    if(is_plugin_active(TEST_PLUGIN_FILE)){
        echo "PLUGIN IS LOADED AND WORKING!!!!";
    }
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Normally you'd find "require WP_TESTS_DIR . 'includes/bootstrap.php';" down here...

In essentially every example, the WordPress test environment bootstrap.php is loaded last.

This seems strange because if I load it earlier, I get access to functions like is_plugin_active which I imagine will be useful when testing plugins that require others...and bailing out if the requirements aren't loaded for some reason.

Is there a reason the test environment is bootstrapped at the end...other than habit/convention?

  • Could you possibly link to the resources and reference materials you've been using to set this up? – Nathan Arthur Jun 9 '14 at 14:53
  • 1
    It took me several days knitting together things from the Codex, tutorials, random GitHub projects and so on. My notes are pretty good, but not electronic (yet). Let me see if I can take the current project and clean it up to share on Github. – Matthew Bakaitis Jun 9 '14 at 15:22
  • That, sir, would be amazing. – Nathan Arthur Jun 9 '14 at 15:26
  • I posted my first working version as a github project: wordtest on GitHub. There are a ton of things on my list of improvements...but for now, it's better than nothing and it's easy to add new plugins/themes as needed. – Matthew Bakaitis Jun 12 '14 at 14:13
  • Wow, this is really nice. I wish there was a better way to make people aware of this project's existence. – Nathan Arthur Jun 12 '14 at 15:01
8

The reason the WordPress test bootstrap is loaded at the end is precisely because it loads WordPress:

// Load WordPress
require_once ABSPATH . '/wp-settings.php';

If you don't hook your function to load your plugin to 'muplugins_loaded' before including the bootstrap, your plugin won't be loaded with WordPress. In most cases that will mean that your plugin won't get set up properly (e.g., 'init' will have already been fired before your plugin's functions get hooked up).

As far as checking for dependencies, you could probably do that by hooking into the 'plugins_loaded' action:

function _check_for_dependencies() {
    if ( ! is_plugin_active( 'some-plugin/some-plugin.php' ) ) {
        exit( 'Some Plugin must be active to run the tests.' . PHP_EOL );
    }
}
tests_add_filter( 'plugins_loaded', '_check_for_dependencies' );

Or, you can just let WordPress' bootstrap load completely and check for your dependencies after it.

  • Thanks for the answer. Try-and-fail experimentation after asking showed that moving it up was causing errors in tests I was writing. Now it makes sense why that was happening. – Matthew Bakaitis May 22 '14 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.