| Q |
A |
| PHPUnit version |
13.0.2 |
| PHP version |
8.4.11 |
| Installation Method |
Composer |
Summary
PHPUnit 12 and 13 support specifying named arguments in a data provider, and ignore order. So you can use whatever order you want in the data provider.
However, the TestDox attribute seems to use the order provided, regardless of the name. So the first data provider argument is used for the first variable seen in the TestDox, regardless of name. I believe this to be a bug.
See examples below.
How to reproduce
class Rectangle
{
public float $area { get => $this->height * $this->width; }
public function __construct(public int $height, public int $width) {}
}
#[TestDox('A rectangle with documentation')]
class DocumentedRectangleTest extends TestCase
{
public static function areaProvider(): \Generator
{
yield '2x2=4' => [
'height' => 2,
'width' => 2,
'expectedArea' => 4,
];
yield '3x2=6' => [
'height' => 3,
'width' => 2,
'expectedArea' => 6,
];
// Note this one is out of order.
// This causes no issue for executing the code, but does break the TestDox.
yield '2x3=6' => [
'expectedArea' => 6,
'height' => 2,
'width' => 3,
];
}
#[Test]
#[TestDox('A rectangle with height $height and width $width has an area of $expectedArea')]
#[DataProvider('areaProvider')]
public function areaWithProvider(int $height, int $width, float $expectedArea): void
{
$r = new Rectangle($height, $width);
self::assertSame($expectedArea, $r->area);
}
}
Current behavior
$ vendor/bin/phpunit --testdox
[...]
A rectangle with documentation
✔ A rectangle with height 2 and width 2 has an area of 4
✔ A rectangle with height 3 and width 2 has an area of 6
✔ A rectangle with height 6 and width 2 has an area of 3
OK (3 tests, 3 assertions)
Expected behavior
$ vendor/bin/phpunit --testdox
[...]
A rectangle with documentation
✔ A rectangle with height 2 and width 2 has an area of 4
✔ A rectangle with height 3 and width 2 has an area of 6
✔ A rectangle with height 2 and width 3 has an area of 6
OK (3 tests, 3 assertions)
Summary
PHPUnit 12 and 13 support specifying named arguments in a data provider, and ignore order. So you can use whatever order you want in the data provider.
However, the
TestDoxattribute seems to use the order provided, regardless of the name. So the first data provider argument is used for the first variable seen in the TestDox, regardless of name. I believe this to be a bug.See examples below.
How to reproduce
Current behavior
Expected behavior