Chapter 15. PHPUnit API

For most uses, PHPUnit has a simple API: subclass PHPUnit2_Framework_TestCase for your test cases and call assertTrue( ) or assertEquals( ). However, for those of you who would like to look deeper into PHPUnit, here are all of its published methods and classes.


Most of the time, you will encounter five classes or interfaces when you are using PHPUnit:


A collection of static methods for checking actual values against expected values


The interface of all objects that act like tests


A single test


A collection of tests


A summary of the results of running one or more tests

Figure 7 shows the relationship of the five basic classes and interfaces in PHPUnit: PHPUnit2_Framework_Assert, PHPUnit2_ Framework_Test, PHPUnit2_Framework_TestCase, PHPUnit2_ Framework_TestSuite, and PHPUnit2_Framework_TestResult.

The five basic classes and interfaces in PHPUnit
Figure 1-7. The five basic classes and interfaces in PHPUnit


Most test cases written for PHPUnit are derived indirectly from the class PHPUnit2_Framework_Assert, which contains methods for automatically checking values and reporting discrepancies. The methods are declared static, so you can write design-by-contract style assertions in your methods and have them reported through PHPUnit (Example 19).

Example 19. Design-by-contract ...

Get PHPUnit Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.