Installation
The easiest way to install PHPUnit — or any other PHP library or framework for that matter — is to use Composer. Let’s install Composer first.
Installing Composer
If you’re using Linux/Unix/OSX, the installation process is simple. All you have to do is to run the following command in a terminal:
$ curl -sS https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer
To test that Composer has been installed successfully, run the following command:
$ composer -V
If everything went well, you should see output similar to this:
Composer version 1.0-dev (feefd5...54d0d5) 2015-12-03 16:17:58
If you’re getting that output, you can move on to the next section.
If you’re not getting that output or using another OS, please go through the official installation instructions.
Configuring the Project and Installing PHPUnit
Now that we have Composer installed, let’s configure our project and install PHPUnit.
Create a directory for the sample project we’re going to work throughout the book. I called it phpunit-starter in my system, but you can come up with any other name if you don’t like that one.
Now, create a file called composer.json in that directory with the following contents:
1 {
2 "require": {
3 "phpunit/phpunit": "^4.8"
4 },
5 "autoload": {
6 "psr-4": {
7 "": "src/"
8 }
9 },
10 "config": {
11 "bin-dir": "bin"
12 }
13 }
This file tells Composer how to handle our application. Let’s go through it.
On lines 2-4, we have a require section. This section tells Composer which packages our application needs — project dependencies. The only dependency we have here is PHPUnit that we specify on line 3. ^4.8 is a version constraint that tells Composer to install PHPUnit of version 4.8 and up to but not including version 5.
On lines 5-9, we configure autoloading of PHP classes. We need that to avoid including all the files we need with include()/include_once()/require()/require_once() statements. Thanks to autoloading, classes will be found and loaded automatically.
On lines 10-12, we tell Composer to copy binary files that a dependency our project depends on can ship with. PHPUnit does ship with one. We need that to get the phpunit binary installed into the bin/ directory instead of the vendor/bin. We do that just for simplicity of executing PHPUnit.
Now that we have composer.json, let’s tell Composer to do its job:
$ composer install
If everything went as expected, you should see output similar to this:
Loading composer repositories with package information
Installing dependencies (including require-dev)
- Installing sebastian/version (1.0.6)
... omitted ...
- Installing phpunit/phpunit (4.8.19)
... omitted ...
Generating autoload files
Now, several things should appear in the project directory. The vendor/ directory contains the dependency we specified in our composer.json file along with dependencies of that dependency and dependencies of those dependencies — you get the idea. It also includes autoloading-related files generated by Composer.
composer.lock is the file listing exact versions of installed dependencies. It makes sense to commit it to your project’s VCS to ensure that all team members and servers get exactly the same versions of dependencies to avoid hard-to-debug bugs caused by installing a dependency of a different version on some server. We don’t really care about that file in this book.
The last but far from the least thing that appeared in the project directory is the bin/ folder. It appeared here thanks to the configuration we did in our composer.json. It’s the most important thing that Composer added for us because it contains the phpunit executable.
Now, let’s verify that PHPUnit got installed correctly by running the following:
$ bin/phpunit --version
If everything went well, you should get output similar to this:
PHPUnit 4.8.19 by Sebastian Bergmann and contributors.
That line means that we got PHPUnit installed and are ready to start learning it. It’s time to write our first test.