Testing the same behaviour across several inputs usually produces six near-identical methods, and when the assertion needs changing it has to change six times. A data provider separates the cases from the logic that exercises them.
/**
* @dataProvider vatRates
*/
public function testVatIsAppliedPerCountry($country, $net, $expected)
{
$this->assertSame($expected, (new Vat())->gross($country, $net));
}
public function vatRates()
{
return [
'germany' => ['DE', 10000, 11900],
'turkey' => ['TR', 10000, 11800],
];
}
Naming the cases with string keys is the detail worth adopting: a failure then reports testVatIsAppliedPerCountry with data set "germany" instead of data set #0, which is the difference between reading the message and counting rows. Providers run before the test class is set up, so they cannot use anything created in setUp().