Content & Coffee Content & Coffee

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.

Example:

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:
user_button_click_saves_document()
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.

Example:

gives_error_when_saving_not_successful()

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:
order_status_update_sends_email()
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.

  

Moving Forward

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.

  

Want to get started with automated testing?

 Don’t hesitate to contact us!

The author

Robert Brown

Senior Back-end PHP Developer

Web development is what I do. It's in my blood. There is no greater communication platform than the internet. I cherish any and every opportunity to be a part of it. My experience tells me what works. My knowledge tells me how it needs to be made. My "gut" tells me the rest.

Contact

Contact

Do you want more information, have a specific question of have a project you want us to work on? Let us know!

I'm interested in:
Loading
Thank you for your message

We will review and revert promptly.