-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Conversation
- update readme - update changelog - remove phpunit command
Size Change: 0 B Total Size: 832 kB ℹ️ View Unchanged
|
I would love to see this updated and merged in. I'm glad to have finally found the top on using |
@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 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. |
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:
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 |
@noahtallen Thanks very much. I required / installed wp-phpunit with composer in my plugin’s root. I do not know how to do this:
I generated phpunit scaffolding for the plugin by running this:
I replaced the bootstrap.php that wp cli generated with this:
When I try to run the test suite with
I ran the same command as above with
...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:
but when I bash’ed into the phpunit container and ls-ed the html directory, there is no file there named 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?
|
Yes to both! Normally, this would be created with
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.
You need to make sure that the composer directory is mapped to the wp-env service. For example, this in wp-env.json:
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 |
@noahtallen Thank you very much again. I have the test suite running now.
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 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. |
Yeah, that's a good idea!
I'm really glad to hear it! |
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: