...

/

Unit Testing: Coding with Dependencies

Unit Testing: Coding with Dependencies

Learn how to use mocks and stubs to test classes with dependencies.

We can unit test things that depend on something else. To test them in isolation, we must replace all the dependencies with dummy implementations called test doubles. Fortunately, with mocking libraries, making such dummy implementations is easy.

PHPUnit has built-in mocking functionality, but phpspec/prophecy provides a friendlier API.

Stubs for dependencies that provide data

We use stubs for dependencies that only provide data. Stubs return dummy data so our function can work.

For example, let’s create a function that creaties a time-related greeting. It will return “Good ...