rawr/cross-data-providers

Allows you to use mix multiple PhpUnit @dataProvider without duplication

1.0.0 2019-06-07 15:48 UTC

README

t.regx.png

Cross @dataProviders

Handy require-dev testing tool for PhpUnit.

It allows you to create square matrices of your data providers!

Build Status Coverage Status Dependencies Repository Size License Composer lock

PHP Version PHP Version PHP Version PHP Version PHP Version PHP Version

PRs Welcome

  1. Installation
  2. Examples
  3. Supported PHP versions

Installation

Installation for PHP 5.6 and later:

$ composer require --dev rawr/cross-data-providers

Examples

Ever wanted to use multiple PhpUnit @dataProvider's with each other? Well, look no more :)

Imagine you have a service that allows you to log in to GitHub, BitBucket, SourceForge and GitLab with either SSH, HTTP or HTTPS and you want to test each possible configuration of those.

/**
 * @test
 * @dataProvider services
 */
public function shouldLogin(string $service, string $method) {
    // given
    $login = new Login($method);
    
    // when
    $result = $login->log($service);
    
    // then
    $this->assertTrue($result);
}

function services() {
    return DataProviders::crossAll(
      [
        ['github.com'],
        ['bitbucket.com'],
        ['gitlab.com'],
        ['sourceforge.net']
      ],
      [
        ['http'],
        ['https'],
        ['ssh']
      ]
    );
}

This is equivalent of having a regular dataProvider that looks like this

function services() {
    return [
        ['github.com', 'http'],
        ['github.com', 'https'],
        ['github.com', 'ssh'],
        ['bitbucket.com', 'http'],
        ['bitbucket.com', 'https'],
        ['bitbucket.com', 'ssh'],
        ['gitlab.com', 'http'],
        ['gitlab.com', 'https'],
        ['gitlab.com', 'ssh'],
        ['sourceforge.net', 'http'],
        ['sourceforge.net', 'https'],
        ['sourceforge.net', 'ssh'],
    ];
}

More advanced example

Let's say that apart from the domain and the protocol, you'd also like to add the protocol port, and the service title. Further more, you'd like to have three strategies of connection: lazy, eager and a test dry run.

/**
 * @test
 * @dataProvider services
 */
public function shouldLogin(string $service, string $title, string $method, int $port, $strategy) {
    // given
    $login = new Login($method, $port);
    $login->useStrategy($strategy);
    
    // when
    $result = $login->log($service);
    
    // then
    $this->assertTrue($result, "Failed to login to $title");
}

function services() {
    return DataProviders::crossAll(
      [
        // First two paramters: $service and $title
        ['github.com',      'GitHub'],
        ['bitbucket.com',   'BitBucket'],
        ['gitlab.com',      'GitLab'],
        ['sourceforge.net', 'SourceForge'],
        ['www.gitkraken.com', 'Git Kraken']
      ],
      [
        // Second pair of parameters: $method and $port
        ['http',  80],
        ['https', 443],
        ['ssh',   22]
      ],
      [
        // Last parameter: $strategy
        new EagerStrategy(),
        new LazyStrategy(),
        new DryRunStrategy(),
      ]
    );
}

This is equal to a @dataProvider with 45 entries. The test will be run 45 times, each time with a unique combination of your parameter sets :)