Admin
How I Review AI-Generated Laravel Code Before Merging
A customer has a $10 regular item and a $5 sale item in the same cart. They add a 10% coupon. The coupon should apply only to the regular item, so the checkout total is $14.00. A plausible patch charges $13.50 and still passes the existing test, because that test only covers a full-price item.
This is a standalone checkout example inspired by e-commerce work like Midas WDA. I use it to show what I look for in a Laravel pull request when a coding agent changes a business rule.
Write the business rule beside the diff
Both items stay in the subtotal. Only full-price items count toward the coupon. On this cart, the customer gets $1.00 off the $15.00 subtotal. Write that expected total down before reading the implementation. It gives you an independent result to check.
The proposed change looks tidy at first glance. It applies the coupon to the cart subtotal.
$subtotalMinor = array_sum(array_column($lines, 'line_total_minor'));
$couponMinor = intdiv($subtotalMinor * $couponPercent, 100);
return $subtotalMinor - $couponMinor;That works when every line is eligible. On a mixed cart, it also discounts the sale item. In the diff I look for the point where eligibility is checked, then follow the amount that reaches the percentage calculation.
Make the missing case fail first
Add a regression test with one eligible line and one excluded line. This PHPUnit method goes in a Laravel test class with the calculator in scope. The two inputs are already line totals after any item-level markdown, expressed in cents.
public function test_coupon_excludes_sale_lines_from_discount(): void
{
$lines = [
['line_total_minor' => 1_000, 'on_sale' => false],
['line_total_minor' => 500, 'on_sale' => true],
];
self::assertSame(1_400, checkoutTotalMinor($lines, 10));
}The first calculation gives 1,350 cents. The assertion expects 1,400. A test that only checks a full-price cart passes with either implementation and leaves this rule uncovered.
Calculate the coupon from eligible lines
Keep the original subtotal for checkout, and compute the coupon from a separate subtotal that includes only eligible lines.
function checkoutTotalMinor(array $lines, int $couponPercent): int
{
$subtotalMinor = array_sum(array_column($lines, 'line_total_minor'));
$eligibleLines = array_filter(
$lines,
static fn (array $line): bool => !$line['on_sale'],
);
$eligibleMinor = array_sum(array_column($eligibleLines, 'line_total_minor'));
$couponMinor = intdiv($eligibleMinor * $couponPercent, 100);
return $subtotalMinor - $couponMinor;
}The example uses a validated whole-number coupon rate and assumes nonnegative line totals. It truncates fractional cents with intdiv in both versions. In an existing checkout I keep its rounding policy and verify the handling of tax and shipping separately.
Check what actually changed
Cart | Expected | Original | Corrected |
|---|---|---|---|
Regular item with coupon | $9.00 | $9.00 | $9.00 |
Regular and sale items with coupon | $14.00 | $13.50 | $14.00 |
Sale item with coupon | $5.00 | $4.50 | $5.00 |
Mixed cart without coupon | $15.00 | $15.00 | $15.00 |
I checked these four cases in a standalone reproduction of the calculation. For a Laravel pull request, I would also add an HTTP feature test around the real checkout endpoint. That test should use the product and coupon fixtures the application already trusts and assert the persisted or returned total, depending on what the endpoint does. Laravel HTTP testing documentation.
What earns the merge
For this example, I would approve the pricing change once the mixed-cart test fails on the original code, passes on the patch, and the existing checkout tests still pass. I would also check that the server supplies the price and sale status from trusted records. A correct formula can still produce the wrong result when a request controls its inputs.
If you are reviewing the decisions of an AI agent inside the product, see how I test a decision before an agent changes an order.

Discussion (0)
Be the first to join the conversation.