*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;
use function sprintf;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class HtmlRenderer
{
/**
* @var string
*/
private const PAGE_HEADER = <<<'EOT'
Test Documentation
EOT;
/**
* @var string
*/
private const CLASS_HEADER = <<<'EOT'
%s
EOT;
/**
* @var string
*/
private const CLASS_FOOTER = <<<'EOT'
EOT;
/**
* @var string
*/
private const PAGE_FOOTER = <<<'EOT'
EOT;
/**
* @psalm-param array $tests
*/
public function render(array $tests): string
{
$buffer = self::PAGE_HEADER;
foreach ($tests as $prettifiedClassName => $_tests) {
$buffer .= sprintf(
self::CLASS_HEADER,
$prettifiedClassName,
);
foreach ($this->reduce($_tests) as $prettifiedMethodName => $outcome) {
$buffer .= sprintf(
" %s\n",
$outcome,
$prettifiedMethodName,
);
}
$buffer .= self::CLASS_FOOTER;
}
return $buffer . self::PAGE_FOOTER;
}
/**
* @psalm-return array
*/
private function reduce(TestResultCollection $tests): array
{
$result = [];
foreach ($tests as $test) {
$prettifiedMethodName = $test->test()->testDox()->prettifiedMethodName();
if (!isset($result[$prettifiedMethodName])) {
$result[$prettifiedMethodName] = $test->status()->isSuccess() ? 'success' : 'defect';
continue;
}
if ($test->status()->isSuccess()) {
continue;
}
$result[$prettifiedMethodName] = 'defect';
}
return $result;
}
}