Introduction

PHPUnit is the defacto testing framework for PHP. If you want to start automating tests of your code, PHPUnit is the right tool for the job.

Why Automate Tests

Manual testing is an error prone and ineffective process.

Testing the same things over and over can bore anyone to death. And since that’s so boring, the natural tendency is to stop paying attention to details and miss bugs. Humans are not meant to do repetitive and mindless work; they’re meant to enjoy fun and creative activities like programming machines to do repetitive and mindless work for them.

And the good news is that while machines are dumb, they’re very fast at doing what they’ve been programmed to. So it makes perfect sense to teach them how to test our code and let them do that whenever we need them to.

Types of Tests

There are a lot of types of automated tests:

  • Unit,
  • Component,
  • Integration,
  • Functional,
  • UI/API,
  • End-to-end,
  • and so on.

Even though on a project with heavily automated testing you’ll meet tests of most of these categories, unit tests are the easiest to start with and that’s what we’ll focus on in this book. After you get familiar with PHPUnit and unit tests, you’ll be able to use it for other types of tests, too.

Who Is This Book For

I assume you’re familiar with PHP. You don’t have to be an expert; knowing basics of the language and having experience with actually writing PHP code is enough.

I don’t assume you know anything about automating tests and hence I’ll introduce you to whatever you need to know to write tests with PHPUnit.

How You Should Read This Book

This book is written in a tutorial style and each chapter flows into the next one. Hence you should read the book from cover to cover and not skip chapters.

Besides, the book is so short that there should be no reasons to jump ahead. The whole book can be read in a day or two.

Sample Project

Since this is not an abstract book, we’ll be writing actual code. And to do that, we need a sample project to focus on.

I don’t want to bother you with learning an unfamiliar domain just to be able to read this book, so the project has to be simple. But in order to have enough problems to automate tests for to show you the features of PHPUnit you’re very likely to use, it has to be not too simple.

Taking all that into account, I came up with an idea to write a simple email address checker subsystem.

Imagine you’re working on some kind of a social network where people sign up with an email address and a password. One of the system requirements is to not let people sign up with an email with certain characteristics. For instance, an email ending with @example.com is invalid because example.com is a reserved domain and no one actually uses it.

You’ve been tasked with creating such a blacklist email address subsystem and that’s what we’ll be doing in this book. It won’t be sophisticated enough for real use, but it will be enough for you to learn PHPUnit.

But first let’s install PHPUnit and ensure it works.