Skip to content

Commit

Permalink
Move getTestSorterUID back its natural habitat
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Nov 26, 2018
1 parent 6834b15 commit 8682a3f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
31 changes: 25 additions & 6 deletions src/Runner/TestSuiteSorter.php
Expand Up @@ -74,6 +74,25 @@ final class TestSuiteSorter
*/
private $executionOrder = [];

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();
}

public function __construct(?TestResultCacheInterface $cache = null)
{
$this->cache = $cache ?? new NullTestResultCache;
Expand Down Expand Up @@ -167,7 +186,7 @@ private function addSuiteToDefectSortOrder(TestSuite $suite): void
$max = 0;

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

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

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

/**
Expand All @@ -280,7 +299,7 @@ private function resolveDependencies(array $tests): array
do {
$todoNames = \array_map(
function ($test) {
return TestResultCache::getTestSorterUID($test);
return self::getTestSorterUID($test);
},
$tests
);
Expand Down Expand Up @@ -326,7 +345,7 @@ private function calculateTestExecutionOrder(Test $suite): array
if ($suite instanceof TestSuite) {
foreach ($suite->tests() as $test) {
if (!($test instanceof TestSuite)) {
$tests[] = TestResultCache::getTestSorterUID($test);
$tests[] = self::getTestSorterUID($test);
} else {
$tests = \array_merge($tests, $this->calculateTestExecutionOrder($test));
}
Expand Down
4 changes: 2 additions & 2 deletions src/Util/TestDox/CliTestDoxPrinter.php
Expand Up @@ -16,7 +16,7 @@
use PHPUnit\Framework\TestSuite;
use PHPUnit\Framework\Warning;
use PHPUnit\Runner\PhptTestCase;
use PHPUnit\Runner\TestResultCache;
use PHPUnit\Runner\TestSuiteSorter;
use PHPUnit\TextUI\ResultPrinter;
use SebastianBergmann\Timer\Timer;

Expand Down Expand Up @@ -241,7 +241,7 @@ public function bufferTestResult(Test $test, string $msg): void
{
$this->outputBuffer[$this->testIndex] = [
'className' => $this->className,
'testName' => TestResultCache::getTestSorterUID($test),
'testName' => TestSuiteSorter::getTestSorterUID($test),
'testMethod' => $this->testMethod,
'message' => $msg,
'failed' => $this->lastTestFailed,
Expand Down
20 changes: 0 additions & 20 deletions src/Util/TestResultCache.php
Expand Up @@ -10,7 +10,6 @@
namespace PHPUnit\Runner;

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

class TestResultCache implements \Serializable, TestResultCacheInterface
{
Expand Down Expand Up @@ -64,25 +63,6 @@ class TestResultCache implements \Serializable, TestResultCacheInterface
*/
private $times = [];

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();
}

public function __construct($filename = null)
{
$this->cacheFilename = $filename ?? $_ENV['PHPUNIT_RESULT_CACHE'] ?? self::DEFAULT_RESULT_CACHE_FILENAME;
Expand Down

0 comments on commit 8682a3f

Please sign in to comment.