Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Jessica Mauerhan:
The Five Types of Test Doubles & How to Create Them in PHPUnit
Oct 11, 2018 @ 10:53:56

Jessica Mauerhan has a tutorial posted to her site that covers the five types of test doubles in PHPUnit and how to use them in your tests. Test "doubles" - the most common one being a mock - are useful for simulating resources or other objects required to test units of code without external interactions.

Did you know that a Mock is only one type of a test double? Most of us use the word “mock” to mean any kind of test double, but there’s actually five different types. It really can help you understand what you’re trying to accomplish with your test if you know a little bit more what you’re doing with your test doubles, so this article will explain the kinds of test doubles, when you use them, how you use them and why.

She starts the post with a brief definition of the term "test double" and then covers each of the five with descriptions and example code:

  • Dummy
  • Stub
  • Spy
  • Mock
  • Fake

While some of the functionality is similar between the types, she defines their differences and the cases where each would be most useful.

tagged: tutorial test doubles unittest phpunit types top5

Link: https://jmauerhan.wordpress.com/2018/10/04/the-5-types-of-test-doubles-and-how-to-create-them-in-phpunit/

Richard Bagshaw:
Jun 24, 2016 @ 09:11:01

Richard Bagshaw has a post to his site sharing some of his experience with the Prophecy testing tool and how it compares to Mockery for creating test doubles (mocks and stubs).

For a while now I have been using Mockery as my test double framework of choice, however recently I have been taking a look at Prophecy as an alternative.

[...] "Prophecy is a highly opinionated yet very powerful and flexible PHP object mocking framework. Though initially it was created to fulfil phpspec2 needs, it is flexible enough to be used inside any testing framework out there with minimal effort."

He then gets into some basic usage of the tool - creating a basic mock, assigning expectations and behaviors and performing the test. He steps through each line of the example explaining what's happening and what can be expected as a result. He ends the post with some final thoughts comparing Prophecy to the normal PHPUnit mocking tools and points out several other features it makes easier to work with as well.

tagged: prophecy unittest doubles mock stub example introduction tutorial

Link: http://www.richardbagshaw.co.uk/prophecy/

Adam Wathan:
Writing Your Own Test Doubles
May 11, 2016 @ 10:19:58

In this recent post to his site Adam Wathan about writing your own custom test doubles (fakes) to help make your tests cleaner and improve their overall readability/maintainability.

Once in a while I run into a situation where trying to use a mocking library hurts the readability of my test. For example, say I’m building out a basic user registration flow where someone signs up and receives a welcome email. [...] To test that an account is created correctly, I can make a request to the endpoint and verify that the new account exists in a test database. [...] This covers creating the account itself, but what’s the best way to test the welcome email?

He goes through a few of the options that could be used to test this including using Mockery to replace the mailer class with a spy or actually sending emails. There's downfalls to both of these methods and he suggests using a custom "fake" where the mailer class is swapped out with an "in-memory" option with the same kind of interface. He does point out a few issues with this method, however, and offers a few tips to remember when using them.

tagged: custom test unittest doubles email example video screencast

Link: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/

Dave Marshall:
Mockery Spies
Oct 09, 2014 @ 10:29:08

In his latest post Dave Marshall takes a look at a handy feature of the Mockery mocking tool (helpful for unit testing) and how to use them in your testing.

Spies have been on the cards for mockery for a long time and even after putting together an implementation in February, I kind of stalled out on making a decision on the public API. Fast forward a few months and I figured it was just time to ship it, so I went with the most mockery like API and merged it in. Mockery still doesn't have a 1.0 release, so I can always make changes before we go 1.0.

For those not familiar with the concept of "spies" in testing he includes a brief definition and some of the reasoning behind using them. The first is relatively simple: how they can reveal the intent of the test. They also allow for two other types of testing methods, "Arrange-Act-Assert" or "Given-When-Then" thinking patterns. He does mention, however, some of the problems with using spies over mocks (including that they're less precise, possibly leading to looser testing). He finishes up the post with a quick note about partial spies and how they can provide a nice compromise in your testing.

tagged: mockery unittest spies doubles mock compare feature

Link: http://davedevelopment.co.uk/2014/10/09/mockery-spies.html

Inviqa techPortal:
PHP Test Doubles Patterns with Prophecy
Jul 23, 2013 @ 12:31:08

The Inviqa techPortal has a new post today from Marcello Duarte about using Prophecy in unit testing to work with mocks and doubles.

Test doubles are a way of replacing all classes in our tests other than the one we are testing. They give us control on direct and indirect outputs that would have been generated by those classes. Understanding the role of the test double helps us to write better tests, and thus better code. [...] When writing unit tests we are focusing on describing one unit of behaviour at a time, and how objects communicate in order to achieve that behaviour. We instantiate the object under test, call a method of the object and observe if the outcome is as expected.

In his examples he uses Prophecy to mock out different kinds of objects - a dummy object, a "fake" (with listeners for other object calls), a stub that mocks other internal method calls and a more familiar mock object. He also talks about two guidelines to follow when thinking about writing tests and code complexity - the Law of Demeter and "Don't Mock What You Don't Own." He gives an example of working around this last one by using the Adapter design pattern.

tagged: prophecy unittest designpattern doubles mock stub dummy fake

Link: http://techportal.inviqa.com/2013/07/23/php-test-doubles-patterns-with-prophecy

All About Mocking with PHPUnit
Sep 28, 2012 @ 09:46:21

NetTuts.com has another post for those out there wanting to further their PHP unit testing knowledge. In this new tutorial, Csaba Patkos introduces you to mocking objects with PHP Unit - a powerful method to help test some of the more difficult things.

There are two styles of testing: “black box” and “white box” styles. Black box testing focuses on the object’s state; whereas, white box testing focuses on behavior. The two styles complement each other and can be combined to thoroughly test code. Mocking allows us to test behavior, and this tutorial combines the mocking concept with TDD to build an example class that uses several other components to achieve its goal.

He illustrates mocking with his sample "toy car" application and shows how to use a few different types of mocks (test doubles) to create some tests. These types include dummy objects, test stubs, test mocks and test fakes. Code is included for all test examples, including some showing the use of the actual PHPUnit mocking functionality.

tagged: mocking phpunit tutorial doubles unittest