How to pass an array as an argument from suppliers in PHPUnit?

Do I have a testing method that takes an array as an argument and I want to pass data from a data provider method?

How can this be achieved?

public function dataProvider(){ return array(array( 'id'=>1, 'description'=>'', )); } /** * @dataProvider dataProvider */ public function testAddDocument($data){ // data here shall be an array provided by the data provider // some test data here } 

What happens is that it passes the key value to "id" ... etc.

I want to pass the whole array

+4
source share
1 answer

Data transfer methods must return an array containing one array for each set of arguments in order to proceed to the testing method. To pass an array, include it in other arguments. Note that in your sample code you will need another enclosing array.

Here is an example that returns two data sets, each with two arguments (array and string).

 public function dataProvider() { return array( // data sets array( // data set 0 array( // first argument 'id' => 1, 'description' => 'this', ), 'foo', // second argument ), array( // data set 1 array( // first argument 'id' => 2, 'description' => 'that', ), 'bar', // second argument ), ); } 

Important: Data provider methods must be non-statistical. PHPUnit instantiates a test case to call each data provider method.

+9
source

All Articles