A behavioural diff before and after a mechanical refactor

A refactor too large to review, and a test that compares behaviour rather than code.

// captured against the old code, once
foreach ($this->corpus() as $case) {
    $expected[$case->id] = $this->calculator->apply($case->input);
}
file_put_contents('baseline.json', json_encode($expected));

// and asserted against the new
public function test_behaviour_is_unchanged(): void
{
    foreach ($this->corpus() as $case) {
        self::assertEquals($baseline[$case->id], $this->calculator->apply($case->input));
    }
}

A corpus of four thousand real inputs, captured from a week of production traffic with the identifiers stripped, is a far stronger assertion than any set of hand-written cases and takes an afternoon to build. It only works for a pure function of its inputs, which is the constraint that decides where this technique applies.