Skip to content
Closed
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
7 changes: 1 addition & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
parameters:
ignoreErrors:
-
message: '#^Method PhpUnitSplitter\\TestMapper\:\:getMap\(\) should return array\<string, array\<string, float\>\> but returns array\<int\|string, array\<string, float\|list\<array\<string, float\|string\>\>\|string\>\>\.$#'
identifier: return.type
count: 1
path: src/TestMapper.php
ignoreErrors: []
6 changes: 4 additions & 2 deletions src/SplitterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* A symfony command for splitting PHPUnit tests.
*
* @phpstan-import-type TestMap from TestMapper
*/
class SplitterCommand extends Command {

Expand Down Expand Up @@ -102,14 +104,14 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/**
* Splits the tests map into the given number of splits.
*
* @param array<string,array<string,float>> $map
* @param TestMap $map
* The map of tests.
* @param int $splits
* The number of splits.
* @param int $index
* The index of the current split.
*
* @return array<string,array<string,float>>
* @return TestMap
* The split map.
*/
private function split(array $map, int $splits, int $index): array {
Expand Down
25 changes: 16 additions & 9 deletions src/TestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

/**
* Generates a map of test methods with their file name and execution time.
*
* @phpstan-type TestCaseTime array{testCase: string, time: float}
* @phpstan-type TestClassEntry array{className: string, time: float, testCases: list<TestCaseTime>}
* @phpstan-type TestMap array<array-key, TestClassEntry>
*/
final class TestMapper {

Expand All @@ -31,7 +35,7 @@ public function __construct(
/**
* Returns a map of test methods with their file name and execution time.
*
* @return array<string,array<string,float>>
* @return TestMap
* The map of test files and execution times.
*/
public function getMap(): array {
Expand All @@ -51,10 +55,8 @@ public function getMap(): array {
if (\str_starts_with($filename, $this->prefix)) {
$filename = \substr($filename, \strlen($this->prefix));
}
$map[$filename] = [
'className' => $className,
'time' => 0.0,
];
$totalTime = 0.0;
$testCaseTimes = [];
$testCases = $class->xpath('testCaseMethod');
foreach ($testCases as $testCase) {
$shortName = (string) $testCase->attributes()['name'];
Expand All @@ -66,23 +68,28 @@ public function getMap(): array {
$shortName .= "@$dataSet";
}
$time = $this->resultCache->getTime($cacheKey);
$map[$filename]['testCases'][] = [
$testCaseTimes[] = [
'testCase' => $shortName,
'time' => $time,
];
$map[$filename]['time'] += $time;
$totalTime += $time;
}
$map[$filename] = [
'className' => $className,
'time' => $totalTime,
'testCases' => $testCaseTimes,
];
}
return $map;
}

/**
* Sorts the map by execution time.
*
* @param array<string,array<string,float>> $map
* @param TestMap $map
* The map to sort.
*
* @return array<string,array<string,float>>
* @return TestMap
* The sorted map.
*/
public function sortMap(array $map): array {
Expand Down
Loading