Setting up PHPUnit with MAMP

After a few hours of Googling and headdesking, I finally got PHPUnit up and running in my local environment. In the end, I had to make reference to a bunch of different resources. It was a convoluted enough process that I don’t think I can replicate step-by-step instructions, but I can at least list some of the stumbling blocks I hit along the way.

My setup: OSX (10.6.8), running PHP 5.3.2 from the MAMP package. Generally, the commands I give here will have to be run as root; use either sudo each time or enter the root prompt with sudo su.

The basic instructions for installing PHPUnit tell you to use PEAR:
[code language=”bash”]
pear channel-discover pear.phpunit.de
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
pear install –alldeps phpunit/PHPUnit
[/code]
But when you’re running MAMP, ‘pear’ may point to the wrong version of PHP – the version that comes bundled with OS X, or another of the versions that comes with MAMP. Make sure that you’re referencing the proper one (source). In my case, that means:
[code language=”bash”]
/Applications/MAMP/bin/php5.3/bin/pear channel-discover pear.phpunit.de
[/code]
and so forth.

As noted here and elsewhere, PHPUnit requires at least PEAR 1.8.1, but MAMP (or at least the version I have installed) ships with an earlier release. (You’ll know that this is the case, because when you try installing PHPUnit with the commands above, you’ll get a bunch of messages about required versions.) When I tried upgrading PEAR with the internal
[code language=”bash”]
/Applications/MAMP/bin/php5/bin/pear channel-update pear.php.net
/Applications/MAMP/bin/php5/bin/pear upgrade pear
[/code]
I got a “Nothing to upgrade” message. I ended up doing a sort of manual upgrade, using step 6 of these instructions:
[code language=”bash”]
cd /Applications/MAMP/bin/php5.3
curl -O http://pear.php.net/go-pear.phar
php go-pear.phar
[/code]
and then following the on-screen instructions for configuring PEAR.

I found that, in my situation, the automatically generated PEAR configuration files pointed to the wrong version of PHP. I used the instructions in this comment to lead me in the right direction. More specifically, when I looked at the PEAR config settings
[code language=”bash”]
pear config-show
[/code]
I saw immediately that some of the paths were pointing to the wrong version of PHP (at /Applications/MAMP/bin/php/ rather than /Applications/MAMP/bin/php5.3/). I reconfigured the necessary paths like so:
[code language=”bash”]
pear config-set doc_dir /Applications/MAMP/bin/php5.3/lib/php/doc
[/code]
and so on.

This got me to the point where I could fire up PHPUnit on my version of PHP, without getting a ‘command not found’ error. However, I didn’t get very far in my testing, because I kept getting fatal errors. In my PHP error logs, I could see that lines in PHPUnit, like the following, were failing to find their targets:
[code language=”php”]
require_once ‘PHPUnit/Autoload.php’;
[/code]
This suggested that the include_path in my php.ini didn’t have the correct directory. So I opened it up (on my installation, it’s at /Applications/MAMP/conf/php5.3/php.ini). It turns out that PEAR had attempted to rewrite the include_path directive but it had pointed to the wrong path for my purposes. I appended the following path (which is where PEAR keeps the PHPUnit package files): /Applications/MAMP/bin/php5.3/lib/php/PEAR

Et voilĂ  – it finally worked:
[code language=”bash”]
phpunit –version
# PHPUnit 3.5.15 by Sebastian Bergmann.
[/code]

4 thoughts on “Setting up PHPUnit with MAMP

  1. Dave Doolin

    Boone, thanks for going through this exercise in detail. I have phpunit and the WP tests running on Ubuntu, and I have it running for my own php files using a locally compiled, MacOXS php. Getting the WP tests running locally has tripped over all the problems you list. I’ll finish up with your MAMP instructions given here. Thanks!

    Reply
  2. Pingback: How to Install PHPUnit with MAMP on a Mac | I'm ZankI'm Zank

  3. gary

    Thanks Boone for posting this. I was struggling to get PHPUnit set up on my system OSX 10.6.8.
    Now when I type $ phpunit –version
    I get a proper response.
    cheers
    Gary

    Reply
  4. pavan

    This was really helpful. I was stuck until I saw this. Thanks so much!

    FYI this worked for me on OS X 10.8.2

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *