Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix testdox printer #5521

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 83 additions & 20 deletions src/Logging/TestDox/PlainTextRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
*/
namespace PHPUnit\Logging\TestDox;

use function array_map;
use function implode;
use function is_int;
use function preg_split;
use function sprintf;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\TestData\NoDataSetFromDataProviderException;
use PHPUnit\Framework\TestStatus\TestStatus;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
Expand All @@ -26,12 +33,8 @@ public function render(array $tests): string
foreach ($tests as $prettifiedClassName => $_tests) {
$buffer .= $prettifiedClassName . "\n";

foreach ($this->reduce($_tests) as $prettifiedMethodName => $outcome) {
$buffer .= sprintf(
' [%s] %s' . "\n",
$outcome,
$prettifiedMethodName,
);
foreach ($_tests as $test) {
$buffer .= $this->renderTestResult($test);
}

$buffer .= "\n";
Expand All @@ -41,28 +44,88 @@ public function render(array $tests): string
}

/**
* @psalm-return array<string, 'x'|' '>
* @throws NoDataSetFromDataProviderException
*/
private function reduce(TestResultCollection $tests): array
private function renderTestResult(TestResult $testResult): string
{
$result = [];
$method = $testResult->test();

foreach ($tests as $test) {
$prettifiedMethodName = $test->test()->testDox()->prettifiedMethodName();
$status = $testResult->status();

if (!isset($result[$prettifiedMethodName])) {
$result[$prettifiedMethodName] = $test->status()->isSuccess() ? 'x' : ' ';
return sprintf(
'%s%s',
$this->renderTestResultHeader($method, $status),
$this->renderTestResultBody($method, $status),
);
}

continue;
}
/**
* @throws NoDataSetFromDataProviderException
*/
private function renderTestResultHeader(TestMethod $method, TestStatus $status): string
{
$testStatus = $status->isSuccess() ? 'x' : ' ';

if ($test->status()->isSuccess()) {
continue;
}
$prettifiedMethodName = $method->testDox()->prettifiedMethodName();

$result[$prettifiedMethodName] = ' ';
$testData = $method->testData();

if ($testData->hasDataFromDataProvider()) {
$dataSetNameOrInt = $testData->dataFromDataProvider()->dataSetName();

$dataSetName = sprintf(
' with data set %s',
is_int($dataSetNameOrInt) ? '#' . $dataSetNameOrInt : '"' . $dataSetNameOrInt . '"',
);
}

return $result;
return sprintf(
' [%s] %s%s%s',
$testStatus,
$prettifiedMethodName,
$dataSetName ?? '',
"\n",
);
}

private function renderTestResultBody(TestMethod $method, TestStatus $status): string
{
if ($status->isSuccess()) {
return '';
}

return sprintf(
"%s\n%s\n%s\n%s\n",
$this->prefixLines($this->prefixFor('start'), ''),
$this->prefixLines($this->prefixFor('message'), $status->message()),
$this->prefixLines($this->prefixFor('default'), sprintf("\n%s:%d", $method->file(), $method->line())),
$this->prefixLines($this->prefixFor('last'), ''),
);
}

private function prefixLines(string $prefix, string $message): string
{
return implode(
"\n",
array_map(
static fn (string $line) => ' ' . $prefix . ($line ? ' ' . $line : ''),
preg_split('/\r\n|\r|\n/', $message),
),
);
}

/**
* @psalm-param 'default'|'start'|'message'|'diff'|'trace'|'last' $type
*/
private function prefixFor(string $type): string
{
return match ($type) {
'default' => '│',
'start' => '┐',
'message' => '├',
'diff' => '┊',
'trace' => '╵',
'last' => '┴',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\Test\BeforeFirstTestMethodErrored;
use PHPUnit\Event\Test\BeforeFirstTestMethodErroredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class BeforeTestClassMethodErroredSubscriber extends Subscriber implements BeforeFirstTestMethodErroredSubscriber
{
public function notify(BeforeFirstTestMethodErrored $event): void
{
$this->collector()->beforeTestClassMethodErrored($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestRunner\ExecutionStarted;
use PHPUnit\Event\TestRunner\ExecutionStartedSubscriber as TestRunnerExecutionStartedSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class ExecutionStartedSubscriber extends Subscriber implements TestRunnerExecutionStartedSubscriber
{
public function notify(ExecutionStarted $event): void
{
$this->collector()->executionStarted($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestRunner\DeprecationTriggered;
use PHPUnit\Event\TestRunner\DeprecationTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestRunnerTriggeredDeprecationSubscriber extends Subscriber implements DeprecationTriggeredSubscriber
{
public function notify(DeprecationTriggered $event): void
{
$this->collector()->testRunnerTriggeredDeprecation($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestRunner\WarningTriggered;
use PHPUnit\Event\TestRunner\WarningTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestRunnerTriggeredWarningSubscriber extends Subscriber implements WarningTriggeredSubscriber
{
public function notify(WarningTriggered $event): void
{
$this->collector()->testRunnerTriggeredWarning($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestData\NoDataSetFromDataProviderException;
use PHPUnit\Event\TestSuite\Finished;
use PHPUnit\Event\TestSuite\FinishedSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestSuiteFinishedSubscriber extends Subscriber implements FinishedSubscriber
{
/**
* @throws NoDataSetFromDataProviderException
*/
public function notify(Finished $event): void
{
$this->collector()->testSuiteFinished($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestSuite\Skipped;
use PHPUnit\Event\TestSuite\SkippedSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestSuiteSkippedSubscriber extends Subscriber implements SkippedSubscriber
{
public function notify(Skipped $event): void
{
$this->collector()->testSuiteSkipped($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\TestSuite\Started;
use PHPUnit\Event\TestSuite\StartedSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestSuiteStartedSubscriber extends Subscriber implements StartedSubscriber
{
public function notify(Started $event): void
{
$this->collector()->testSuiteStarted($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\Test\DeprecationTriggered;
use PHPUnit\Event\Test\DeprecationTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestTriggeredDeprecationSubscriber extends Subscriber implements DeprecationTriggeredSubscriber
{
public function notify(DeprecationTriggered $event): void
{
$this->collector()->testTriggeredDeprecation($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\Test\ErrorTriggered;
use PHPUnit\Event\Test\ErrorTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestTriggeredErrorSubscriber extends Subscriber implements ErrorTriggeredSubscriber
{
public function notify(ErrorTriggered $event): void
{
$this->collector()->testTriggeredError($event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Logging\TestDox;

use PHPUnit\Event\Test\NoticeTriggered;
use PHPUnit\Event\Test\NoticeTriggeredSubscriber;

/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class TestTriggeredNoticeSubscriber extends Subscriber implements NoticeTriggeredSubscriber
{
public function notify(NoticeTriggered $event): void
{
$this->collector()->testTriggeredNotice($event);
}
}
Loading