No more mental arithmetic: Gorm has had an Addition (bill) service written, which calculates each table's bill. Its rules fit in three lines:
- happy hour: 20% off everything;
- group of 6 adventurers or more: 10% off, which adds up with happy hour (100 gp → 80 → 72);
- amounts are rounded to the hundredth of a gold piece.
And when the bill is split (parPersonne(), per person), zero adventurers is an error (InvalidArgumentException).
This kind of rule breaks easily, and silently: a > instead of a >=, an elseif instead of an if… Write the tests that protect them.
Your mission
In tests/Taverne/AdditionTest.php, test total() and parPersonne(): each rule, and above all its boundaries.
A unit test
No need for a browser or a database here: Addition is a plain PHP object. The test creates it, calls it and checks the result. A unit test runs in a few milliseconds, and it tells you exactly what is broken.
Several cases, a single test: data providers
Rather than ten nearly identical methods, a data provider supplies the data sets:
public static function additions(): iterable { yield '6 adventurers: 10% off' => [[100.0], 6, false, 90.0]; // … } #[DataProvider('additions')] public function testTheTotal(array $prix, int $personnes, bool $happyHour, float $expected): void { $this->assertSame($expected, (new Addition())->total($prix, $personnes, $happyHour)); }
Four mutants 🐛 are waiting for you. Each one attacks a rule, right at its boundary.