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:
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.