Skip to content

Commit

Permalink
- Fix conflicting placeholder replacement
Browse files Browse the repository at this point in the history
- Fix argument exporting inconsistencies
  • Loading branch information
marcospassos authored and sebastianbergmann committed Nov 27, 2018
1 parent c471247 commit 73a4961
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/Util/TestDox/NamePrettifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,19 @@ public function prettifyTestCase(TestCase $test): string
$annotations = $test->getAnnotations();
$annotationWithPlaceholders = false;

$callback = static function (string $variable) : string {
return \sprintf('/%s(?=\b)/', \preg_quote($variable, '/'));
};

if (isset($annotations['method']['testdox'][0])) {
$result = $annotations['method']['testdox'][0];

if (\strpos($result, '$') !== false) {
$annotation = $annotations['method']['testdox'][0];
$providedData = $this->mapTestMethodParameterNamesToProvidedDataValues($test);
$variables = \array_map($callback, \array_keys($providedData));

$result = \trim(
\str_replace(
\array_keys($providedData),
$providedData,
$annotation
)
);
$result = \trim(\preg_replace($variables, $providedData, $annotation));

$annotationWithPlaceholders = true;
}
Expand Down Expand Up @@ -161,7 +160,9 @@ private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test)
$providedDataValues = \array_values($test->getProvidedData());
$i = 0;

foreach ($reflector->getParameters() as $parameter) {
$parameters = $reflector->getParameters();

foreach ($parameters as $parameter) {
if (!\array_key_exists($i, $providedDataValues) && $parameter->isDefaultValueAvailable()) {
$providedDataValues[$i] = $parameter->getDefaultValue();
}
Expand All @@ -180,7 +181,7 @@ private function mapTestMethodParameterNamesToProvidedDataValues(TestCase $test)
$value = \gettype($value);
}

if (\is_bool($value) || \is_numeric($value)) {
if (\is_bool($value) || \is_int($value) || \is_float($value)) {
$exporter = new Exporter;

$value = $exporter->export($value);
Expand Down
58 changes: 58 additions & 0 deletions tests/unit/Util/TestDox/NamePrettifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,62 @@ public function getAnnotations(): array

$this->assertEquals('1 + 2 = 3', $this->namePrettifier->prettifyTestCase($test));
}

public function testPhpDoxArgumentExporting(): void
{
$test = new class extends TestCase {
public function __construct()
{
parent::__construct('testExport', [
'int' => 1234,
'strInt' => '1234',
'float' => 2.123,
'strFloat' => '2.123',
'string' => 'foo',
'bool' => true,
'null' => null,
]);
}

public function testExport($int, $strInt, $float, $strFloat, $string, $bool, $null): void
{
}

public function getAnnotations(): array
{
return [
'method' => [
'testdox' => ['$int, $strInt, $float, $strFloat, $string, $bool, $null'],
],
];
}
};

$this->assertEquals('1234, 1234, 2.123, 2.123, foo, true, NULL', $this->namePrettifier->prettifyTestCase($test));
}

public function testPhpDoxReplacesLongerVariablesFirst(): void
{
$test = new class extends TestCase {
public function __construct()
{
parent::__construct('testFoo', []);
}

public function testFoo(int $a = 1, int $ab = 2, int $abc = 3): void
{
}

public function getAnnotations(): array
{
return [
'method' => [
'testdox' => ['$a, "$a", $a$ab, $abc, $abcd, $ab'],
],
];
}
};

$this->assertEquals('1, "1", 12, 3, $abcd, 2', $this->namePrettifier->prettifyTestCase($test));
}
}

0 comments on commit 73a4961

Please sign in to comment.