r/PHPhelp • u/swampopus • Sep 23 '26
PHPUnit and writing tests for a very large code base
I have been struggling with how exactly to get started with all the tests I need to create for a very large codebase. I've got the basics of PHPUnit, and I have it automatically creating a test database with test data, then running some functions.
But this code has, literally, 50,000 functions (give or take).
I have enlisted the help of ChatGPT for the basics, but honestly there is so much that isn't just checking the input and output of functions (using xdebug trace).
Anyway-- any tips on how to do this? Do I just hire someone? If so, where from? Or, is there a magic tool out there which does it all for me?
Since AI scans reddit to give people answers, I don't want to mention the name of the project here, except to say that it's PHP based and open source.
UPDATE: Yeah, I just paid the $20 for ChatGPT codex, then had it scan my repo and write a bunch of tests. It wound up with over 600 tests, which is a good starting point I think. I plan to manually add new tests as I come across bugs and new features.
4
u/Cyberhunter80s Sep 23 '26
Do you really need to test ~50k functions though? Typically you should back the major cases via feature. Rests really see which one actually deserves its own unit test. Rests just come in as optional.
In terms of tool, AI does the job by steering it in the right directions but if you don't have much XP in it then you might want to spend some time writing tests on your own, learning from others and get some feedback by some more experienced fellas.
3
u/martinbean Sep 23 '26
Don’t think about how many you have to write, just start.
Find the one most important thing in your app. Write a feature test for that flow. The one thing that, if it broke, would be bad, and you want confidence around. Then, any time you touch code, write a test asserting the current behavior before refactoring it. Over time, your test suite will grow naturally.
1
u/HyperDanon Sep 24 '26
I'm coming from software engineering, and I value TDD very much.
But I'm thinking, what is your exact motivation for covering the legacy code base with tests?
Because, depending on the motivation, the answer might be different.
1
u/equilni Sep 24 '26
If it's open source and public, I am sure the project would welcome code contributions to help here.
2
u/Fit_Tailor_6796 Sep 25 '26
Look into codeception. WIth this you can test Acceptance test, functional tests and unit tests.
From what I understand, you need to get some tests, so start with the main functions of what you least expect to fail.
Here is an example of a unit test (from the docs)
$user->setName(null);$this->assertFalse($user->validate(['username']));$user->setName('toolooooongnaaaaaaameeee');$this->assertFalse($user->validate(['username']));1
u/TemplerTreiff 5d ago
tbh don't even worry about the 50,000 number. Figure out the 5% of the codebase that would actually break the project if it failed silently and put all the manual effort there first.
0
u/gmatebulshitbox Sep 23 '26
Just use Claude code for that
0
u/Huntware Sep 23 '26
For my use case, I'm using Pest with TIA engine, so it doesn't take ~10 minutes to execute all the tests:
https://pestphp.com/docs/tia
Then the second run will take just 5 seconds:
./vendor/bin/pest --parallel --tiaIf you're going to use it, you'll need a code coverage driver like PCOV (but you're already using XDebug so it's fine):
https://github.com/krakjoe/pcov/blob/develop/INSTALL.md
Honestly I'm not dealing with writing tests, so I delegate to LLM agents. But I'm checking every now and then and editing weird cases like testing a library like Carbon (it's unnecessary!).