Testing: More Information About Testing in PHP

As your projects grow larger, the benefits of comprehensive testing increase as well. At first, it feels like a drag to write a bunch of seemingly extra code to verify some­thing obvious, such as the basic mathematical operations in restaurant_check(). But as your project accumulates more and more functionality (and perhaps more and more people working on it), the accumulated tests are invaluable.

Absent some fancy-pants computer science formal methods, which rarely find their way into modern PHP applications, the results of your tests are the evidence you have to answer the question “How do you know your program does what you think it does?” With tests, you know what the program does because you run it in various ways and ensure the results are what you expect.

This chapter shows the basics for integrating PHPUnit into your project and writing some simple tests. To go further, here are a few additional resources about PHPUnit and testing in general:

  • The PHPUnit manual is helpful and comprehensive. It includes tutorial-style information on common PHPUnit tasks as well as reference material on PHPU- nit’s features.
  • There is a great list of presentations about PHPUnit at https://phpunit.de/presen- html.
  • Browsing the test directory of popular PHP packages to see how those packages do their tests is instructive as well. In the Zend Framework, you can find the tests for the zend-form component and the zend-validator component on GitHub. The popular Monolog package has its tests on on GitHub as well.
  • Naturally, PHPUnit has numerous tests that verify its behavior. And those tests are PHPUnit tests!

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

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