Skip to content

Commit c17423b

Browse files
Closes #3196
1 parent dc98a61 commit c17423b

File tree

6 files changed

+100
-10
lines changed

6 files changed

+100
-10
lines changed

ChangeLog-7.3.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ All notable changes of the PHPUnit 7.3 release series are documented in this fil
1616
* The `--order-by=reverse` CLI option should now be used instead of `--reverse-order`
1717
* Implemented [#3161](https://github.com/sebastianbergmann/phpunit/pull/3161): Support for indexed arrays in `PHPUnit\Framework\Constraint\ArraySubset`
1818
* Implemented [#3194](https://github.com/sebastianbergmann/phpunit/issues/3194): `@covers class` (and `@uses class`) should include traits used by class
19+
* Implemented [#3196](https://github.com/sebastianbergmann/phpunit/issues/3196): Support for replacing placeholders in `@testdox` text with data provider values
1920

2021
[7.3.0]: https://github.com/sebastianbergmann/phpunit/compare/7.2...7.3.0
2122

phpstan-tests.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ parameters:
4444
- tests/_files/phpunit-example-extension/tests/OneTest.php
4545
- tests/Regression/Trac/783/OneTest.php
4646
- tests/_files/3194.php
47+
- tests/_files/RouterTest.php

src/Framework/TestCase.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,6 +1121,14 @@ public function getDataSetAsString(bool $includeData = true): string
11211121
return $buffer;
11221122
}
11231123

1124+
/**
1125+
* Gets the data set of a TestCase.
1126+
*/
1127+
public function getProvidedData(): array
1128+
{
1129+
return $this->data;
1130+
}
1131+
11241132
/**
11251133
* Override to run the test and assert its state.
11261134
*
@@ -1546,14 +1554,6 @@ protected function prophesize($classOrInterface = null): ObjectProphecy
15461554
return $this->getProphet()->prophesize($classOrInterface);
15471555
}
15481556

1549-
/**
1550-
* Gets the data set of a TestCase.
1551-
*/
1552-
protected function getProvidedData(): array
1553-
{
1554-
return $this->data;
1555-
}
1556-
15571557
/**
15581558
* Creates a default TestResult object.
15591559
*/

src/Util/TestDox/NamePrettifier.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,35 @@ public function prettifyTestClass(string $className): string
5656

5757
public function prettifyTestCase(TestCase $test): string
5858
{
59-
$annotations = $test->getAnnotations();
59+
$annotations = $test->getAnnotations();
60+
$annotationWithPlaceholders = false;
6061

6162
if (isset($annotations['method']['testdox'][0])) {
6263
$result = $annotations['method']['testdox'][0];
64+
65+
if (\strpos($result, '$') !== false) {
66+
$annotation = $annotations['method']['testdox'][0];
67+
$result = '';
68+
69+
$providedData = $this->mapTestMethodParameterNamesToProvidedDataValues($test);
70+
71+
foreach (\explode(' ', $annotation) as $word) {
72+
if (\strpos($word, '$') === 0) {
73+
$result .= $providedData[$word] . ' ';
74+
} else {
75+
$result .= $word . ' ';
76+
}
77+
}
78+
79+
$result = \trim($result);
80+
81+
$annotationWithPlaceholders = true;
82+
}
6383
} else {
6484
$result = $this->prettifyTestMethod($test->getName(false));
6585
}
6686

67-
if ($test->usesDataProvider()) {
87+
if ($test->usesDataProvider() && !$annotationWithPlaceholders) {
6888
$result .= ' data set "' . $test->dataDescription() . '"';
6989
}
7090

@@ -130,4 +150,18 @@ public function prettifyTestMethod(string $name): string
130150

131151
return $buffer;
132152
}
153+
154+
private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test): array
155+
{
156+
$reflector = new \ReflectionMethod(\get_class($test), $test->getName(false));
157+
$providedData = [];
158+
$providedDataValues = $test->getProvidedData();
159+
$i = 0;
160+
161+
foreach ($reflector->getParameters() as $parameter) {
162+
$providedData['$' . $parameter->getName()] = $providedDataValues[$i++];
163+
}
164+
165+
return $providedData;
166+
}
133167
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
phpunit --testdox RouterTest ../_files/RouterTest.php
3+
--FILE--
4+
<?php
5+
$_SERVER['argv'][1] = '--no-configuration';
6+
$_SERVER['argv'][2] = '--testdox';
7+
$_SERVER['argv'][3] = 'RouterTest';
8+
$_SERVER['argv'][4] = __DIR__ . '/../_files/RouterTest.php';
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
PHPUnit\TextUI\Command::main();
12+
--EXPECTF--
13+
PHPUnit %s by Sebastian Bergmann and contributors.
14+
15+
Router
16+
✔ Routes /foo/bar to FooBarHandler
17+
18+
Time: %s, Memory: %s
19+
20+
OK (1 test, 1 assertion)

tests/_files/RouterTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
use PHPUnit\Framework\TestCase;
12+
13+
final class RouterTest extends TestCase
14+
{
15+
/**
16+
* @dataProvider routesProvider
17+
* @testdox Routes $url to $handler
18+
*/
19+
public function testRoutesRequest(string $url, string $handler): void
20+
{
21+
$this->assertTrue(true);
22+
}
23+
24+
public function routesProvider()
25+
{
26+
return [
27+
'/foo/bar' => [
28+
'/foo/bar',
29+
FooBarHandler::class,
30+
// ...
31+
]
32+
];
33+
}
34+
}

0 commit comments

Comments
 (0)