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.
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:
1 |
which php |
If it says something like
1 |
/usr/bin/php |
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:
1 |
echo $PATH |
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:
1 |
PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php5.4.4/bin:$PATH" |
Then, in terminal type:
1 |
vi ~/.bash_profile |
Use the arrow keys to scroll just below the last line of the file
1 |
ESC (the escape key, puts you in 'edit' mode) |
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
1 |
echo $PATH |
You should now see php5.4.4 in your path. Perfect. Even better, try
1 |
php -v |
which should give something like:
1 2 3 4 5 |
PHP 5.4.4 (cli) (built: Jul 4 2012 17:28:56) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies with XCache v2.0.0, Copyright (c) 2005-2012, by mOo with Xdebug v2.2.0, Copyright (c) 2002-2012, by Derick Rethans |
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:
1 |
pear -V (Note the capital 'V') |
you should see
1 2 3 4 |
PEAR Version: 1.9.4 PHP Version: 5.4.4 Zend Engine Version: 2.4.0 Running on: Darwin...(more stuff...) |
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).
1 2 3 4 5 6 |
pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear channel-discover pear.symfony-project.com pear channel-discover pear.symfony.com pear install --alldeps phpunit/PHPUnit pear install phpunit/PHPUnit_Selenium |
Then run
1 |
phpunit --version |
The output should be
1 |
PHPUnit 3.6.12 by Sebastian Bergmann. |
Then run
1 |
which phpunit |
To get
1 |
/Applications/MAMP/bin/php/php5.4.4/bin/phpunit |
The final step is to open Netbeans and tell it where PHPUnit was installed. See the image below.
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
Hi Zank,
Thanks for the easy weasy steps to install PHPUnit. Worked like a charm!
Pawan
You’re welcome Pawan. I’m glad it worked.