Laravel is an awesome PHP framework and comes with support for unit testing with PHPUnit out of the box!
So… I thought I’d post up this quick guide as a reference to others and in my opinion is a rather neat way of keeping Composer and PHPUnit contained inside the project as opposed to installing it for ‘global’ use, this means when you’ve had enough you can simple delete your Laravel project folder and all traces of Composer and PHPUnit are gone 🙂
In this post we will be using PHPUnit 3.8.x and for this to work correctly, you’ll need to ensure that you are running PHP greater than version 5.4.7, if you cannot upgrade and are not currently running PHP 5.4..7 or greater you can substitute the version number of PHPUnit below with 3.7!
The manual ‘Step-by-step’ guide
Step 1: Downloading Composer
There are several ways in which you can download Composer, firstly you can use cURL if you have it installed on your system like so:
curl -sS https://getcomposer.org/installer | php
Or you can use PHP from the command line to download it:-
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
Or grab the latest snapshot of the composer.phar file directly using this URL:
http://getcomposer.org/composer.phar
Step 2: Download the Laravel framework
Download and extract the Laravel framework to a folder named ‘laravel’ -You can obviously change the name of the folder if you wish but for this tutorial I’ll keep it as ‘laravel’.
https://github.com/laravel/laravel/archive/master.zip
Step 3: Add ‘PHPUnit’ to the composer.json
So now what you need to do is open up the composer.json file of which will be found in the root directory of the extracted Laravel download, we are going to update the file by replacing this line:-
"laravel/framework": "4.0.*"
with the following two lines (notice the addition of the common on the before mentioned line!)
"laravel/framework": "4.0.*",
"phpunit/phpunit": "3.8.*"
Save the file and we can now move on…
Step 4: Let Composer do it’s magic!
Now that we’ve ‘required’ that Composer download and installs PHPUnit on top of the other composer.json dependencies, we’ll now initiate the ‘installation’ process, this will then kick off Composer to download all of the dependencies including PHPUnit!
First we need to move the downloaded composer.phar file into the root of your laravel folder.. which can be done like so:-
mv composer.phar laravel/composer.phar
Now ‘change into’ your Laravel directory…
cd laravel/
Now we’ll get Composer to do the ‘install’…
php composer.phar install
Step 5: You’re done!
Now that Laravel is installed and Composer has downloaded all of the required packages it’s now time for you to get coding! – You can now fire up the local development server like so…
php artisan serve
….you’ll now be able to develop and test in your browser using http://localhost:8000.
All in one go!
I’ve created a Gist which basically enables a one-liner copy and paste solution to do all of this, you can read/bookmark the gist here for future reference 🙂