Having a formalized unit testing infrastructure for your projects will save you time in the long run, especially when looking for bugs in code. This article introduces you to setting up a unit testing framework. It is excerpted from chapter 6 of the book Advanced PHP Programming, written by George Schlossnagle (Sams; ISBN: 0672325616).
When you have a number of small test cases (for example, when checking that both the local part and the domain are split out correctly), you can avoid having to create a huge number of TestCase classes. To aid in this, a TestCase class can support multiple tests:
class EmailAddressTestCase extends
PHPUnit_Framework_TestCase{
public function _ _constructor($name) {
parent::_ _constructor($name);
}
public function testLocalPart() {
$email = new EmailAddress("george@omniti.com");
// check that the local part of the address is
equal to 'george'
$this->assertTrue($email->localPart == 'george');
}
public function testDomain() {
$email = new EmailAddress("george@omniti.com");
$this->assertEquals($email->domain, 'omniti.com');
}
}
Multiple tests are registered the same way as a single one:
$suite = new PHPUnit_FrameWork_TestSuite();
$suite->addTest(new
EmailAddressTestCase('testLocalPart'));
$suite->addTest(new
EmailAddressTestCase('testDomain'));
PHPUnit_TextUI_TestRunner::run($suite);
As a convenience, if you instantiate the PHPUnit_Framework_TestSuite object with the name of the TestCase class, $suite automatically causes any methods whose names begin with test to automatically register:
$suite = new
PHPUnit_Framework_TestSuite('EmailAddressTestCase');
// testLocalPart and testDomain are now
auto-registered
PHPUnit_TextUI_TestRunner::run($suite);
Note that if you add multiple tests to a suite by using addTest, the tests will be run in the order in which they were added. If you autoregister the tests, they will be registered in the order returned by get_class_methods() (which is how TestSuite extracts the test methods automatically).
Writing Inline and Out-of-Line Unit Tests
Unit tests are not only useful in initial development, but throughout the full life of a project. Any time you refactor code, you would like to be able to verify its correctness by running the full unit test suite against it. How do you best arrange unit tests so that they are easy to run, keep up-to-date, and carry along with the library?
There are two options for packaging unit tests. In the first case, you can incorporate your testing code directly into your libraries. This helps ensure that tests are kept up-to-date with the code they are testing, but it also has some drawbacks. The other option is to package your tests in separate files.