A solid unit test suite is essential for ongoing development in large projects, especially those with many people involved. Going back and manually testing every individual component of an application after every change is impractical. Your unit tests will help alleviate that by automatically testing your application’s components and alerting you when something is not working the same way it was when you wrote your tests.

The Zend Framework 2 API uses PHPUnit, and so does this tutorial application. A detailed explanation of unit testing is beyond the scope of this tutorial, so we will only provide sample tests for the components in the pages that follow. This tutorial assumes that you already have PHPUnit installed.

Setting up the Tests Directory

Start by creating a directory called test in zf2-tutorial\module\Application with the following subdirectories:

zf2-tutorial/
/module
/Application
/test
/ApplicationTest
/Model

The structure of the test directory matches exactly with that of the module’s source files, and it will allow you to keep your tests well-organized and easy to find.

Bootstrapping Your Tests

Next, create a file called phpunit.xml under zf2-tutorial/module/Application/test: