Installing PHPUnit On OSX Mavericks via Composer

Installing PHPUnit globally in OSX can be a bit tedious with many paths that can be gone down whether it be PHAR or PEAR. In this quick guide I will be showing how to easily install PHPUnit using Composer and show how to set up a syslink to run it from anywhere in your system. I will be using a fresh install of Mac OSX Mavericks.

Install Composer

If you have yet to install composer globally this can be done. First open the Terminal App (In Application > Utilities) and enter the following two commands:

	curl -sS | php
	sudo mv composer.phar /usr/local/bin/composer

You will be prompted to enter your administator password when running the second command.

Once done you can now run composer from the Terminal, lets just test that it works, enter the following in the Terminal App:

	composer --version

If everything installed corrently you should get back something like the following: Composer version [Version String] [Installation Date]

Download PHPUnit

Next we need to download PHPUnit, but we need a location on our system to store it, I like to store all mine in ~/libraries, so lets create this folder and traverse into it, from the Terminal App run the following:

	mkdir ~/libraries
    cd ~/libraries

Now it’s time to create our composer.json file, once again using the Terminal App that we have open run:

	touch composer.json
  nano composer.json

The command line text editor nano will then open, paste the following code in it:

    "require-dev": {
        "phpunit/phpunit": "3.7.*"

To save the file and exit nano press ctrl+x, type ‘y’ and press the enter key.

Finally run the composer install command from the Terminal app to download PHPUnit and it’s dependencies like so:

	composer install

Note: If you don’t have git installed on your system you might get a xcode notiifcation that will allow you to automatically download and install it it, this is fine allow it to run.

Install PHPUnit Globally

We now have PHPUnit installed within the libraries folder, lets just confirm this by run the following:

	~/libraries/vendor/bin/phpunit --version

The corrent response should be PHPUnit 3.7.28 by Sebastian Bergmann.

Now lets create a system link to our computers bin folder from PHPUnit Binary, in terminal run:

	sudo ln -s ~/libraries/vendor/bin/phpunit /usr/bin/phpunit

Again you will be asked to enter your password.

PHPUnit will now be installed on your system. To confirm this close down the Terminal app, open it again and run the phpunit --version command. The version number should appear as it did before.

The advantage of this approach is that it gives us a central library folder, want to install behat or php_codesniffer? Simply add it to our composer.json file, run composer install and create a system link to the binary.

Happy testing!