An example of the DataProvider in PHP Unit.

0.0.2 2015-07-17 10:26 UTC

README

Build

Build Status

Code Quality

SensioLabsInsight Scrutinizer Code Quality Code Climate Codacy Badge

Code Style

StyleCI

Description

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.

Tests

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

Available in packagist

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",

What Next?

Issues are put in HuBorad, do have a look.

Contribution Guide

This is just an example