Innovations in PHPUnit 12: Separation of test stubs and mock objects

Sebastian Bergmann, author of PHPUnit, talks in an interview about highlights and preparation tips for the release of version 12 in February 2025.

listen Print view
Psychologist listens to his patient and writes down

(Image: Prostock-studio/Shutterstock.com)

5 min. read

Version 12 of the PHP testing framework PHPUnit is scheduled for release in early February 2025. iX talks to Sebastian Bergmann, initiator, main developer and maintainer of the project, about the current state of development, upcoming changes and personal highlights of the upcoming release.

Sebastian Bergmann has been an open source person from the very beginning. He shares his experience in publications and at conferences. He is also co-founder and Principal Consultant of The PHP Consulting Company (thePHP.cc).

iX: PHPUnit 12 is expected to be released in February 2025: What is the current state of development?

Sebastian Bergmann: A new major version like PHPUnit 12 is released every year on the first Friday in February. The release of PHPUnit 12.0 is therefore planned for February 7, 2025.

A release like PHPUnit 12.0 is mainly for cleanup and provides an annual opportunity to make changes that are not backward compatible. For example, features that were marked as "deprecated" in an earlier version are removed.

In the main branch of the PHPUnit repository on GitHub, all planned changes that are not backwards compatible are already implemented. In this respect, the current state of development already corresponds to what can be expected when PHPUnit 12 is released.

Videos by heise

iX: Which features of the new version are your personal highlights?

Sebastian Bergmann: A new version like PHPUnit 12.0 usually doesn't bring many new features, as it forms the basis for the feature releases of the year in February. However, there are a few small new features in PHPUnit 12.0 to enable even better control of code coverage via attributes.

However, my personal highlight of PHPUnit 12 is not a new feature, but rather the result of the clean-up, reorganization and improvement work of recent years: test stubs are finally clearly separated from mock objects.

Sebastian Bergmann, author of the PHP framework PHPUnit and co-founder of thePHP.cc.

Brief digression: In object-oriented programming, there are different types of objects: Services, entities and value objects, for example. Services encapsulate processes, which can be business processes or more technical things such as loading and saving data. Ideally, services implement an interface.

When testing, we use a real implementation of this interface if we want to test this implementation. We use a test stub of this interface if we want to test a component that depends on the interface. We use a mock object of the interface if we want to test the communication between cooperating objects.

When test doubles were introduced into the test frameworks of the time about 20 years ago and later became popular, the terms test stub and mock object were still interchangeable. For this reason, PHPUnit initially only offered one API, the getMock() method, for creating test doubles. This was confusing and the reason why we have worked a lot on the APIs for test doubles in recent years.

When reading test code, it's important to understand what it does and why it does it. For example, if a dependency is replaced by a test double for testing purposes, it is important to know why this is happening. If the component under test is only to be isolated from a dependency, a test stub should be used. If the communication between objects is to be specified and/or verified, a mock object is required. If an API for generating mock objects is used everywhere in the test code, it is only possible to recognize what the test is about at second or third glance. The strict separation of the APIs for creating test stubs and mock objects now makes this clearer.

iX: How can developers prepare for PHPUnit 12?

Sebastian Bergmann: Since the changes that are not backwards-compatible with PHPUnit 11 have already been implemented (see above), those who are brave enough can already test their own tests with what will become PHPUnit 12.

Of course, it is easier and less risky with the current stable version: If I can run my tests with PHPUnit 11.5 without the test runner "complaining" that I am using discontinued functionality (deprecations), then I know that I can run my tests with PHPUnit 12.

The questions were asked by Madeleine Domogalla.

(mdo)

Don't miss any news – follow us on Facebook, LinkedIn or Mastodon.

This article was originally published in German. It was translated with technical assistance and editorially reviewed before publication.