I recently got my hands on the book “PHPUnit Essentials” and decided to do a review.
The book offers a wide variety of information about PHPUnit. At the very beginning it provides insights how to install PHPUnit and how to set up a development environment. Further, it elaborates on the question why to do tests and how to write code that can be tested easier. Finally it reaches its culmination with more business oriented techniques of “Functional Tests in the Browser using Selenium” and “Continuous Integration”.
I skipped the first chapters about installing PHPUnit and Integration in IDEs, because I use it already in my daily work. But still while flying over the chapters I noticed that there is a good overview of the state of the art and lots of examples that make it much easier for beginners to try things on their own.
For example, there is a short introduction about the dependency manager Composer in the first chapter, which makes the installation of PHPUnit quite simple. There are also other methods mentioned (PEAR, native Linux packages and manual installation), but Composer is much more promoted, which is good in my opinion. It gives even beginners a much more professional feeling.
The second chapter gives an overview about largest or most used IDEs in PHP development: Netbeans, Zend Studio, Eclipse PDT, Phpstorm and shows how to set up a PHPUnit testing environment in each of them. Too bad that Aptana Studio was not mentioned, as it is my tool of choice for my daily work. Anyway everyone has to make their own choice and find out what they like best.
Chapter 3 explains what are tests and why they are important. It gives a good overview of what are testcases, the PHPUnit API with its different testing methods and how to create testcases.
I think when the project is small tests might not be needed, but as it grows they become more and more important. The difficult choice is when to start. Being disciplined in ones way of programming makes it much easier to implement tests later. On the other hand, using tests from the beginning helps to stay disciplined and keep up the code quality.
The Chapters 4 and 5 are about “Testing Dependencies and Exceptions” and “Running Tests from the Command Line”. Especially Chapter 4 includes an extensive example with a lot of code. I will not go into details here, but the use case is well explained. Chapter 5 explains in detail all the command line switches and usages. I think it is a little overkill, but as the book is directed to beginners, it might be fine.
The book gets more and more interesting towards the end. Now we are slowly moving to the more advanced features of PHPUnit: Chapter 6 “Test Isolation and Interaction”, Chapter 8 “Using Test Doubles”, Chapter 9 “Database Testing”, Chapter 10 “Testing APIs” and Chapter 11 “Testing Legacy Code”.
Yes I missed out Chapter 7 “Organizing Tests”. I think it should be moved more to the front of the book, as it is still about configuring PHPUnit and setting up a test suite. I develop mostly with Zend Framework 2 and using the Zend Skeleton Application it is basically everything set up for you, still it is good to know.
Chapter 9 “Database Testing” seems to be the largest chapter in the book. It highlights nicely how database testing works and also gives an introduction to Doctrine 2 ORM. Again a good gotcha for beginners that might not have come across Doctrine yet.
Chapter 10 was especially interesting for me, as I do a lot of API development and I know how difficult it is to do proper testing there. There are some nice examples using the Paypal API, Facebook API and Twitter API. In the last case they use the Twitter library which is integrated in Zend Framework 2. While all the APIs are based on OAuth and basically work in the same way, each example shows a different method of testing.
Chapter 11 “Testing Legary Code” is also an interesting chapter. The horror of every developer is to look into an old project which has to be updated and there is lots of “spaghetti code” inside where updating without destroying is like disarming a bomb. This chapter shows how to test “untestable” code, it include an example of testing some WordPress methods. Since Wordpress has historically grown with the development of PHP it includes lots of spaghetti code from the old times when there was not so much object orientation in PHP. The runkit PHP extension presented in the end of that chapter was new for me. It was really interesting to read about such powerful mechanism that even can override PHP internal methods.
The Chapters 12 and 13 presenting Selenium and Continuous Integration where definitely one of the most interesting chapters for me. Both are things that I wanted to try, but didn’t have the time and need to set up yet. These tools and methods are usually needed only on larger projects where many developers are involved. Not really necessary for beginners, but still good to know.
The book ends with “PHPUnit Alternatives, Extensions, Relatives and BDD“. The book goes away from PHPUnit and shows some alternative frameworks and different methods of testing. It is a good roundup on what can be found out there.
To sum it all up: the book offers state of the art information about testing in PHP and gives many real world examples that beginners can follow well. Even experts can find some useful information that they probably have not used in their daily work yet.
For my taste there was a little too much code in the book. I had the ebook version where it is fine in a way, because you can copy and test the examples yourself, but in a printed version this would be a lot of “wasted” pages. Still publishers will unlikely publish different versions of the same book.