A Gentle Introduction to Testing in PHP

A Gentle Introduction to Testing in PHP

Learning testing can be very overwhelming especially if you're just starting out. I felt very overwhelmed when I started learning it. And after years of writing tests and learning how to write them (and I still do), I realized that it can be easier to learn than it's supposed to be.

You might have read a bunch of PHPUnit tutorials and maybe you tried to apply some of what you've learned in your projects, but it doesn't always feel right. And sometimes you get into situations where you have no clue how to test some feature in your application.

If anything of this happened to you, don't feel bad, we all have had this experience. And it's almost because of the way we learn it.

In this article, I'm going to give you a quick introduction on how PHP applications are being tested. You won't see any code here, instead I'm going to focus more on the basic concepts of testing. And then, in future tutorials, we'll see testing more in practice.

Everyone is already testing

We all know that testing has countless of benefits (or you won't be reading this in the first place!). And it's clear that the main goal of it, is to make sure that our code is still working even after changes.

Does that workflow of checking your work after changes feel familiar to you? We all tend to manually open up the browser to ensure that we didn't break anything with our changes. This is called testing, more specifically manual testing.

But as you can imagine, it becomes extremely difficult to check our work manually each time we make a change especially when the project gets larger.

Because of that, we write automated tests. And this is what tools like PHPUnit are made for (we'll learn about it in future posts).

Testing types

The key to understanding testing is to know the types of testing available to us. Each one may require a full post to explain. So let's make it simple and talk about the important aspects of each one.

Functional Testing

This one uses the same style as manual testing, which we talked about earlier — you basically write tests that simulate the browser.

So for example, you'd write a test that visits a certain page and check to see if a certain text exists on it.

All of that would run behind the scenes, so it's not necessary to see the browser opens and the URL address changes (unless you're using Selenium).

Unit Testing

As the name implies, we use it to test units. These units are the smallest parts of our applications such as classes & functions. So you take a single class and test it in isolation without any participation from any other class.

So it seems like testing units alone is not enough. In many situations you'll find that all your tests are passing but the whole application doesn't work. Because of that, we support our application with other types of tests such as functional & integration.

Integration Testing

So, we saw that functional testing is a very high-level testing that tests the application as a whole (like manual testing). Also we saw that unit testing is used to test the smallest units in the system like classes & functions.

Integration testing is like a middle point between the two. This one often deals with multiple objects interacting with each other.

For example, if you are building a Todo list app, you might have a Task class and a List class. With this type of testing, you might write a test to see how a Task is saved into a List — We let them interact with one another.

What about the database?

A common question beginners ask is how to test the database. There are actually several ways to do that (which we'll discuss in another post).

But for now, all I want you to know is that we don't touch the database with our unit tests. We do that with a higher level tests like Integration & Functional.

What tools to use?

There are countless PHP testing tools out there. The good news is we don't have to know them all. Actually, you can do pretty much everything with just one tool — like PHPUnit.

If you search the internet for the available testing tools, you'll find that some of them are more focused on a specific testing type than another. For example, PHPSpec focuses more on unit testing.

Also you'll find that a tool like Codeception (which is built on top of PHPUnit) supports all testing types.

It's also worth mentioning that you can use more than one tool at the same time. For example, you can use PHPSpec for unit testing and PHPUnit for integration testing.

The important thing I want you to take from this section, is not to make it about the tool. Tools themselves don't matter. What matters, though, is how to use them to write clean, useful tests.

What is Test-driven development (TDD)?

A common misconception among developers is that TDD means unit testing. TDD is just a methodology that we use when we are testing. And it can be used with any type of testing (Functional, Integration, Unit).

In a nutshell, TDD is to write your tests before your actual code. If this is new to you, it won't make much sense in the beginning. But once you understand the insights behind it, it'll become your most favorite part in testing.

As this is beyond the scope of this article, we won't discuss it further. But I might dedicate a whole article for it in the future. But at least for now, you know what TDD means.

Where to go from here?

If you have any experience with testing, you'll notice that there are many topics I didn't touch on in this article. For example, I didn't talk about Test Doubles (mocks), or what a Regression test is, or what's the difference between Acceptance & Functional testing, etc...

The great thing is that you don't have to know everything to start testing your code. With a basic understanding of each type, you can test almost everything you want.

They certainly have their uses, but for this simple introduction you don't have to care much about it, just focus on the basics.

Now I think you know everything you need to know to start learning testing in practice. So, in the next tutorial, we'll see how to install and use PHPUnit to write our very first unit tests.

PHP Testing
Taha Shashtari

About Taha Shashtari

I'm a freelance web developer. Laravel & VueJS are my main tools these days and I love building stuff using them. I write constantly on this blog to share my knowledge and thoughts on things related to web development... Let's be friends on twitter.