How to Install PHPUnit with MAMP on a Mac

By August 22, 2012Startup Technology
phpUnitMamp

I wanted to use PHPUnit for unit testing within Netbeans. This can get a little tricky, so I’ll be as explicit as I can. I really hope this helps. I went round-about for a few hours before arriving at this solution. I applied this to my desktop machine (Mac Pro, running OSX 10.6.8) in just a few minutes, after spending far too long on outdated web tutorials, getting it to work on my laptop.

mamp

Download MAMP

First, go get MAMP. The latest and greatest. You can find the link here:

http://www.mamp.info/en/downloads/index.html

Once you’ve installed the latest version (2.2.1 at the time of this writing), you’ll have access to PHP 5.4.4 and a recent version of pear. We’re going to need them both.

First, open up terminal and look at your bash_proflie file.

 

 

Type:

If it says something like

Then your system version of PHP is the default. To install PHPUnit to work with MAMP, we need to make the default version of PHP the same one that MAMP is using. Let’s do that now.

Type:

You’re looking for any mention of PHP5.4.4 within the MAMP folder. If you didn’t put it there, chances are, you won’t see any mention of it. Let’s fix that.

Still in terminal, we’re going to edit your .bash_profile file to put MAMP’s version of PHP in your path.

First, copy the following line of text:

Then, in terminal type:

Use the arrow keys to scroll just below the last line of the file

Then the following key sequence: CTRL-v, ESC and SHIFT-Z-Z

You should now be comfortably back at the command prompt. Open a NEW terminal window!! Very important. Then type

You should now see php5.4.4 in your path. Perfect. Even better, try

which should give something like:

It’s also important to have a recent version of pear (pear does the installation of PHPUnit). Luckily, the latest version of MAMP takes care of that. I think installation requires a pear version > 1.8.1

Check your pear version similarly by:

you should see

Now, your system is accessing the MAMP-installed versions of PHP (and pear). It’s time to install phpunit

Now you’re ready to install PHPUnit. Type the following into terminal. Your current directory shouldn’t matter. Note that you may have to precede each of the following with ‘sudo’ (omitting the quotes, of course).

Then run

The output should be

Then run

To get

The final step is to open Netbeans and tell it where PHPUnit was installed. See the image below.

Netbeans Screen Shot

That’s it!

Thanks to Teleogistic for many of the setup instructions. That page is here:

http://teleogistic.net/2011/08/setting-up-phpunit-with-mamp/

 

If you’re using Yii, you may have to run the following lines:

1
require_once('PHPUnit/Runner/Version.php'); require_once('PHPUnit/Autoload.php');

See: http://stackoverflow.com/questions/13209812/yii-unit-testing-not-working

Join the discussion 2 Comments

Leave a Reply