O'Reilly logo

PHPUnit Essentials by Zdenek Machek

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Test doubles in action

Enough of theory, let's have a look at a simple example that will show us where test doubles can be very useful.

Let's create an example that will demonstrate a simple Transaction class. It will take the passed data, create an XML request, send it to the third-party API, and log the request. This is quite a common situation. Let's keep this example simple, and see how you can use test doubles to write tests, as shown in the following code snippet:

<?php class Transaction { private $logger; private $client; private $data; private $response; public function __construct(ILogger $logger, HttpClient $client, array $data) { $this->logger = $logger; $this->client = $client; $this->data = $data; } public function prepareXMLRequest() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required