The benefits of Automated Testing in PHP
In the year 2000, I started my computer science career. I knew immediately then as I do now that I was going to be a web developer. The internet was new, Google was still in its starting blocks, and every day new innovations were introduced into the market. In the 25+ years since I wrote my first “<html>” tag, nothing has advanced or brought more quality to the field of web development than automated testing.
The impact of PHPUnit
PHPUnit was brought into this world on November 27, 2001, by Sebastian Bergmann. Despite its 22-year history and proven track record, many large, well-known digital agencies, and big corporations still write PHP applications without using it. I can personally testify that even in 2023, “industry leaders” in PHP development still simply don’t write tests.
My Journey Towards Automated Testing
I myself admittedly was guilty of this well until the year 2016. That was when I started working at Content and Coffee and met Mathias Selleslach. With a kind guiding hand, my eyes were opened to the advantages and benefits of automated testing. No longer was I stuck in the typical web developer’s vicious cycle of “Write code, refresh browser, write code, refresh browser…”
The game had changed, I changed. No longer do I spin in a vicious cycle. In its place is now a progressive pattern, “Write small code, test it, move on…”
Advantages of Automated Testing
There are two main advantages that automated tests provide for you as a developer: A) You prove that your code is doing the “thing” it is supposed to be doing. B) You set a tripwire so that if another developer later “breaks” something, you know about it before it’s too late.
Demonstrating Completed Features with PHPUnit
One of the biggest time-wasting aspects of web development is proving to project managers and product owners that the feature that was requested has been implemented and completed. With PHPUnit tests to show and prove what you have done this now becomes a trivial task. Your test can literally be written and titled the same as the acceptance criteria that is in the story / issue.
In the Issue/Ticket it is written: “User clicks the button and the document is saved.” Your actual test in PHPUnit can be written as:
The code in this test will assume or simulate the button being clicked but also verify that code to save the document is indeed saving the document. In most modern issue tracking applications, this test can actually be linked to the specific acceptance criteria and even a little green check mark placed next to it when this test is passing.
Handling Problems with Automated Tests
In addition to satisfying acceptance criteria, automated testing can also ensure that you are handling problems correctly. For the previous scenario, what do you do if the document is not saved? Show an error message, presumably. Again, a test can be made for that.
In this test, you can set up numerous scenarios such as the disk being full, the storage location not being writable, an existing file already in the place of where you want to write. Having automated PHPUnit tests is the same as the old expression “Show Your Work” that you had way back in primary school math lessons.
Ensuring Consistent Behavior
As a tripwire, automated PHPUnit tests ensure that code is working and behaving the way that it is supposed to behave and continues to do so even when other changes and updates are happening.
Let’s take a situation where an email needs to be sent anytime the status of an order has been updated. Of course, that test would then take on the form of:
At a later time let’s say that a change to how emails are sent or constructed is made. If that change is now breaking the sending of the email, this test will tell us. We have tripped the tripwire. You know that there will be a problem if you put this code into production.
Automated testing isn’t just about ensuring that code written is good. Automated testing protects your company and bottom line. When a release is being published to production you know it’s safe and tested. You know there will be no surprises. You know that the client will be pleased. The question isn’t whether or not you can afford to write and do testing, the question is whether you can afford not to. Automated testing is at the core of everything we do here at Content and Coffee. We have a passion for helping people do digital projects. Implementing a continuous development pipeline with integrated automated testing is just one of our specialities. Invite us over for a coffee, we’ll get you sorted.