A legacy report with no specification and forty branches cannot be unit tested, and it can be pinned: record what it produces today and fail when that changes.
public function testTheQuarterlyReportIsUnchanged(): void
{
$actual = (new QuarterlyReport($this->fixedDataset()))->generate();
$golden = __DIR__ . '/golden/quarterly.json';
if (getenv('UPDATE_GOLDEN')) {
file_put_contents($golden, json_encode($actual, JSON_PRETTY_PRINT));
}
$this->assertJsonStringEqualsJsonFile($golden, json_encode($actual));
}
This asserts that behaviour has not changed rather than that it is correct, which is exactly what a refactor needs and is not a substitute for understanding the report. The dataset has to be fixed and free of dates and randomness or the test is flaky. The update flag is the dangerous part: regenerating without reading the diff turns the test into a record of whatever the code currently does, which is the same failure mode as a snapshot test.