This is a simple example of using data provider in PHP Unit. It can be used to write less test with multiple data sets keeping the code coverage high.
/**
* Data provider for testCalculateTotal
* variables are in the order of
* $paymentMethod, $expectedTotal
*
* @return type
*/
public function paymentMethodProvider()
{
return [
['Cash', 100.00],
['Credit Card', 95.00],
];
}
/**
* Test to check if the order total is calculated correctly
* for given payment method.
*
* @param string $paymentMethod
* @param float $expectedTotal
*
* @dataProvider paymentMethodProvider
*/
public function testCalculateTotal($paymentMethod, $expectedTotal)
{
$this->checkout->calculateTotal($paymentMethod);
$this->assertEquals(
$this->checkout->getTotal(),
$expectedTotal,
sprintf('Testing total calculation for %s.', $paymentMethod)
);
}
The description of how use data provider in PHP Unit is provided in my blog post.
You can run composer update
~> composer update --prefer-dist
and then run the tests using the command below on folder where the repo is cloned.
phpunit --bootstrap=vendor/autoload.php tests
The package is availabe in packagist. You can use it for reference by adding the followign line to you composer.json file
"data-provider/example" : "0.0.2",
Issues are put in HuBorad, do have a look.
This is just an example