# PHPUnit testing setup

**URL:** <https://community.localwp.com/t/phpunit-testing-setup/1104>\
**Category:** Feature requests\
**Created:** [March 26, 2017, 3:52am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104 "2017-03-26T03:52:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![tw2113](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/tw2113/32/4620_2.png) [@tw2113](https://community.localwp.com/u/tw2113)\
**Post date:** [March 26, 2017, 3:52am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/1 "2017-03-26T03:52:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tpkemme](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/tpkemme/32/4674_2.png) [@tpkemme](https://community.localwp.com/u/tpkemme)\
**Post date:** [April 28, 2017, 8:25am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/2 "2017-04-28T08:25:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![keesiemeijer](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/keesiemeijer/32/4692_2.png) [@keesiemeijer](https://community.localwp.com/u/keesiemeijer)\
**Post date:** [May 8, 2017, 1:50pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/3 "2017-05-08T13:50:25Z")

</div>

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](https://github.com/Varying-Vagrant-Vagrants/VVV) and are used by many plugins that had their unit tests [scaffolded by WP-CLI](https://developer.wordpress.org/cli/commands/scaffold/plugin/)

Read the installation instructions in the script itself

> <https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba>

---

<div class="post-metadata">

**Author:** ![tpkemme](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/tpkemme/32/4674_2.png) [@tpkemme](https://community.localwp.com/u/tpkemme)\
**Post date:** [May 31, 2017, 6:43am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/4 "2017-05-31T06:43:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![brianhogg](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/brianhogg/32/24521_2.png) [@brianhogg](https://community.localwp.com/u/brianhogg)\
**Post date:** [June 11, 2017, 10:54pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/5 "2017-06-11T22:54:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![tw2113](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/tw2113/32/4620_2.png) [@tw2113](https://community.localwp.com/u/tw2113)\
**Post date:** [June 11, 2017, 11:07pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/6 "2017-06-11T23:07:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brianhogg](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/brianhogg/32/24521_2.png) [@brianhogg](https://community.localwp.com/u/brianhogg)\
**Post date:** [June 12, 2017, 3:38pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/7 "2017-06-12T15:38:02Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![aubreypwd](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/aubreypwd/32/22472_2.png) [@aubreypwd](https://community.localwp.com/u/aubreypwd)\
**Post date:** [November 20, 2017, 4:15pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/8 "2017-11-20T16:15:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![raquelmsmith](https://avatars.discourse-cdn.com/v4/letter/r/3ab097/32.png) [@raquelmsmith](https://community.localwp.com/u/raquelmsmith)\
**Post date:** [January 3, 2018, 4:03am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/9 "2018-01-03T04:03:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aubreypwd](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/aubreypwd/32/22472_2.png) [@aubreypwd](https://community.localwp.com/u/aubreypwd)\
**Post date:** [February 12, 2018, 10:25pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/10 "2018-02-12T22:25:59Z")

</div>

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:

```auto
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).

```

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![clay](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/clay/32/8248_2.png) [@clay](https://community.localwp.com/u/clay)\
**Post date:** [April 20, 2018, 7:42pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/11 "2018-04-20T19:42:39Z")

</div>

Hey all,

I wrote a guide on how to run PHPUnit and PhpStorm with Local 2.3.0+. See [Local + PHPUnit + PhpStorm](https://local.getflywheel.com/community/t/local-phpunit-phpstorm/6203)

---

<div class="post-metadata">

**Author:** ![hazephase](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/hazephase/32/4376_2.png) [@hazephase](https://community.localwp.com/u/hazephase)\
**Post date:** [September 12, 2018, 9:37pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/12 "2018-09-12T21:37:36Z")

</div>

> [@keesiemeijer](#):
>
> 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](https://github.com/Varying-Vagrant-Vagrants/VVV) and are used by many plugins that had their unit tests [scaffolded by WP-CLI](https://developer.wordpress.org/cli/commands/scaffold/plugin/)
> 
> Read the installation instructions in the script itself

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

---

<div class="post-metadata">

**Author:** ![keesiemeijer](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/keesiemeijer/32/4692_2.png) [@keesiemeijer](https://community.localwp.com/u/keesiemeijer)\
**Post date:** [September 14, 2018, 12:03pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/13 "2018-09-14T12:03:58Z")

</div>

Hi @hazephase

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

---

<div class="post-metadata">

**Author:** ![hazephase](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/hazephase/32/4376_2.png) [@hazephase](https://community.localwp.com/u/hazephase)\
**Post date:** [September 15, 2018, 1:49am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/14 "2018-09-15T01:49:39Z")

</div>

Yes, I did SSH into the local server

---

<div class="post-metadata">

**Author:** ![keesiemeijer](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/keesiemeijer/32/4692_2.png) [@keesiemeijer](https://community.localwp.com/u/keesiemeijer)\
**Post date:** [September 15, 2018, 1:57pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/15 "2018-09-15T13:57:28Z")

</div>

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](https://gist.github.com/keesiemeijer/a888f3d9609478b310c2d952644891ba#file-setup-phpunit-sh-L49-L54) from the script to remove the connectivity check. You’ll get some warnings if it can’t download the necessary packages.

---

<div class="post-metadata">

**Author:** ![hazephase](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/hazephase/32/4376_2.png) [@hazephase](https://community.localwp.com/u/hazephase)\
**Post date:** [September 16, 2018, 2:50pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/16 "2018-09-16T14:50:45Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![keesiemeijer](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/keesiemeijer/32/4692_2.png) [@keesiemeijer](https://community.localwp.com/u/keesiemeijer)\
**Post date:** [September 17, 2018, 12:56pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/17 "2018-09-17T12:56:57Z")

</div>

Did you run `composer install` before running this command?

---

<div class="post-metadata">

**Author:** ![hazephase](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/hazephase/32/4376_2.png) [@hazephase](https://community.localwp.com/u/hazephase)\
**Post date:** [September 17, 2018, 4:40pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/18 "2018-09-17T16:40:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![hazephase](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/hazephase/32/4376_2.png) [@hazephase](https://community.localwp.com/u/hazephase)\
**Post date:** [September 17, 2018, 4:47pm UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/19 "2018-09-17T16:47:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JJJ](https://sea1.discourse-cdn.com/flex019/user_avatar/community.localwp.com/jjj/32/5110_2.png) [@JJJ](https://community.localwp.com/u/JJJ)\
**Post date:** [September 21, 2018, 2:34am UTC](https://community.localwp.com/t/phpunit-testing-setup/1104/20 "2018-09-21T02:34:34Z")

</div>

This doesn’t seem to work in Local 2.4.0.

Found this thread: [Local + PHPUnit + PhpStorm](https://local.getflywheel.com/community/t/local-phpunit-phpstorm/6203)

But that doesn’t appear to be working either.

[Next page](https://community.localwp.com/t/phpunit-testing-setup/1104.md?page=2)
