Skip to content
An simple example of PHPUnit data proivder.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src/DataProvider/Example
tests/DataProvider/Example/Test
.codeclimate.yml
.gitignore
.travis.yml
README.md
circle.yml
composer.json
composer.lock

README.md

Data Provider Example

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

You can’t perform that action at this time.