https://github.com/geshan/dataprovider-example
  
  
    A simple example of PHPUnit data provider. 
    https://github.com/geshan/dataprovider-example
  
data-provider packagist php-unit phpunit phpunit-tests quality
        Last synced: 8 months ago 
        JSON representation
    
A simple example of PHPUnit data provider.
- Host: GitHub
- URL: https://github.com/geshan/dataprovider-example
- Owner: geshan
- Created: 2014-01-31T08:38:37.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-09-01T11:34:59.000Z (about 10 years ago)
- Last Synced: 2025-01-10T08:30:49.331Z (10 months ago)
- Topics: data-provider, packagist, php-unit, phpunit, phpunit-tests, quality
- Language: PHP
- Homepage: https://geshan.com.np/blog/2014/02/using-phpunit-data-provider-for-less/
- Size: 262 KB
- Stars: 0
- Watchers: 3
- Forks: 2
- Open Issues: 0
- 
            Metadata Files:
            - Readme: README.md
 
Awesome Lists containing this project
README
          # Data Provider Example
## Build
[](https://travis-ci.org/geshan/dataprovider-example)
## Code Quality
[](https://insight.sensiolabs.com/projects/56ff09b2-7954-4cb2-a6da-6af863775107)
[](https://scrutinizer-ci.com/g/geshan/dataprovider-example/?branch=master)
[](https://codeclimate.com/github/geshan/dataprovider-example)
[](https://www.codacy.com/app/geshan/dataprovider-example)
## Code Style
[](https://styleci.io/repos/16403318)
## 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.
```php
    /**
    * 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](http://geshan.blogspot.com/2014/02/using-phpunit-data-provider-for-less.html).
## 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](http://bit.ly/1tLI2AC). 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](http://bit.ly/1Dkepso).
## Contribution Guide
This is just an example