Local Community

PHPUnit testing setup

Any out of box help, or at least tutorials, to get unit testing with phpunit and the WordPress test suite would be awesome.

Hey @tw2113 I was able to get phpunit working through the ssh terminal but you have to repeat these steps for each site. Here’s how I did it:

  1. Install wget
    apt-get update
    apt-get install wget

  2. Install svn (this might not be necessary, i needed it to download the wp test library)
    apt-get install subversion

  3. Install phpunit
    wget https://phar.phpunit.de/phpunit.phar chmod +x phpunit.phar mv phpunit.phar /usr/local/bin/phpunit

I used ‘localhost’ for the database host when creating the test database. Let me know if you have any issues getting it working, I was able to use it successfully after install wget, svn, and then phpunit.

1 Like

I’ve created a script to install PHPUnit. You only have to run it once for each site.

It also installs WordPress and the WP_UnitTestCase in the /tmp directory. It adds the $WP_CORE_DIR and $WP_TESTS_DIR variables to the .bashrc file.

These variables are available in VVV and are used by many plugins that had their unit tests scaffolded by WP-CLI

Read the installation instructions in the script itself

5 Likes

Hey, thanks i seriously appreciate that! maybe we could make this into an addon where you have the option to setup unit testing for each site

While the script is great to get PHPUnit and the WP test suite all set up (nice one @keesiemeijer !), is there any docs on connecting via SSH without using the right-click ‘Open Site SSH’ option with Local itself? Reason being is I’d like to set it up so you can run the tests within the PHPStorm IDE rather than through the command line.

The script the open site SSH seems to just be eval’ing a docker instance, which doesn’t give a port/address to use as far as I can see.

1 Like

FWIW, I’ve just copy/pasted the line that Local puts into iterm, into my terminal in PHPStorm, and it lets me go from there.

That definitely works, just not through the Run configuration (to run via a hot key, see the phpunit pass/fail results in the PHPStorm UI, etc). Doing TDD by having to flip to the terminal a couple/few times a minute would get pretty annoying.

Came close to setting it up by trying the ‘Docker’ option and selecting the docker-manager within the flywheel app folder, but not able to set up the rest of the PHPUnit configuration yet. Will keep trying and will ping PHPStorm support to see if they have any thoughts!

I would like to point out that this worked for me (installing phpunit this way) vs. using apt-get.

This script worked great for me! Thanks for writing and sharing it.

As of right now, 2.2.3, I’m no longer able to install what I need via apt-get. Downloading the latest phpunit, version 7, using wget https://phar.phpunit.de/phpunit.phar && chmod +x phpunit.phar && mv phpunit.phar && /usr/local/bin/phpunit installs phpunit properly but you have to ensure, now with version 7, that you are using PHP 7.1+ in your site settings.

But, apt-get installation method, e.g. apt-get install phpunit will not work and apt-get will hang with the errors:

The following packages have unmet dependencies:
 php-codecoverage : Depends: libjs-twitter-bootstrap but it is not going to be installed
E: Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution).
Preparing to unpack .../libjs-twitter-bootstrap_2.0.2+dfsg-5_all.deb ...
Unpacking libjs-twitter-bootstrap (2.0.2+dfsg-5) ...
dpkg: error processing archive /var/cache/apt/archives/libjs-twitter-bootstrap_2.0.2+dfsg-5_all.deb (--unpack):
 unable to make backup link of `./etc/apache2/conf-available/libjs-twitter-bootstrap.conf' before installing new version: Operation not permitted
Errors were encountered while processing:
 /var/cache/apt/archives/libjs-twitter-bootstrap_2.0.2+dfsg-5_all.deb
E: Sub-process /usr/bin/dpkg returned an error code (1)

Hey all,

I wrote a guide on how to run PHPUnit and PhpStorm with Local 2.3.0+. See Local + PHPUnit + PhpStorm

2 Likes

I get
Could not install packages
No network connection detected
when I run bash setup-phpunit.sh

Hi @hazephase

Did you SSH into the local server before running the script? And were you connected to the internet?

Yes, I did SSH into the local server

The script is pinging the Google’s public DNS server at ip 8.8.8.8 to check for internet conectivity. It could be the DNS server was down.

Check if you can reach it by SSH in to the local server and using this command:

ping -c 3 --linger=5 8.8.8.8 >> /dev/null 2>&1 && echo "Connected" || echo "Not connected"

If it prints “Not connected” it could mean something is blocking access to this ip address (or the DNS server/internet is down).

You could try removing these lines from the script to remove the connectivity check. You’ll get some warnings if it can’t download the necessary packages.

That worked for me but when I try and run composer phpcs-tests I get a different error.

[Symfony\Component\Console\Exception\CommandNotFoundException]
Command “phpcs-tests” is not defined.
how do I fix this?

Did you run composer install before running this command?

Yes, I am running these commands in my theme folder.

yes, I did that . I did it in my theme folder

This doesn’t seem to work in Local 2.4.0.

Found this thread: Local + PHPUnit + PhpStorm

But that doesn’t appear to be working either.