chriszarate / docker-compose-wordpress

An example Docker Compose setup for WordPress plugin or theme development.
162 stars 54 forks source link

Tests Directory Within Plugin Dir #3

Closed MikeiLL closed 7 years ago

MikeiLL commented 7 years ago

Am not clear on what the .env variable, PHPUNIT_TEST_DIR is supposed to be doing. Is it supposed to be a full path that runs on the hosted docker container?

Want to keep the tests within the directory of the plugin, at least for now.

If I update this repo's phpunit.xml.dist file:

bootstrap="./my-plugin/tests/bootstrap.php"`\
<directory prefix="test-" suffix=".php">./my-plugin/tests/</directory>

Then it will run my bootstrap and tests:

<?php
$_tests_dir = getenv( 'WP_TESTS_DIR' );
if ( ! $_tests_dir ) {
    $_tests_dir = '/tmp/wordpress-tests-lib';
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

/**
 * Manually load the plugin being tested.
 */
function _manually_load_plugin() {
    require dirname( dirname( __FILE__ ) ) . '/my-plugin.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

When I have the .env set to:

DOCKER_DEV_DOMAIN=myplugin.dev
DOCKER_LOCAL_IP=108.205.63.109
PHPUNIT_TEST_DIR=my_plugin

It still seems to be looking at the phpunit.xml file from this repo, in the dir above my plugin.

chriszarate commented 7 years ago

The PHPUNIT_TEST_DIR environment variable should be the absolute system path (inside the container) to your phpunit.xml. The system path to wp-content is /var/www/html/wp-content/.

The example file in this repo shows /var/www/html/wp-content/plugins/my-plugin as an example. You only need to edit it if you changed the corresponding path in the volumes section.

The only purpose of this environment variable is when using the convenience command to run PHPUnit tests:

docker-compose exec wordpress tests

This command will chdir to the path contained in PHPUNIT_TEST_DIR. If you run tests via some other means, you don't need to set PHPUNIT_TEST_DIR.

MikeiLL commented 7 years ago

Yes. I am running docker-compose exec wordpress tests. actually added an alias to my ~/.bashrc: alias tests=docker-compose exec wordpress tests so I can just run tests in the command line, which seems to be working with just my-plugin set in the .env file, and not the entire path.

MikeiLL commented 7 years ago

This is totally outside of the scope of this wonderful little repository, but am I able to add variables to .env and access them in the test files?

chriszarate commented 7 years ago

Yes, you can pull in these env vars using getenv in PHP.

MikeiLL commented 7 years ago

I expected to be able to and tried. This getenv('DOCKER_DEV_DOMAIN');, when called in a test within the tests directory within the plugin, returns nothing. I must be missing a step. At the moment I just added some attributes to a gitignored static class file and put an Autoloader in the tests structure, but would love to see this work, if I haven't already over-taxed you.

Also I realize I was thinking that PHPUNIT_TEST_DIR was supposed to be inside of the .env file.

chriszarate commented 7 years ago

Ah, you're right. Two things here:

  1. Variables in .env are specific to Docker Compose and are not passed into the container.

  2. Variables defined in the environment section of docker-compose.yml are passed into the container. However the way I'm running the tests command doesn't expose them to your test code. I'll look into fixing that.

MikeiLL commented 7 years ago

That would be a cool feature, and if the plugin repo is next to docker-compose.yml, then those could be variables like credentials that you wouldn't want in a public repo.

MikeiLL commented 7 years ago

Arg. Not quite there yet with having plugin in directory within the repo clone, I just discovered, that while my tests are running fine, however the literal my-plugin.php file at the top level that is being installed and activated as my-actual-plugin, and trying to reference the files and directories parallel to it as plugin files.

Needed to update the docker-compose.yml:

services:
  wordpress:`
    ...stuff...
    volumes:
      - "./my-actual-plugin/var/www/html/wp-content/plugins/my-actual-plugin"

Wondering if it would be best to just use this the way you originally intended it. But it seems like a lot of files to add at the top level of my plugin, which already begs cleaning up. : )

chriszarate commented 7 years ago

Closing this issue, feel free to reopen if you are still having trouble.