Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unit Test Installation and Running #61

Open
fklein-lu opened this issue Nov 16, 2015 · 2 comments
Open

Fix Unit Test Installation and Running #61

fklein-lu opened this issue Nov 16, 2015 · 2 comments
Labels
Milestone

Comments

@fklein-lu
Copy link
Contributor

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: wp-cli/wp-cli#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.
@fklein-lu fklein-lu added the Bug label Nov 16, 2015
@fklein-lu fklein-lu added this to the 0.9.1 milestone Nov 16, 2015
@mattheu
Copy link
Member

mattheu commented Nov 16, 2015

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 😄

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
Copy link
Contributor Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants