Skip to content

Commit 016505e

Browse files
Cache result of prettifyTestCase()
1 parent 9f3967c commit 016505e

File tree

1 file changed

+53
-20
lines changed

1 file changed

+53
-20
lines changed

src/Logging/TestDox/NamePrettifier.php

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ final class NamePrettifier
5959
*/
6060
private array $strings = [];
6161

62+
/**
63+
* @var array<non-empty-string, non-empty-string>
64+
*/
65+
private array $prettifiedTestCases = [];
66+
6267
/**
6368
* @param class-string $className
6469
*/
@@ -172,40 +177,39 @@ public function prettifyTestMethodName(string $name): string
172177

173178
public function prettifyTestCase(TestCase $test, bool $colorize): string
174179
{
175-
$annotationWithPlaceholders = false;
176-
$methodLevelTestDox = MetadataRegistry::parser()->forMethod($test::class, $test->name())->isTestDox()->isMethodLevel();
180+
$key = $test::class . '#' . $test->name();
177181

178-
if ($methodLevelTestDox->isNotEmpty()) {
179-
$methodLevelTestDox = $methodLevelTestDox->asArray()[0];
182+
if ($test->usesDataProvider()) {
183+
$key .= '#' . $test->dataName();
184+
}
180185

181-
assert($methodLevelTestDox instanceof TestDox);
186+
if ($colorize) {
187+
$key .= '#colorize';
188+
}
182189

183-
$result = $methodLevelTestDox->text();
190+
if (isset($this->prettifiedTestCases[$key])) {
191+
return $this->prettifiedTestCases[$key];
192+
}
184193

185-
if (str_contains($result, '$')) {
186-
$annotation = $result;
187-
$providedData = $this->mapTestMethodParameterNamesToProvidedDataValues($test, $colorize);
194+
$testDox = MetadataRegistry::parser()->forMethod($test::class, $test->name())->isTestDox()->isMethodLevel();
195+
$isCustomized = false;
188196

189-
$variables = array_map(
190-
static fn (string $variable): string => sprintf(
191-
'/%s(?=\b)/',
192-
preg_quote($variable, '/'),
193-
),
194-
array_keys($providedData),
195-
);
197+
if ($testDox->isNotEmpty()) {
198+
$testDox = $testDox->asArray()[0];
196199

197-
$result = preg_replace($variables, $providedData, $annotation);
200+
assert($testDox instanceof TestDox);
198201

199-
$annotationWithPlaceholders = true;
200-
}
202+
[$result, $isCustomized] = $this->processTestDox($test, $testDox, $colorize);
201203
} else {
202204
$result = $this->prettifyTestMethodName($test->name());
203205
}
204206

205-
if (!$annotationWithPlaceholders && $test->usesDataProvider()) {
207+
if (!$isCustomized && $test->usesDataProvider()) {
206208
$result .= $this->prettifyDataSet($test, $colorize);
207209
}
208210

211+
$this->prettifiedTestCases[$key] = $result;
212+
209213
return $result;
210214
}
211215

@@ -305,4 +309,33 @@ private function objectToString(object $value): string
305309

306310
return $value::class;
307311
}
312+
313+
/**
314+
* @return array{0: string, 1: bool}
315+
*/
316+
private function processTestDox(TestCase $test, TestDox $testDox, bool $colorize): array
317+
{
318+
$placeholdersUsed = false;
319+
320+
$result = $testDox->text();
321+
322+
if (str_contains($result, '$')) {
323+
$annotation = $result;
324+
$providedData = $this->mapTestMethodParameterNamesToProvidedDataValues($test, $colorize);
325+
326+
$variables = array_map(
327+
static fn (string $variable): string => sprintf(
328+
'/%s(?=\b)/',
329+
preg_quote($variable, '/'),
330+
),
331+
array_keys($providedData),
332+
);
333+
334+
$result = preg_replace($variables, $providedData, $annotation);
335+
336+
$placeholdersUsed = true;
337+
}
338+
339+
return [$result, $placeholdersUsed];
340+
}
308341
}

0 commit comments

Comments
 (0)