function AddClassTest::testAddWithDataProvider

Same name in other branches
  1. 4.0.x modules/phpunit_example/tests/src/Unit/AddClassTest.php \Drupal\Tests\phpunit_example\Unit\AddClassTest::testAddWithDataProvider()

Test AddClass::add() with a data provider method.

This method is very similar to testAdd(), but uses a data provider method to test with a wider range of data.

You can tell PHPUnit which method is the data provider using the '@dataProvider' annotation.

The data provider method just returns a big array of arrays of arguments. That is, for each time you want this test method run, the data provider should create an array of arguments for this method. In this case, it's $expected, $a, and $b. So one set of arguments would look a bit like this pseudocode:

array(
    valueForExpected,
    valueForA,
    valueForB,
);

It would then wrap this up in a higher-level array, so that PHPUnit can loop through them, like this pseudocode:

return array(
    array(
        first,
        set,
    ),
    array(
        next,
        set,
    ),
);

This test has a better methodology than testAdd(), because it can easily be adapted by other developers, and because it tries more than one data set. This test is much better than testAdd(), although it still only tests 'good' data. When combined with testAddWithBadDataProvider(), we get a better picture of the behavior of the method under test.

@dataProvider addDataProvider

See also

self::addDataProvider()

File

modules/phpunit_example/tests/src/Unit/AddClassTest.php, line 87

Class

AddClassTest
AddClass units tests.

Namespace

Drupal\Tests\phpunit_example\Unit

Code

public function testAddWithDataProvider($expected, $a, $b) {
    $sut = new AddClass();
    $this->assertEquals($expected, $sut->add($a, $b));
}