Problem with most simple examples showing how to use PHP Unite Test is that the examples are – well – simple. It looks like – yes, PHPUnit Testing is working, but not worth using (too simple scenarios). The reality is, that for simple interfaces, simple scenarios PHPUnit Test is a kind of overkill – it can be used, but more development time will be lost than it is worth.
Where PHPUnit really shines is when you use it in more advanced and complex projects, where failure on production version is a Big No No. At first sight PHPUnit Test assertions look simple, and they are simple indeed. The point however is, that there are many assertions, which being simple help to to understand them quicker, use them more effectively, and at the same time cover large area of possible case scenarios hitting precisely every aspect of project functionality.
PHPUnit Tests help you not only to check if this or that function, or class produces correct results, it also makes you thinking about possible scenarios, like what kind of data should you really expect. Suddenly, a piece of functionality can be seen more like from potential user’s point of view. PHPUnit forces you to dive deeper into functionality and replace some functionality/data cogs which would break at some point in production.
Also, many developers believes that manual testing can be sufficient. Well, there are right as long as there are few or several tests to perform. But, for instance, for complex functionality you usually check how many basic possibilities you have and how many different options is per single functionality, and then you multiply the numbers. For a function or module accepting three different types of data it can be like 4 * 2 * 2 = 16. Well, it doesn’t look now like something good for manual testing – just for one sweep where every possibility/option is tested only once you have to do 16 tests. If you do the 16 tests it means in fact, that per every scenario you got only one test, and it doesn’t sound encouraging. Here comes automation. With something like PHPUnit Test you can automate testing process and thousands of tests are not that big problem anymore. Automated PHPUnit Tests are not worth to do for very simple function or classes, but for commercial projects thorough testing is absolute necessity, and it is just as important part of development like writing code.
Game Changer – Testing without corrupting database data.
Tests can be simple for simple classes or functions, but when you have to test using database data things quickly become complex and a bit dangerous. Manual testing with some data existing in database tables usually related with each other, but without corrupting the data can be very stressful and rather should not be done. With Laravel containing integrated PHPUnit out of the box testing with fake data without corrupting the main database is not a problem anymore and makes testing much faster and tests results more reliable.