Skip to content

MAMP 2.0 Pear and PHPUnit Installation

November 10, 2011

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!

About these ads
16 Comments
  1. Joshua Carter permalink

    Thank you, sir! After scouring the web for a solution, yous is the only one that worked. Cheers.

  2. Thanks, thanks a lot for your post ;)
    Worked at first try!

  3. Luís Manuel Vicente permalink

    Thanks a lot for very well explained post.

  4. You are amazing. Finally, after almost a day of wasted time you have got me there!

  5. Michael Grech permalink

    Gosh, thank you! Why on earth was this so freaking hard. Plus why can’t MAMP just bundle this :)

  6. rujero permalink

    Installing phpunit requires privileges:

    sudo ./pear install –alldeps pear.phpunit.de/PHPUnit

  7. terraba permalink

    this is the error msg that I am getting after entering
    sudo ./pear upgrade pear

    Could not get contents of package “/Applications/MAMP/bin/php/php5.3.6/bin/pear”.
    Invalid tgz file.
    upgrade failed

    any help??

    • I got the same error, but I tried sudo ./pear upgrade and it worked! Get rid of the last pear and it will work.

  8. chefjake permalink

    why is it looking in /Applications/MAMP/bin/php5.3/bin/php ?

    jake$ sudo ./pear config-set auto_discover 1
    ./pear: line 28: /Applications/MAMP/bin/php5.3/bin/php: No such file or directory

  9. Torben Sandberg permalink

    Thx for the post I find that after I installed Xcode command line tools and Mamp server controls and libraries it all worked correct.

  10. carrie permalink

    this helped me with channel discovery, thanks!

  11. Gregor McKelvie (@gregormckelvie) permalink

    This is a great post – thanks. One thing; it works for MAMP 2.0, but tried it with MAMP 2.1.1 and it doesn’t work. I’ve kept with MAMP 2.0, but if you are using MAMP 2.1.1 then I think you need to set up a symbolic link between php5.4.4 and php5.3.6 (where PHPUnit installs for some reason).

    Thanks again!

  12. John K permalink

    Appreciate this info, but as rujero pointed out, the install command had to be preceded by ‘sudo’ otherwise you get errors when it tries to create some directories under /Applications/MAMP/bin/php/php5.3.6/lib/php/docs

  13. Jakes permalink

    At last! Ive tried 10 other websites that use the slightly different ways. This one actually works!

    Thank you!!!

Trackbacks & Pingbacks

  1. PHPUnit 3.6 Tests and Komodo IDE Unit Testing « BigDiver
  2. Setting up a IDE | Learning how to be a web developer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: