PHPUnit Plugin Integration Tests: Getting wordpress-tests-lib PHP into IDE (VS Code)

So I've managed to follow the instructions to get the Plugin Integration Tests set up using wp-cli and the install-wp-tests.sh script as described in the handbook article:

https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/

After much hassle and debugging of a million things, I even have an example test running, yay! It's possible! Don't give up!

My issue: The custom PHPUnit extensions for WP aren't picked up by my IDE.

  • The /wordpress-tests-lib/ directory is deep in the bowels of my Mac, where install-wp-tests.sh installed it along with the ghost copy of WP
  • The code runs when I use the phpunit command in the terminal because bootstrap.php includes it
  • But because the testing library is nowhere near my WP directory or the plugin, VS Code (and probably any other IDE) doesn't pick it up, and my test ends up looking like this:

I can't use autocomplete or navigate to source and all those good things.

I can't be the only one with this problem! How do you deal with this?

I'm going to post my janky solution below, but am eager to hear how others make this work for themselves :)

Topic unit-tests ide Wordpress

Category Web


The best solution I've found is to make a symlink from the plugin directory to the actual location of the /wordpress-tests-lib/.

At least in the case of VS Code, the symlink seems to help it "find" the files and incorporate them into autocomplete etc. I'm still having bugs but this is the best I could do.

Find the temporary directory

There's probably a smarter way, but I found my test dir by just echoing it out to the terminal when I run phpunit.

Edit the bootstrap.php file from your plugin like so:

add echo "$_tests_dir" after the variable is set up

Then run phpunit in the terminal and copy your path URL. Mine (mac) is

/var/folders/sn/y4jh01gs67xbbdh2v7zy_86w0000gn/T/wordpress-tests-lib

Add the symlink to the plugin dir

Open your plugin in the terminal and add the link with the following command:

ln -s /PATH/ _wordpress-tests-lib

You can use a different link string if you want, it shouldn't matter.

So here's the actual command I ran:

ln -s /var/folders/sn/y4jh01gs67xbbdh2v7zy_86w0000gn/T/wordpress-tests-lib/ _wordpress-tests-lib

After this I restarted my IDE to ensure it rescanned the directories, and the class was no longer marked as undefined.

Add the symlink to .gitignore

For bonus points, you can block the symlink from showing up in Git, which you probably want because it will be pointing to a directory that is system-specific.

Just add _wordpress-tests-lib to a new line in your .gitignore file and commit the change.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.