Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 44 additions & 6 deletions overrides/Runner/TestSuiteSorter.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ final class TestSuiteSorter

public const int ORDER_DEFECTS_FIRST = 3;

public const int ORDER_DURATION = 4;
public const int ORDER_DURATION_ASCENDING = 4;

public const int ORDER_SIZE = 5;
public const int ORDER_SIZE_ASCENDING = 5;

public const int ORDER_DURATION_DESCENDING = 6;

public const int ORDER_SIZE_DESCENDING = 7;

/**
* @var non-empty-array<non-empty-string, positive-int>
Expand Down Expand Up @@ -113,8 +117,10 @@ public function reorderTestsInSuite(Test $suite, int $order, bool $resolveDepend
self::ORDER_DEFAULT,
self::ORDER_REVERSED,
self::ORDER_RANDOMIZED,
self::ORDER_DURATION,
self::ORDER_SIZE,
self::ORDER_DURATION_ASCENDING,
self::ORDER_SIZE_ASCENDING,
self::ORDER_DURATION_DESCENDING,
self::ORDER_SIZE_DESCENDING,
];

if (! in_array($order, $allowedOrders, true)) {
Expand Down Expand Up @@ -157,10 +163,14 @@ private function sort(TestSuite $suite, int $order, bool $resolveDependencies, i
$suite->setTests($this->reverse($suite->tests()));
} elseif ($order === self::ORDER_RANDOMIZED) {
$suite->setTests($this->randomize($suite->tests()));
} elseif ($order === self::ORDER_DURATION) {
} elseif ($order === self::ORDER_DURATION_ASCENDING) {
$suite->setTests($this->sortByDuration($suite->tests()));
} elseif ($order === self::ORDER_SIZE) {
} elseif ($order === self::ORDER_DURATION_DESCENDING) {
$suite->setTests($this->sortByDurationDescending($suite->tests()));
} elseif ($order === self::ORDER_SIZE_ASCENDING) {
$suite->setTests($this->sortBySize($suite->tests()));
} elseif ($order === self::ORDER_SIZE_DESCENDING) {
$suite->setTests($this->sortBySizeDescending($suite->tests()));
}

if ($orderDefects === self::ORDER_DEFECTS_FIRST) {
Expand Down Expand Up @@ -242,6 +252,20 @@ private function sortByDuration(array $tests): array
return $tests;
}

/**
* @param list<Test> $tests
* @return list<Test>
*/
private function sortByDurationDescending(array $tests): array
{
usort(
$tests,
fn (Test $left, Test $right) => $this->cmpDuration($right, $left),
);

return $tests;
}

/**
* @param list<Test> $tests
* @return list<Test>
Expand All @@ -256,6 +280,20 @@ private function sortBySize(array $tests): array
return $tests;
}

/**
* @param list<Test> $tests
* @return list<Test>
*/
private function sortBySizeDescending(array $tests): array
{
usort(
$tests,
fn (Test $left, Test $right) => $this->cmpSize($right, $left),
);

return $tests;
}

/**
* Comparator callback function to sort tests for "reach failure as fast as possible".
*
Expand Down
29 changes: 29 additions & 0 deletions tests/Features/OrderBy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use Symfony\Component\Process\Process;

it('runs with every supported order', function (string $order): void {
$process = new Process(
['php', 'bin/pest', 'tests/Fixtures/DirectoryWithTests/ExampleTest.php', '--order-by='.$order],
dirname(__DIR__, 2),
['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'],
);

$process->run();

expect($process->getExitCode())->toBe(0)
->and(removeAnsiEscapeSequences($process->getOutput()))->toContain('1 passed');
})->with([
'default',
'defects',
'duration',
'duration-ascending',
'duration-descending',
'random',
'reverse',
'size',
'size-ascending',
'size-descending',
]);