TDD
TDD writes the test before the code. Red → Green → Refactor cycles drive simple, testable design.
TDD writes the test before the code. Red → Green → Refactor cycles drive simple, testable design.
// 1. Red: write failing test
public function test_discount_applied_on_orders_over_100(): void {
$order = new Order(items: [new Item(120)]);
$this->assertEquals(108.0, $order->total()); // 10% off
}
// 2. Green: write minimal code to pass
class Order {
public function total(): float {
$sub = array_sum(array_column($this->items, "price"));
return $sub > 100 ? $sub * 0.9 : $sub;
}
}
// 3. Refactor: extract DiscountPolicy, keep tests green
TDD is a design tool as much as a testing tool — it pushes you toward simple, injectable code.