Book Image

Instant Hands-on Testing with PHPUnit How-to

By : Michael Lively
Book Image

Instant Hands-on Testing with PHPUnit How-to

By: Michael Lively

Overview of this book

No developer wants to accept the inherent difficulty of writing software as an excuse for not finding the bugs in our code before anyone else does. PHPUnit is a framework that was created to allow developers to solve that very problem. It provides a feature-rich environment with most of the tools necessary to provide adequate tests for any project. "Instant Hands-on Testing with PHPUnit How-to" provides a thorough overview of the functionality provided by the PHPUnit framework. It shows how the plethora of features in the framework can be used to write tests for real world projects to ensure they function and will continue to function in the ways that you expect. This book will show how you can set up the scaffolding necessary to run unit tests in your project with PHPUnit. It will walk you through the process of how to write a basic test and how to maintain your project's test suite. You will learn how to use some of the more advanced features of PHPUnit and then see how you can use mock objects to isolate the code you are testing. We will then discover how to create tests that verify your interaction with databases and even see how you can use PHPUnit to understand which code you are actually testing. At the end of the book you will have all of the basic understanding necessary to begin adding tests to your project. This book provides a great foundation for becoming a expert at writing unit tests.
Table of Contents (7 chapters)

Installing PHPUnit (Simple)

The first step to understanding how to test with PHPUnit is to understand how to make it available in our environment. In this recipe, we will install PHPUnit using the PHP Extension and Application Repository (PEAR) package manager. While PEAR has been available to PHP developers for some time, new packaging frameworks such as Composer have become increasingly more common. We will also cover these additional packaging methods.

How to do it...

Execute the following commands in a shell:

sudo pear config-set auto_discover 1
sudo pear install

How it works...

Installing via PEAR will give you access to PHPUnit across all projects in your environment. PHPUnit uses its own PEAR server for distribution. These are called channels. By default, PEAR is only aware of its own channel. The auto_discover setting tells PEAR that any time a package from a new channel is requested, it should automatically register that channel. Otherwise, each new channel would have to be added explicitly using pear channel-discover. Not only does PHPUnit itself have its own channel, but some of its dependencies are on other custom channels. This is why we use pear config-set to enable auto_discover. Because we are using auto_discover, we only need to run pear install to complete the installation.

There's more...

The standard PEAR installation of PHPUnit should provide everything you need to start writing tests. PEAR is a convenient way to install PHPUnit at the environment level. You may find that installing PHPUnit using a dependency manager called Composer works better for your needs.

Installing PHPUnit using Composer

It is becoming more and more common in the PHP world to bundle everything that you need to run and develop an application at the application level. Composer is a dependency manager for PHP that works well in handling this concept. To understand how Composer works and how you can integrate it into your project, read the Getting Started guide on their website at Once you have Composer integrated into your site, you can add the following package to your composer.json file:

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

This will set up PHPUnit as a development requirement for your package. This works well as you typically don't need your end users to run your tests. Also, you should always double check the PHPUnit page on packagist to see what the latest version is. Please note that the remaining examples in this book make use of the PEAR installed version of PHPUnit.

Installing PHPUnit on older versions of PHP

Currently, the latest version of PHPUnit is 3.7. This version requires PHP 5.3.3 or higher. If you find yourself using an older version of PHPUnit, due to a bug in PEAR you may have an issue attempting to install PHPUnit.

Duplicate package channel:// found
Duplicate package channel:// found
install failed

If you see these errors then you need to explicitly tell PEAR to install these packages in addition to the PHPUnit package. If you see this error with File_Iterator, you will likely see the same error with Text_Template as well.

sudo pear install

This will provide PEAR the information that it needs to be able to install all of the appropriate packages. If you are using a newer version of PHP (5.3.3 or higher) you shouldn't have this problem.