Test methods often need data to be tested with. To test some methods completely you need to provide different data sets for every possible test condition. Of course, you can do it manually using loops, like this:
...
public function testSomething()
{
$data = [...];
foreach($data as $dataSet) {
$this->assertSomething($dataSet);
}
}
...
And someone can find it convenient. But there are some drawbacks of this approach. First, you'll have to perform additional actions to extract data if your test function accepts several parameters. Second, on failure it would be difficult to distinguish the failing data set without additional messages and debugging. Third, PHPUnit provides automatic way to deal with test data sets using data providers.
Data provider is a function, that should return data for your particular test case.
A data provider method must be public and either return an array of arrays or an object that implements the Iterator interface and yields an array for each iteration step. For each array that is part of the collection the test method will be called with the contents of the array as its arguments.
To use a data provider with your test, use @dataProvider
annotation with the name of data provider function specified:
/**
* @dataProvider dataProviderForTest
*/
public function testEquals($a, $b)
{
$this->assertEquals($a, $b);
}
public function dataProviderForTest()
{
return [
[1,1],
[2,2],
[3,2] //this will fail
];
}
Note that
dataProviderForTest()
returns array of arrays. Each nested array has two elements and they will fill necessary parameters fortestEquals()
one by one. Error like this will be thrownMissing argument 2 for Test::testEquals()
if there are not enough elements. PHPUnit will automatically loop through data and run tests:
public function dataProviderForTest()
{
return [
[1,1], // [0] testEquals($a = 1, $b = 1)
[2,2], // [1] testEquals($a = 2, $b = 2)
[3,2] // [2] There was 1 failure: 1) Test::testEquals with data set #2 (3, 4)
];
}
Each data set can be named for convenience. It will be easier to detect failing data:
public function dataProviderForTest()
{
return [
'Test 1' => [1,1], // [0] testEquals($a = 1, $b = 1)
'Test 2' => [2,2], // [1] testEquals($a = 2, $b = 2)
'Test 3' => [3,2] // [2] There was 1 failure:
// 1) Test::testEquals with data set "Test 3" (3, 4)
];
}
class MyIterator implements Iterator {
protected $array = [];
public function __construct($array) {
$this->array = $array;
}
function rewind() {
return reset($this->array);
}
function current() {
return current($this->array);
}
function key() {
return key($this->array);
}
function next() {
return next($this->array);
}
function valid() {
return key($this->array) !== null;
}
}
...
class Test extends TestCase
{
/**
* @dataProvider dataProviderForTest
*/
public function testEquals($a)
{
$toCompare = 0;
$this->assertEquals($a, $toCompare);
}
public function dataProviderForTest()
{
return new MyIterator([
'Test 1' => [0],
'Test 2' => [false],
'Test 3' => [null]
]);
}
}
As you can see, simple iterator also works.
Note that even for a single parameter, data provider must return an array
[$parameter]
Because if we change our current()
method (which actually return data on every iteration) to this:
function current() {
return current($this->array)[0];
}
Or change actual data:
return new MyIterator([
'Test 1' => 0,
'Test 2' => false,
'Test 3' => null
]);
We'll get an error:
There was 1 warning:
1) Warning
The data provider specified for Test::testEquals is invalid.
Of course, it is not useful to use
Iterator
object over a simple array. It should implement some specific logic for your case.
It is not explicitly noted and shown in manual, but you can also use a generator as data provider. Note that Generator
class actually implements Iterator
interface.
So here's an example of using DirectoryIterator
combined with generator
:
/**
* @param string $file
*
* @dataProvider fileDataProvider
*/
public function testSomethingWithFiles($fileName)
{
//$fileName is available here
//do test here
}
public function fileDataProvider()
{
$directory = new DirectoryIterator('path-to-the-directory');
foreach ($directory as $file) {
if ($file->isFile() && $file->isReadable()) {
yield [$file->getPathname()]; // invoke generator here.
}
}
}
Note provider
yield
s an array. You'll get an invalid-data-provider warning instead.