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

Env: Better way to expose phpunit #22365

Closed
wants to merge 1 commit into from

Conversation

noahtallen
Copy link
Member

Description

This splits out some code from #20090. The goal of this PR is to expose phpunit without requiring the user to figure out where their phpunit config file is located inside the wp-env docker environment. I split it out because there is pending discussion about how to best expose this command.

An alternative to this is exposing a phpunit command directly, which would avoid having to make modifications to wp-env run.

Why is this needed?

You currently can run phpunit with wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist'. The problem with this is that you must specify /var/www/html/wp-content/plugins, which is the internal Docker path to your mapped source. Though it's not that confusing, I don't think we should expect anyone to learn this.

Effectively, we need to point phpunit to the right place in the docker container without the user needing to know where that is. The current PR adds CWD support to wp-env run which will set the CWD of the run command to the specified location in Docker. We translate the given external/local filesystem path into a path that works inside the docker service by finding the mapped source which the CWD belongs to. This works quite well so long as the specified CWD points to one of your mapped sources locally.

How has this been tested?

running wp-env commands locally

Types of changes

new feature/enhancement

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

- update readme
- update changelog
- remove phpunit command
@noahtallen noahtallen added [Type] Enhancement A suggestion for improvement. [Package] Env /packages/env labels May 14, 2020
@noahtallen noahtallen self-assigned this May 14, 2020
@noahtallen noahtallen changed the title Try: Better way to expose phpunit Env: Better way to expose phpunit May 14, 2020
@noahtallen noahtallen mentioned this pull request May 14, 2020
8 tasks
@github-actions
Copy link

Size Change: 0 B

Total Size: 832 kB

ℹ️ View Unchanged
Filename Size Change
build/a11y/index.js 1.02 kB 0 B
build/annotations/index.js 3.62 kB 0 B
build/api-fetch/index.js 3.39 kB 0 B
build/autop/index.js 2.83 kB 0 B
build/blob/index.js 620 B 0 B
build/block-directory/index.js 6.59 kB 0 B
build/block-directory/style-rtl.css 764 B 0 B
build/block-directory/style.css 764 B 0 B
build/block-editor/index.js 104 kB 0 B
build/block-editor/style-rtl.css 10.8 kB 0 B
build/block-editor/style.css 10.8 kB 0 B
build/block-library/editor-rtl.css 7.19 kB 0 B
build/block-library/editor.css 7.19 kB 0 B
build/block-library/index.js 118 kB 0 B
build/block-library/style-rtl.css 7.48 kB 0 B
build/block-library/style.css 7.48 kB 0 B
build/block-library/theme-rtl.css 683 B 0 B
build/block-library/theme.css 685 B 0 B
build/block-serialization-default-parser/index.js 1.88 kB 0 B
build/block-serialization-spec-parser/index.js 3.1 kB 0 B
build/blocks/index.js 48.1 kB 0 B
build/components/index.js 182 kB 0 B
build/components/style-rtl.css 17.1 kB 0 B
build/components/style.css 17 kB 0 B
build/compose/index.js 6.67 kB 0 B
build/core-data/index.js 11.4 kB 0 B
build/data-controls/index.js 1.29 kB 0 B
build/data/index.js 8.42 kB 0 B
build/date/index.js 5.47 kB 0 B
build/deprecated/index.js 772 B 0 B
build/dom-ready/index.js 569 B 0 B
build/dom/index.js 3.1 kB 0 B
build/edit-navigation/index.js 5.6 kB 0 B
build/edit-navigation/style-rtl.css 618 B 0 B
build/edit-navigation/style.css 617 B 0 B
build/edit-post/index.js 28.1 kB 0 B
build/edit-post/style-rtl.css 12.2 kB 0 B
build/edit-post/style.css 12.2 kB 0 B
build/edit-site/index.js 12 kB 0 B
build/edit-site/style-rtl.css 5.22 kB 0 B
build/edit-site/style.css 5.22 kB 0 B
build/edit-widgets/index.js 7.86 kB 0 B
build/edit-widgets/style-rtl.css 4.69 kB 0 B
build/edit-widgets/style.css 4.69 kB 0 B
build/editor/editor-styles-rtl.css 425 B 0 B
build/editor/editor-styles.css 428 B 0 B
build/editor/index.js 44.3 kB 0 B
build/editor/style-rtl.css 5.07 kB 0 B
build/editor/style.css 5.08 kB 0 B
build/element/index.js 4.65 kB 0 B
build/escape-html/index.js 733 B 0 B
build/format-library/index.js 7.63 kB 0 B
build/format-library/style-rtl.css 502 B 0 B
build/format-library/style.css 502 B 0 B
build/hooks/index.js 2.13 kB 0 B
build/html-entities/index.js 622 B 0 B
build/i18n/index.js 3.56 kB 0 B
build/is-shallow-equal/index.js 711 B 0 B
build/keyboard-shortcuts/index.js 2.51 kB 0 B
build/keycodes/index.js 1.94 kB 0 B
build/list-reusable-blocks/index.js 3.13 kB 0 B
build/list-reusable-blocks/style-rtl.css 226 B 0 B
build/list-reusable-blocks/style.css 226 B 0 B
build/media-utils/index.js 5.29 kB 0 B
build/notices/index.js 1.79 kB 0 B
build/nux/index.js 3.4 kB 0 B
build/nux/style-rtl.css 616 B 0 B
build/nux/style.css 613 B 0 B
build/plugins/index.js 2.56 kB 0 B
build/primitives/index.js 1.5 kB 0 B
build/priority-queue/index.js 789 B 0 B
build/redux-routine/index.js 2.85 kB 0 B
build/rich-text/index.js 14.8 kB 0 B
build/server-side-render/index.js 2.68 kB 0 B
build/shortcode/index.js 1.7 kB 0 B
build/token-list/index.js 1.28 kB 0 B
build/url/index.js 4.02 kB 0 B
build/viewport/index.js 1.84 kB 0 B
build/warning/index.js 1.14 kB 0 B
build/wordcount/index.js 1.17 kB 0 B

