I have a testclass in which one test runs multiple times via a @dataProvider and another test that @depends on the first method. However, when I called var_dump on what should be passed to the second test, it gives me a solid NULL, which I didn’t expect.
In other words: what should this do:
<?php
class DevicesTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider registerDataProvider
*/
public function testRegister($device, $supposedResult)
{
//do a bunch of tests
return array($device, $supposedResult);
}
public function registerDataProvider()
{
return array(array("foo", "foo"));
}
/**
* @depends testRegister
*/
public function testSaveDevicePreferences($deviceArr)
{
$this->assertNotEmpty($deviceArr);
}
}
?>
Normally
@dataProvideris used when you want to run a test multiple times with different data sets for each. It exists to save you from writing looping code in the test and to allow different data sets to pass or fail individually.As I said in my comments, I believe that PHPUnit will use either
@dependsor@dataProvider, and from your example my guess is that the second wins out. Another possibility is that tests with data providers cannot be used as dependencies because PHPUnit doesn’t know which test-plus-dataset to pick.Since
registerDataProviderreturns a single data set, you could just as easily call it from the test itself. This would allow@dependsto work in the second test without the@dataProvider. Assuming thattestRegisterneeds to modify$deviceand/or$supposedResult, this should work:If those variables don’t need to be modified by the first test, you can simply call
registerDataProviderfrom both tests. Note that PHPUnit will not separate a returned array from a dependend upon test into arguments to the dependent test as the data provider mechanism does. This is because it doesn’t know that the array being returned is multiple arguments versus a single argument.