Sitemap

Member-only story

How to test static function-calls in static nested classes

2 min readJun 26, 2025

--

Intro

As I’m working on the old codebase I wanted to become more confident about the code. Also I wanted to make it easier for following developer that enhance the code further more to know if something breaks.
I’m working with PHP so my go-to solution for this is PHPUnit by Sebastian Bergmann.
I looked at the code and found a lot of static functions calling other static functions. 3 static functions is not unusual but if I wanted to write a meaningful test I must check the results/data that are stored into the database.

Press enter or click to view image in full size

Mockery

After some unsuccessful tries with PHPUnit’s class mockings I searched in the internet and learned about Mockery. That did the trick for me.
Mockery provides an overload possibility for classes so I was able to overwrite the function.
With a closure I was able to verify the data with my test data.

Code

The code is hosted at codeberg in testing_static_classes — Repo, feel free to test it out.
The starting point is the UserTest-Class. I create the UserEntity Mock with 2 function, the `saveUserData` and `getUserData`. After that I use the Mockery::on function to be able to verify the provided data with my…

--

--

Florian Völker
Florian Völker

Written by Florian Völker

Professional Senior PHP Developer for ~13 Years, don't mind to do devOps, Serveradministration and project management