compressed-size-action

@timnolte
Copy link
Contributor

I would love to see this updated and merged in. I'm glad to have finally found the top on using wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/gutenberg/phpunit.xml.dist as an example of using the PHPUnit Docker container for my own plugin.

@pjv
Copy link

pjv commented Oct 18, 2020

@noahtallen Sorry to @ you from left field, but this is a request for some basic docs that seem relevant to this PR. I’ve read through the wp-env docs and searched all the issues here but haven’t been able to figure out how to run phpunit tests for an arbitrary plugin via wp-env.

I’m on macOS with wp-env installed globally. It works fine; I am able to wp-env start from within a plugin dir and then open up http://localhost:8888 in my browser.

If I am starting on the command line in the root of an unscaffolded arbitrary plugin, what steps (commands) would take me from there to being able to scaffold (with wp cli) and run the generated sample test suite under wp-env?

Apologies in advance if this is documented somewhere; I couldn’t find it.

@noahtallen
Copy link
Member Author

hi @pjv! I'm sorry that there aren't any docs for this. Essentially, wp-env provides a service which you can run phpunit commands inside of, but you have to set up most of the scaffolding yourself.

I added phpunit support to a plugin which I maintain, and I would recommend looking over this pull request as an example of adding it to an arbitrary plugin: Automattic/wp-calypso#39328. You can also review the phpunit-related scaffolding in this Gutenberg repo. Here are some loose steps:

  1. you'll need to have composer install wp-phpunit so that the wp tests scaffolding is available. This will also need to be mapped to the wp-env service.
  2. You'll need a basic phpunit.xml and bootstrap.php file. both of which need to be in your plugin so they are available inside of the wp-env service.
  3. You would need to run the phpunit command in a custom way: wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/$plugin-path/phpunit.xml.dist'

That makes sure that phpunit is pointing to your phpunit.xml file inside of the docker service.

I hope this is helpful. I'm hoping we can improve these steps for everyone soon

@pjv
Copy link

pjv commented Oct 20, 2020

@noahtallen Thanks very much.

I required / installed wp-phpunit with composer in my plugin’s root.

I do not know how to do this:

This will also need to be mapped to the wp-env service

