First, I would like to say that this is a documentation of how I was able to make phpunit work on my macOS system, and that your set up may vary. I may not be doing everything the best or most efficient way, but it is currently working for me. If you have any ideas on how I can be more efficient or do things better let me know in the comments.

Now lets start where I started, not knowing anything about PHP unit testing except for the fact that I should be doing it and hearing a lot of smart people talking about it. What is unit testing? how do I set it up? how do I get it to run? and how do I write tests for it? Those were all questions I had when I started this journey, and they are questions I hope to answer here.

What is unit testing?

Unit testing is a process that test each single process method in a class, or each smallest part of a larger program. These tests are usually exhaustive, taking in every possible option for input and testing that the output of that method is exactly what you would expect.

Take for example you wanted to figure out on average how many times your dog would catch a frisbee. We could write the following class/method:

Then we would write tests to determine if what we are expecting back is what we actually get back by passing numbers and strings into the method and checking the return values. For instance if you passed 5 and 7 into dog::catching_avg you should expect to see 0.571 as a return, but what if you passed in 5 and 0? or 16 and ‘a string’? Unit testing will help you catch and solve those problems before they become bugs for your users.

How do I set unit testing up?

Lets get to it with a little bit about my local development setup, which consists of MAMP Pro for my server, PHP Storm for my IDE, iTerm2 for my terminal application. Today we will mostly be concerned with using a terminal app so if you are not comfortable with the command line I would suggest that you maybe skip this until your more confident. If you’re looking for some resources Treehouse’s article ‘Introduction to the Mac OS X Command Line‘ is a good little intro, or you could take the ‘Learn the Command Line‘ course at Code Academy, or if you subscribe to you can take their extensive ‘Unix for Mac OS X Users‘.

For this walk through I am going to assume that you have MAMP installed, up, and running on your Mac.

Setting PATH

The first thing we need to do is to make sure that we have our PATH properly set. To do that we need to check to see if the system default PHP being used or the MAMP version of PHP.

In terminal type which php

If it displays a system location such as /usr/bin/php we will need to change it.

we will want to open our .bash_profile located in our user directory.

If you are comfortable with editing files in terminal you can open and edit your bash profile in vim or emacs, if not, you can follow along with me here.

In terminal type defaults write AppleShowAllFiles YES; killall Finder /System/Library/CoreServices/ this will show all of your hidden files.

Now navigate to your user folder and you will find .bash_profile as shown in the image below, if you do not see a file named .bash_profile you can create one with TextEdit and save it to this directory.

Finder Showing Invisible Files.

Once you have .bash_profile open in TextEdit or your favorite plain text editor add the following line of code:

Note: Be sure to replace php5.6.25 with whatever version of php you are using when you boot up your servers with MAMP.

Now you can save and close this file.

Once this is done, you will want to hide the hidden files in finder, you can do that with the following command in terminal:

defaults write AppleShowAllFiles NO; killall Finder /System/Library/CoreServices/

Now in terminal type which php again and you should get something like: /Applications/MAMP/bin/php/php5.6.25/bin/php

Next we will move on to installing composer & phpunit. There are two options for this, you can install it system wide, or you can install it within the folder of the plugin that you are developing in. I first installed it system wide, and then decided to install it local to my plugin. So we will walk through how to install it local to your plugin below.

Installing Composer

So back in terminal we will want to move into the folder of the plugin we are developing, for example purposes I will be using split-test as my plugin folder.

Once within the folder in terminal the first thing we will want to do is install composer. You can do that with the following command: curl -sS | php

Installing composer will look like this:

Installing Composer

Configuring Composer

Now we will want to let composer know what to install with a file named composer.json. This file will go in the split-test directory.

The composer.json file, for a basic install, should look like this:

Note: This will install the latest version (at the time of writing this) of phpunit. When version 6 is released the line "phpunit/phpunit": "5.*" will need to be updated to stay up to date.

The "autoload" section of this file is declaring name spaces and corresponding folders that it will auto load files from so you will now need to create a folder named classes and a folder named tests within your split-test folder.

Next we will need to add a file named phpunit.xml that will tell phpunit how to boot up and configure when you run the phpunit command in terminal.

The phpunit.xml file, for a basic install, should look like this:

Installing & running phpunit

Now that we have our configuration files in place we can complete the installation. In order to do that you are going to want to run the following command in terminal:

./composer.phar update -dev

That command will look like this:

Composer Update

Now that php is installed you should be able to run the command phpunit --version and get something like this:

run phpunit version

You can also run the phpunit command and get something like this:

run phpunit without tests

When we have unit tests written against our code running phpunit will look like:

run phpunit with tests

Setting up and Running Tests

I will go into detail on how to set up namespaces, import the correct files, and testing many cases with one method in the next blog post.

Leave a Reply

Your email address will not be published.