The bill, please

Exercise 3 of 6 in chapter 6 · 230 XP · 5 objectives checked by tests

Topics covered: Unit testTestCase#[DataProvider]expectException

No more mental arithmetic: Gorm has had an Addition (bill) service written, which calculates each table's bill. Its rules fit in three lines:

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.