Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Writing your own test doubles

Adam Wathan posted another excellent article on testing on his blog. This time he talks about creating test doubles. Adam demonstrates that creating your own fake can result in a much more readable test than using mocks or spies.

You’ll learn to create an InMemoryMailer like this:

public function test_new_users_are_sent_a_welcome_email()
    $mailer = new InMemoryMailer;

    $this->post('register', [
        'name' => 'John Doe',
        'email' => [email protected]',
        'password' => 'secret',

    $this->assertTrue($mailer->hasMessageFor([email protected]'));
    $this->assertTrue($mailer->hasMessageWithSubject('Welcome to my app!'));

That seems like a pretty readable test to me. Read (or view) the whole tutorial here: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band.