Skip to content

Commit

Permalink
Add functionality to calculate execution order to TestSuiteSorter
Browse files Browse the repository at this point in the history
  • Loading branch information
epdenouden authored and sebastianbergmann committed Nov 26, 2018
1 parent eee9e67 commit d634df7
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 63 deletions.
51 changes: 49 additions & 2 deletions src/Runner/TestSuiteSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ final class TestSuiteSorter
*/
private $cache;

/**
* @var array array<string> A list of normalized names of tests before reordering
*/
private $originalExecutionOrder = [];

/**
* @var array array<string> A list of normalized names of tests affected by reordering
*/
private $executionOrder = [];

public function __construct(?TestResultCacheInterface $cache = null)
{
$this->cache = $cache ?? new NullTestResultCache;
Expand All @@ -72,7 +82,7 @@ public function __construct(?TestResultCacheInterface $cache = null)
/**
* @throws Exception
*/
public function reorderTestsInSuite(Test $suite, int $order, bool $resolveDependencies, int $orderDefects): void
public function reorderTestsInSuite(Test $suite, int $order, bool $resolveDependencies, int $orderDefects, bool $isRootTestSuite = true): void
{
$allowedOrders = [
self::ORDER_DEFAULT,
Expand All @@ -98,9 +108,13 @@ public function reorderTestsInSuite(Test $suite, int $order, bool $resolveDepend
);
}

if ($isRootTestSuite) {
$this->originalExecutionOrder = $this->calculateTestExecutionOrder($suite);
}

if ($suite instanceof TestSuite) {
foreach ($suite as $_suite) {
$this->reorderTestsInSuite($_suite, $order, $resolveDependencies, $orderDefects);
$this->reorderTestsInSuite($_suite, $order, $resolveDependencies, $orderDefects, false);
}

if ($orderDefects === self::ORDER_DEFECTS_FIRST) {
Expand All @@ -109,6 +123,20 @@ public function reorderTestsInSuite(Test $suite, int $order, bool $resolveDepend

$this->sort($suite, $order, $resolveDependencies, $orderDefects);
}

if ($isRootTestSuite) {
$this->executionOrder = $this->calculateTestExecutionOrder($suite);
}
}

public function getOriginalExecutionOrder(): array
{
return $this->originalExecutionOrder;
}

public function getExecutionOrder(): array
{
return $this->executionOrder;
}

private function sort(TestSuite $suite, int $order, bool $resolveDependencies, int $orderDefects): void
Expand Down Expand Up @@ -312,4 +340,23 @@ function ($name) use ($testClass) {

return $names;
}

private function calculateTestExecutionOrder(Test $suite): array
{
$tests = [];

if ($suite instanceof TestSuite) {
foreach ($suite->tests() as $test) {
if (!($test instanceof TestSuite)) {
$tests[] = $this->getNormalizedTestName($test);
}
}

foreach ($suite as $_suite) {
$tests = \array_merge($tests, $this->calculateTestExecutionOrder($_suite));
}
}

return $tests;
}
}
Loading

0 comments on commit d634df7

Please sign in to comment.