Key points
- A characterization test records what the code does today, not what it should do.
- AI is handy for listing branches and edge cases, but every test must pass against the current code untouched.
- Break the code on purpose to prove the tests actually catch something.
Every inherited project has one: the dreaded function. Three hundred lines, nested conditions, no tests and a “do not touch” comment. Before refactoring it, you need a safety net that proves its behaviour has not changed. Writing that net by hand is slow and tedious, which is precisely the kind of work where an AI assistant saves us real time, provided we ask for the right thing.
Ask it to freeze behaviour, not fix it
The usual mistake is to ask “write tests for this function”. The assistant then tests what the function is supposed to do, judging by its name. What you want is the opposite: a record of what it actually does, bugs included. Our prompt looks like this:
Here is shipping_price() and the functions it calls.
Write PHPUnit 10 tests that pin down its CURRENT behaviour:
- one case per branch in the code, plus the boundary values;
- do not fix anything: if a result looks wrong, keep it
and add a "probable bug" comment;
- no mocks, except for network calls and the current date.
Include the code of the functions it calls, too. Without them the model guesses, and its guesses end up baked into your expected values.
What comes back
For a shipping cost function, you get something along these lines:
use PHPUnitFrameworkAttributesDataProvider;
use PHPUnitFrameworkTestCase;
final class ShippingPriceTest extends TestCase
{
public static function cases(): array
{
return [
'empty basket' => [0.0, 'FR', 0.0],
'below threshold' => [49.99, 'FR', 6.9],
'exact threshold' => [50.0, 'FR', 0.0],
'unlisted country' => [30.0, 'XX', 14.9],
'negative total' => [-5.0, 'FR', 6.9], // probable bug, kept as is
];
}
#[DataProvider('cases')]
public function test_current_behaviour(float $total, string $country, float $expected): void
{
$this->assertEqualsWithDelta($expected, shipping_price($total, $country), 0.001);
}
}
The negative total is the interesting one. The function happily accepts it, which is almost certainly a bug. Fixing it now, though, would mix two changes in one. Keep it, leave the comment, and fix it in its own commit once the refactor is done.
Check the tests before trusting them
- Every test must pass on the existing code with no code changes. A failure means the model guessed wrong: work out why, then set the expected value from the real output.
- Look at coverage. A coverage report (Xdebug or PCOV) shows which branches no case reaches. Feed those back to the assistant.
- Break the code on purpose. Flip a condition, nudge a threshold. If nothing turns red, your net has a hole. Infection can automate this mutation testing.
- Watch what you paste. No API keys, passwords or real customer data in your prompt. Anonymise sample data first.
Only then, refactor
With the net in place, refactor in small steps and run the suite after each one. On the projects we take over, this is usually when we find two branches doing exactly the same thing, or a parameter nobody has used in years.
FAQ
Do these tests stay in the project afterwards?
Usually, yes. Once a recorded bug is fixed, update and rename its test so it describes the intended behaviour.
What if the function relies on the database or on WordPress?
Start by extracting the pure calculation into its own function you can test without a database. For the rest, WordPress integration tests (wp-env, WP_UnitTestCase) take over.
Got a PHP application or WordPress site nobody dares to change any more? We take over and modernise existing code, with tests to back every change.



