Rectangle 27 0

php How to configure PHPUnit in MAMP with PHAR?


/Applications/MAMP/bin/php/php5.2.17/bin/phpunit.phar --version

/usr/local/bin/ is just recommended as a convention, because it's always in the $PATH on Unix systems, you can use any other location that's in your path, or leave it anywhere else and specify the absolute path when using it.

Note the .phar suffix, because you did not rename the file while moving, as you'd have with sudo mv phpunit.phar /usr/local/bin/phpunit

With what you have, you should be able to run:

Note
Rectangle 27 0

php How to configure PHPUnit in MAMP with PHAR?


# Use MAMP's latest version of PHP
MAMP_LATEST_PHP=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${MAMP_LATEST_PHP}/bin:$PATH
# Use colors when running phpunit
alias phpunit='phpunit --colors'
/Applications/MAMP/bin/php/php7.0.0/bin/php
/usr/local/bin
PHPUnit 5.3.2 by Sebastian Bergmann and contributors.
cd /usr/local/bin
chmod +x phpunit-5.3.2.phar
class SomeTest extends PHPUnit_Framework_TestCase
phpunit -version
phpunit SomeTest
  • Download the most recent PHPUnit PHAR from https://phar.phpunit.de
  • Make a symbolic link with ln -s phpunit-5.3.2.phar ./phpunit (adjust according to actual name)
  • Method names in your test class that are to be run as tests must start with test: public function testSomething()
  • Your test class file name must end with Test.php and match the contained class: SomeTest.php

Assuming that it all works (Yay!) try this alias in your ~/.bash_profile

Building a symbolic link in step 5 permits you to use phpunit instead of having to type phpunit-5.3.2.phar instead. It also allows you to update PHPUnit without having to change what you type, assuming of course that you create a new symbolic link when you update.

By this time, it should be as easy as:

Edit or create ~/.bash_profile with the lines below

Here are the steps I used to successfully get PHPUnit working in MAMP. These instructions are pieced together from various places. I hope that having it all in one place helps someone else. Happy testing!

If everything goes well, PHPUnit will run your test and give you the results.

Mostly, this is downloading the PHP archive (PHAR) from the PHPUnit website. There are ways to do this from the command line that I couldn't get to work. So, I used a web browser.

One line you forgot to mention after adding the lines to the bash file. "Use source ~/.bash_profile after making your changes to make sure they take effect."

Place these lines after any other lines exporting $PATH - this assures that your MAMP PHP is found first in the path. Note that these lines try to find the highest numbered version of PHP in your MAMP installation. Feel free to adjust this to a specific one that you have, if desired.

This isn't an exhaustive section. There are far better tutorials on writing tests. Instead, this is merely some notes from my experience on rules that tripped me up, though I'm sure everyone else knows them:

You can tell you did it right when you get a MAMP path from which php in your terminal. You should get something like this:

You should get something like this:

Note