humanmade / S3-Uploads

The WordPress Plugin to Store Uploads on Amazon S3
1.95k stars 391 forks source link

Fix Unit Test Installation and Running #61

Open fklein-lu opened 9 years ago

fklein-lu commented 9 years ago

Currently it is very difficult to install and run the plugin's unit tests on either Salty WordPress or VVV.

With Salty WordPress, the bin/install-wp-tests.sh script needs to be run first. This is not marked in any documentation. After the script has finished running, you get the following error:

PHP Fatal error: Class 'WP_REST_Server' not found in /srv/www/wordpress-develop.dev/tests/phpunit/includes/spy-rest-server.php on line 3

This is due to WP CLI issue: https://github.com/wp-cli/wp-cli/issues/2129, and the solution is to update the install script based on this: https://raw.githubusercontent.com/wp-cli/wp-cli/master/templates/install-wp-tests.sh

After having done this, we encounter the same issue as on VVV, that does not need any set up. This is the error you get:

PHP Notice:  Undefined variable: test_root in /srv/www/s3-uploads/htdocs/wp-content/plugins/s3-uploads/tests/bootstrap.php on line 21
PHP Warning:  require(/includes/functions.php): failed to open stream: No such file or directory in /srv/www/s3-uploads/htdocs/wp-content/plugins/s3-uploads/tests/bootstrap.php on line 21
PHP Fatal error:  require(): Failed opening required '/includes/functions.php' (include_path='/usr/local/src/composer/vendor/phpunit/php-file-iterator:/usr/local/src/composer/vendor/phpunit/phpunit:/usr/local/src/composer/vendor/symfony/yaml:.:/usr/share/php:/usr/share/pear') in /srv/www/s3-uploads/htdocs/wp-content/plugins/s3-uploads/tests/bootstrap.php on line 21

I fixed this error by replacing the tests/bootstrap.php file with the following code:

$_tests_dir = getenv('WP_TESTS_DIR');
if ( !$_tests_dir ) $_tests_dir = '/tmp/wordpress-tests-lib';
require_once $_tests_dir . '/includes/functions.php';

function _manually_load_plugin() {
        require dirname( __FILE__ ) . '/../s3-uploads.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

if ( getenv( 'S3_UPLOADS_BUCKET' ) ) {
        define( 'S3_UPLOADS_BUCKET', getenv( 'S3_UPLOADS_BUCKET' ) );
}

if ( getenv( 'S3_UPLOADS_KEY' ) ) {
        define( 'S3_UPLOADS_KEY', getenv( 'S3_UPLOADS_KEY' ) );
}

if ( getenv( 'S3_UPLOADS_SECRET' ) ) {
        define( 'S3_UPLOADS_SECRET', getenv( 'S3_UPLOADS_SECRET' ) );
}

require $_tests_dir . '/includes/bootstrap.php';

This makes the unit tests run successfully.

What I think we need to do is:

  1. Find out exactly what the cause of theses errors are (is it really WP-CLI?).
  2. Find out whether we can get by without having to use the Shell script to set up unit testing.
  3. Fix any issues so that the plugin can be tested on a variety of setups (Salty WordPress, VVV, maybe even MAMP).
  4. Provide documentation for setting things up correctly.
mattheu commented 9 years ago

Great write up. We definitely need to add a section to the readme on how to get this running.

I don't think its a WP-CLI issue as such... but rather the latest version of wordpress-tests-lib requires WP trunk (not latest stable). I guess we used the scaffold plugin command from WP-CLI when creating the plugin - hence the WP-CLI connection here.

Once 4.4 is released, I think this should just work :smile:

Also - If we need to define any environment variables - this should be documented in the readme. I had to add WP_TEST_DIR and WP_CONFIG_DIR to get this running.

fklein-lu commented 9 years ago

Okay, so since WordPress 4.4 is slated for early December, let's hold off on this until it gets released.

We'll then revisit and see what errors persist, and what steps need to be taken.