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

Matthieu Napoli:
Using anonymous classes to write simpler tests
Jan 24, 2017 @ 16:33:18

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.

tagged: anonymous class testing unittest mock spy fixture

Link: http://mnapoli.fr/anonymous-classes-in-tests/


Trending Topics: