In a recent post to his site Matthieu Napoli shows you how to use the recently added anonymous classes functionality to help make your unit tests simpler. Anonymous classes allow for the on-demand creation of class instances without the need for the predefined class being required.

Anonymous classes were added to PHP 7. This article intends to show how useful they can be for writing tests.

He breaks it down into the three most useful places he uses them in his tests:

mocking classes to make it simpler to test method output

spies looking at properties in the mock class

fixture classes to help with needs such as reflection tests

Code examples are provided for each of the examples, especially the final point there.