Testing – It’s Easier Than You Think

Testing is an often-neglected part in PHP development. Compared to languages such as Java and Ruby, where testing is strongly ingrained into the workflow of developers, PHP has been lagging behind. This is mainly because simple PHP applications tend to be tightly coupled and are, therefore, difficult to test. However, thanks to standardization and modularization efforts and frameworks that encourage the separation of concerns, PHP testing has become more accessible and the mentality towards it is slowly changing.

Laravel 5 is a framework that was built from the ground up to facilitate testing. It comes with all the necessary files to get started, along with different helpers to test your application, thus helping beginners to overcome some of the biggest obstacles.

In this chapter, we will demonstrate how Laravel makes it very simple to get started with testing, without forcing you to go for a test-first approach, or making you aim for complete test coverage. In this gentle introduction to testing, we will look at the following topics:

The advantages of writing tests for your application
How to prepare your tests
The software design patterns that Laravel fosters
How to use Mockery to test objects in isolation
The built-in features and helpers that facilitate testing

The benefits of testing

The anatomy of a test