Skip to content

Commit

Permalink
Consolidate generators of unique identification for Test objects
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Nov 26, 2018
1 parent 8774833 commit c655a5d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
34 changes: 6 additions & 28 deletions src/Runner/TestSuiteSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ private function addSuiteToDefectSortOrder(TestSuite $suite): void
$max = 0;

foreach ($suite->tests() as $test) {
$testname = $this->getNormalizedTestName($test);
$testname = TestResultCache::getTestSorterUID($test);

if (!isset($this->defectSortOrder[$testname])) {
$this->defectSortOrder[$testname] = self::DEFECT_SORT_WEIGHT[$this->cache->getState($testname)];
Expand Down Expand Up @@ -233,8 +233,8 @@ function ($left, $right) {
*/
private function cmpDefectPriorityAndTime(Test $a, Test $b): int
{
$priorityA = $this->defectSortOrder[$this->getNormalizedTestName($a)] ?? 0;
$priorityB = $this->defectSortOrder[$this->getNormalizedTestName($b)] ?? 0;
$priorityA = $this->defectSortOrder[TestResultCache::getTestSorterUID($a)] ?? 0;
$priorityB = $this->defectSortOrder[TestResultCache::getTestSorterUID($b)] ?? 0;

if ($priorityB <=> $priorityA) {
// Sort defect weight descending
Expand All @@ -254,7 +254,7 @@ private function cmpDefectPriorityAndTime(Test $a, Test $b): int
*/
private function cmpDuration(Test $a, Test $b): int
{
return $this->cache->getTime($this->getNormalizedTestName($a)) <=> $this->cache->getTime($this->getNormalizedTestName($b));
return $this->cache->getTime(TestResultCache::getTestSorterUID($a)) <=> $this->cache->getTime(TestResultCache::getTestSorterUID($b));
}

/**
Expand All @@ -280,7 +280,7 @@ private function resolveDependencies(array $tests): array
do {
$todoNames = \array_map(
function ($test) {
return $this->getNormalizedTestName($test);
return TestResultCache::getTestSorterUID($test);
},
$tests
);
Expand All @@ -296,28 +296,6 @@ function ($test) {
return \array_merge($newTestOrder, $tests);
}

/**
* @param DataProviderTestSuite|TestCase $test
*
* @return string Full test name as "TestSuiteClassName::testMethodName"
*/
private function getNormalizedTestName($test): string
{
if ($test instanceof TestSuite && !($test instanceof DataProviderTestSuite)) {
return $test->getName();
}

if ($test instanceof PhptTestCase) {
return $test->getName();
}

if (\strpos($test->getName(), '::') !== false) {
return $test->getName(true);
}

return \get_class($test) . '::' . $test->getName(true);
}

/**
* @param DataProviderTestSuite|TestCase $test
*
Expand Down Expand Up @@ -348,7 +326,7 @@ private function calculateTestExecutionOrder(Test $suite): array
if ($suite instanceof TestSuite) {
foreach ($suite->tests() as $test) {
if (!($test instanceof TestSuite)) {
$tests[] = $this->getNormalizedTestName($test);
$tests[] = TestResultCache::getTestSorterUID($test);
} else {
$tests = \array_merge($tests, $this->calculateTestExecutionOrder($test));
}
Expand Down
38 changes: 14 additions & 24 deletions src/Util/TestDox/CliTestDoxPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Runner\TestResultCache;
use PHPUnit\TextUI\ResultPrinter;
use PHPUnit\Util\TestDox\TestResult as TestDoxTestResult;
use SebastianBergmann\Timer\Timer;
Expand Down Expand Up @@ -69,7 +70,7 @@ class CliTestDoxPrinter extends ResultPrinter

private $msg;

private $lastTestWasVerbose = false;
private $lastFlushedTestWasVerbose = false;

public function __construct($out = null, bool $verbose = false, $colors = self::COLOR_DEFAULT, bool $debug = false, $numberOfColumns = 80, bool $reverse = false)
{
Expand Down Expand Up @@ -134,7 +135,11 @@ public function endTest(Test $test, float $time): void
$msg = $this->formatTestResultMessage($this->formatWithColor('fg-green', ''), '', $time, $this->verbose);
}

$this->writeBufferedTestResult($test, $msg);
if ($this->bufferExecutionOrder) {
$this->bufferTestResult($test, $msg);
} else {
$this->writeTestResult($msg);
}

parent::endTest($test, $time);
}
Expand Down Expand Up @@ -175,24 +180,9 @@ public function addSkippedTest(Test $test, \Throwable $t, float $time): void
$this->msg = $this->formatTestResultMessage($this->formatWithColor('fg-yellow', ''), (string) $t, $time);
}

public function writeBufferedTestResult(Test $test, string $msg): void
public function bufferTestResult(Test $test, string $msg): void
{
if (!$this->bufferExecutionOrder) {
$this->writeTestResult($msg);

return;
}
$testName = '';

if ($test instanceof PhptTestCase) {
$testName = $test->getName();
} elseif ($test instanceof TestCase) {
$testName = $test->getName(true);

if (\strpos($testName, '::') === false) {
$testName = \get_class($test) . '::' . $testName;
}
}
$testName = TestResultCache::getTestSorterUID($test);

if ($testName == $this->originalExecutionOrder[$this->testFlushCount]) {
$prevClassName = $this->lastFlushedClassName();
Expand Down Expand Up @@ -272,16 +262,16 @@ private function formatTestSuiteHeader(?string $lastClassName, string $className
private function formatTestResultMessage(string $symbol, string $resultMessage, float $time, bool $verbose = false): string
{
$additionalInformation = $this->getFormattedAdditionalInformation($resultMessage, $verbose);
$msg = \sprintf(
"%s %s %s%s\n%s",
$this->lastTestWasVerbose ? "\n" : '',
$msg = \sprintf(
" %s %s%s\n%s",
$symbol,
$this->testMethod,
$verbose ? ' ' . $this->getFormattedRuntime($time) : '',
$additionalInformation
);

$this->lastTestWasVerbose = !empty($additionalInformation);
$this->lastFlushedTestWasVerbose = !empty($additionalInformation);

return $msg;
}

Expand Down Expand Up @@ -338,7 +328,7 @@ private function printNonSuccessfulTestsSummary(int $numberOfExecutedTests): voi

foreach ($this->nonSuccessfulTestResults as $result) {
$msg = $this->formatTestSuiteHeader($prevClassName, $result['className'], $result['message']);
$msg = strpos($msg, "\n") === 0 ? $msg : "\n$msg";
$msg = \strpos($msg, "\n") === 0 ? $msg : "\n$msg";
$this->write($msg);
$prevClassName = $result['className'];
}
Expand Down
21 changes: 21 additions & 0 deletions src/Util/TestResultCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
namespace PHPUnit\Runner;

use PHPUnit\Framework\Test;
use PHPUnit\Framework\TestCase;

class TestResultCache implements \Serializable, TestResultCacheInterface
{
/**
Expand Down Expand Up @@ -190,4 +193,22 @@ private function createDirectory(string $directory): bool
{
return !(!\is_dir($directory) && !@\mkdir($directory, 0777, true) && !\is_dir($directory));
}

public static function getTestSorterUID(Test $test): string
{
if ($test instanceof PhptTestCase) {
return $test->getName();
}

if ($test instanceof TestCase) {
$testName = $test->getName(true);

if (\strpos($testName, '::') === false) {
$testName = \get_class($test) . '::' . $testName;
}
return $testName;
}

return $test->getName();
}
}

0 comments on commit c655a5d

Please sign in to comment.