I generated phpunit scaffolding for the plugin by running this:

wp-env run tests-cli wp scaffold plugin-tests bt-grain-inventory-manager

I replaced the bootstrap.php that wp cli generated with this:

require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php';
$_tests_dir = getenv('WP_PHPUNIT__DIR');
require_once $_tests_dir . '/includes/functions.php';

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

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

When I try to run the test suite with wp-env run phpunit "phpunit -c /var/www/html/wp-content/plugins/bt-grain-inventory-manager/phpunit.xml.dist" I get:

Error: The required WP_TESTS_DOMAIN constant is not defined.

I ran the same command as above with --debug and I can see this config object:

"config": {
                "WP_DEBUG": false,
                "SCRIPT_DEBUG": false,
                "WP_PHP_BINARY": "php",
                "WP_TESTS_EMAIL": "admin@example.org",
                "WP_TESTS_TITLE": "Test Blog",
                "WP_TESTS_DOMAIN": "http://localhost:8889/",
                "WP_SITEURL": "http://localhost:8889/",
                "WP_HOME": "http://localhost:8889/"
            }

...in both the development and the tests environments, but it seems like the environment variables are not being mapped to php constants anywhere.

In the debug output’ docker-compose.yml, I also see this in the phpunit service:

"environment": {
                "LOCAL_DIR": "html",
                "WP_PHPUNIT__TESTS_CONFIG": "/var/www/html/phpunit-wp-config.php"
            }

but when I bash’ed into the phpunit container and ls-ed the html directory, there is no file there named phpunit-wp-config.php. Should there be? And should that file be setting those required constants?

Sidenote: In a lot of the many issues and PR conversations I’ve been pouring through trying to sleuth out how to make this work, it looks like you all are locally installing wp-env in the plugin directory. I am running wp-env from a global (yarn) install. Should that matter?

wp-env --version
2.1.0

@noahtallen
Copy link
Member Author

there is no file there named phpunit-wp-config.php. Should there be? And should that file be setting those required constants?

Yes to both! Normally, this would be created with wp-env start.

I am running wp-env from a global (yarn) install. Should that matter?

It shouldn't make a difference -- the only thing would be having an up to date version, and it does appear you do have that.

I do not know how to do this:

This will also need to be mapped to the wp-env service

You need to make sure that the composer directory is mapped to the wp-env service. For example, this in wp-env.json:

	"mappings": {
		"wp-content/plugins/full-site-editing-plugin/vendor": "../../vendor"
	}

Alternatively, you can have your composer dependencies directly in wp-env, instead of mapping them from your local system. The problem with that approach is that composer install inside of wp-env can take a long time due to poor docker filesystem performance.

@pjv
Copy link

pjv commented Oct 20, 2020

@noahtallen Thank you very much again. I have the test suite running now.

Yes to both! Normally, this would be created with wp-env start.

That was the clue I needed. It turned out I had a dependency in my plugin on having a valid timezone string set and when the phpunit service tried to activate my plugin (in bootstrap.php), it failed because the vanilla wp db install has no timezone string set (defaults to just UTC), and when my plugin threw a fatal error, that apparently was breaking the phpunit service’s initialization so it never created / bind-mounted the phpunit-wp-config.php file.

That was a silent failure. It might be good to figure out a way to echo to the command line if something derails the error-free initialization of the phpunit service.

I develop custom and premium plugins and my development environment is docker. I stumbled onto wp-env looking for a quick way to spin up a docker-based tests environment that didn’t have all the overhead of a full wp core development setup. wp-env is a great project. Thanks again for your help.

@noahtallen
Copy link
Member Author

noahtallen commented Oct 20, 2020

That was a silent failure. It might be good to figure out a way to echo to the command line if something derails the error-free initialization of the phpunit service.

Yeah, that's a good idea!

wp-env is a great project. Thanks again for your help.

I'm really glad to hear it!

Base automatically changed from master to trunk March 1, 2021 15:43
@noahtallen noahtallen closed this Oct 26, 2021
@sirreal sirreal deleted the add/better-way-to-expose-phpunit branch September 25, 2024 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Env /packages/env [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants