MAMP 2.0 Pear and PHPUnit Installation
I have been struggling to get PHPUnit working under MAMP 2.0 on OSX Lion, and Snow Leopard. After a lot of searching and googling, and combing through countless forums I figured it out and my tests are finally running.
Here are the steps I took for MAMP using PHP 5.3.6, if your PHP is different adjust accordingly, but basically you have to be in the bin directory of MAMP’s PHP install. If you are you should see the binary for php, for pear, in this directory.
cd /Applications/MAMP/bin/php/php5.3.6/bin
sudo ./pear upgrade pear
In my brand new, fresh, install of MAMP i got the following error:
Notice: unserialize(): Error at offset 276 of 1133 bytes in Config.php on line 1050
ERROR: The default config file is not a valid config file or is corrupted.
If you get this error as well just delete the pear.conf file, and rerun the pear upgrade
rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf
sudo ./pear upgrade pear
You should now see something like (truncated the output for space):
downloading PEAR-1.9.4.tgz ...
Starting to download PEAR-1.9.4.tgz (296,332 bytes)
.............................................................done: 296,332 bytes
downloading Archive_Tar-1.3.8.tgz ...
Starting to download Archive_Tar-1.3.8.tgz (17,995 bytes)
...done: 17,995 bytes
downloading Console_Getopt-1.3.1.tgz ...
Starting to download Console_Getopt-1.3.1.tgz (4,471 bytes)
...done: 4,471 bytes
upgrade ok: channel://pear.php.net/Archive_Tar-1.3.8
upgrade ok: channel://pear.php.net/Console_Getopt-1.3.1
upgrade ok: channel://pear.php.net/PEAR-1.9.4
...
Now that you’ve upgraded Pear you can install PHPUnit and its dependencies. Following instructions from the PHPUnit Github repository, i.e. directly from the developer, you can install PHPUnit by running:
./pear config-set auto_discover 1
./pear install --alldeps pear.phpunit.de/PHPUnit
DO NOT RUN ./pear install phppunit as that will install some old and useless version that will not work! There are a lot of tutorials out on the web that are outdated and will not work for the latest version of MAMP and PHPUnit.
You should get something similar to the following (truncated for space):
Attempting to discover channel "pear.phpunit.de"...
downloading channel.xml ...
Starting to download channel.xml (804 bytes)
...
install ok: channel://pear.phpunit.de/PHP_CodeCoverage-1.1.1
install ok: channel://pear.phpunit.de/PHPUnit_MockObject-1.1.0
install ok: channel://pear.phpunit.de/PHPUnit-3.6.3
Notice that PHPUnit_MockObject and PHP_CodeCoverage are also installed.
You are now ready to run phpunit and see if it works…
./phpunit
Should print usage information without any errors, on the screen your your PHP Error log file. If there are any include errors in you PHP Error file, you may have multiple PHPUnit versions installed and you need to remove them all by using the pear uninstall command.
My php_error.log had a bunch of entries that looked like this:
PHP Fatal error: require_once(): Failed opening required 'PHPUnit/Framework/MockObject/Autoload.php' (include_path='.:/Applications/MAMP/bin/php/php5.3.6/lib/php') in /Applications/MAMP/bin/php/php5.3.6/lib/php/PHPUnit/Autoload.php on line 48
...
PHP Warning: require_once(PHPUnit/TestCase.php): failed to open stream: No such file or directory in /Applications/MAMP/bin/php/php5.3.6/lib/php/PHPUnit.php on line 49
To resolve this I uninstalled all PHPUnit, and PHPUnit_MockObject.
./pear uninstall phpunit
./pear uninstall pear.phpunit.de/PHPUnit
./pear uninstall pear.phpunit.de/PHPUnit_MockObject
Then reinstalled PHPUnit as explained earlier in this article.
One final comment on Using PHPUnit in your tests. A lot of the tutorials I found on the web, including the PHPUnit manual tell you to include PHPUnit in your code as:
require_once 'PHPUnit/Framework.php'
For the version at the time of this writing which was PHPUnit 3.6.3 you must do the following instead
require_once 'PHPUnit/Autoload.php';
Happy testing!
[...] having problems in getting PHPUnit to work in the first place check my other article to get PHPUnit to work with MAMP on Mac OSX Snow Leopard and Lion. LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); [...]
PHPUnit 3.6 Tests and Komodo IDE Unit Testing « BigDiver
November 10, 2011 at 1:50 pm
Thank you, sir! After scouring the web for a solution, yous is the only one that worked. Cheers.
Joshua Carter
November 18, 2011 at 10:53 pm
Thanks, thanks a lot for your post
Worked at first try!
Marcelo Rodrigo
December 22, 2011 at 1:21 pm
Thanks a lot for very well explained post.
Luís Manuel Vicente
December 28, 2011 at 12:51 pm