Adding PHPUnit To LAMP Projects

Since we had a client ask about the effort involved to setup automated testing in their product, and because I mentioned it before in a LAMP Best Practices post, we thought it’d be a great idea to share how easy it is to get PHPUnit going in a new project.


Our unit testing framework of choice is PHPUnit.  So before we get started, we’ll need to get that installed.  At Solution Factor we all develop on Linux Mint, so installing it from the repo is simple (note: the following commands only apply to Debian based distributions).

To install PHPUnit, run:
[code lang=”bash”]sudo pear install phpunit/PHPUnit[/code]

Next, a CodeCoverage library may be needed; unfortunately it’s left out of some PHPUnit packages.  To install it, we just ran the following:

sudo pear channel-discover
sudo pear channel-discover
sudo pear channel-discover
sudo pear install phpunit/PHP_CodeCoverage

With that, our PHPUnit install is complete. Now we’re ready to start adding PHPUnit to our project.

Directory And File Setup

To get PHPUnit setup, we started by creating a tests directory.  This can be created anywhere in a project, so long as it’s outside the document root (for security purposes).   The tests directory will house everything test related, and within it we created 3 things:

  1. a phpunit.xml file – the config file for phpunit
  2. classes/baseTest.php – will house a simple test to confirm PHPUnit is working
  3. bootstrap.php – to allow PHPUnit to bootstrap into our project

Here’s what the basic file and directory setup looks like in my dev environment:

PHPUnit file structure

PHPUnit file structure

PHPUnit File Modifications

With the file and directory structure setup, it’s time to inform PHPUnit about our test suite, and provide it with tests to run.

First up is the phpunit.xml file.   This is PHPUnit’s config file and tells it where tests and any bootstrap files are. Here’s the content we added, note the Bootstrap and test suite settings:

<!--?xml version="1.0" encoding="UTF-8"?-->


Next we added a simple test case to classes/baseTest.php file.  The test isn’t meaningful, it only serves as a way to confirm PHPUnit is running properly.  Here’s the file content:


class BaseTest extends PHPUnit_Framework_TestCase
    public function testBase()
        $this->assertEquals(1, 1);

And that completes the setup. To confirm PHPUnit runs fine, cd to the tests directory and run phpunit.  We initially got a bunch of HTML output, and you may too, but at the very bottom PHPUnit did give the following confirmation:  OK (1 test, 2 assertions).

So if you see that, phpunit is working.

The Bootstrap File, And Getting Rid Of HTML Output

We didn’t mention the Bootstrap file much, but we did just mention some unneeded HTML output.  Both are related.

The Bootstrap file for PHPUnit serves a similar purpose as it would for other frameworks (i.e. most common MVC frameworks):  it helps PHPUnit initialize and load any config or libraries needed to run tests properly.  For example, you should aim to provide PHPUnit with the same config parameters that your app has, for things like database connections, path information, etc. An example of how we utilized a bootstrap.php file with teh Kohana framework can be found here: PHPUnit into Kohana.

Fortunately in our case little was needed in bootstrap.php. All we added was one line of code to load the config file for the app we’re testing:


With that, PHPUnit then had access to the same config parameters our app does. Without loading the app’s config in bootstrap.php, we’d have to redefine the same config values somehow for PHPUnit; the bootstrap.php file allows you to avoid having to do that in a redundant way.

For your app, expect that bootstrap.php will serve a similar purpose, but obviously will contain different code than what we added.


We believe every LAMP project should integrate automated testing. As shown here, it’s easy to get that started. Once setup, reaping the benefits of automated testing is only a matter of adding tests as the code base progresses. And, once matured, the test suite is a great way to gain confidence that new features, bug fixes, or other changes are stable as they move upstream to production.

This entry was posted in General. Bookmark the permalink.

4 Responses to Adding PHPUnit To LAMP Projects

  1. dave says:

    what is in bootstrap.php?

    • mzarate says:

      Hi Dave,

      I modified the post to include the one line of code that we had to add in bootstrap.php.

      But that content will be different for your project, as the bootstrap file is just a way for PHPUnit to pre-load any config or libraries needed to run tests properly.

      Hope this helps!

  2. dave says:

    Everything worked for me except the tests did not run. I had to rename base_test.php to baseTest.php for it to work.

    • mzarate says:

      Ah yes, that makes sense, as it follows PHPUnit’s test file naming convention. I probably took the screen shot that cited base_test.php before I ran into that problem. Screen shot updated to show baseTest.php instead of base_test.php.

      Thanks for pointing that out.

Leave a Reply to mzarate Cancel reply

Your email address will not be published. Required fields are marked *