PHPUnit: Testing Abstract Classes with an anonymous class

Image for post
Image for post
Photo by Luca Bravo on Unsplash

Before PHP 7, the only way to test an Abstract Class was to mock it, or to create a new Class separately and extending the abstract in just some other file separately.

Now, thanks to PHP 7, anonymous classes can be saved in one line. So no more empty instantiable classes in a separate file just for testing or mocking a class and “expecting” a method without testing the inner functionality.

This really helps with putting the testing logic into the file in charge of the testing instead of creating files not related to it or cluttering up unnecessary code.

Graphic Designer graduate. Full Stack Web Developer. Retired Tech & Gaming Editor.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store