phpunit: Unable to chain a @dataProvider in method a with a @depends in method b

Q A
PHPUnit version 7.0.2
PHP version 7.1.10
Installation Method Composer

Here is a code snippet of what I mean in the title

/**
 * @return array
 */
public function demoDP(): array 
{
	return [[0], [1], [2], [3]];
}

/**
 * @dataProvider demoDP
 * @param int $nr
 * @return int
 */
public function testDemoA(int $nr): int
{
	self::assertTrue($nr >= 0);
	return $nr + 1;
}

/**
 * @depends testDemoA
 * @param int $nr
 */
public function testDemoB(int $nr)
{
	self::assertTrue($nr > 0);
}

I would expect testDemoA to run 4 times: with $nr equal to 0, 1, 2 and 3. I also expect testDemoB to run 4 times: with $nr equal to 1, 2, 3 and 4.

testDemoA works fine, but testDemoB gives the error `Argument 1 passed to <testclass>::testDemoB() must be of the type integer, null given

If I don’t use the dataprovider for testDemoA, but a hardcoded value for $nr instead, testDemoB works fine, so the problem lies with using @depends in one method that refers to a method that uses a dataprovider.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (9 by maintainers)

Commits related to this issue

Most upvoted comments

Agreed, but this issue seems to have been reopened in #3498

I have done some digging! @bcremer I have found the root cause of your issue and will have a fix end of this weekend. Without boring you too much with the internals of PHPUnit, it has to do with how test names are handled: testName, TestCaseClass::testMethodName, with @dataprovider-details or even just a filename for PHPT tests.

The dependency resolver needs the names it gets to match exactly otherwise I will not see the dependencies. I will fix the name handling and provide extra test coverage.

I stubled upon the same issue. I added a minimal testcase here: https://github.com/sebastianbergmann/phpunit/issues/3168.