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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ phpunit-splitter 4 2 --tests-file=tests.xml --results-file=.phpunit.result.cache
Pass the results to PHPUnit:

```bash
./phpunit-splitter 2 0 --tests-file=tests/fixtures/tests.xml --results-file=tests/fixtures/.phpunit.result.cache | while IFS= read -r line; do
filepath=$(echo "$line" | awk '{print $NF}')
testname=$(echo "$line" | awk '{$NF=""; print $0}')
./vendor/bin/phpunit --filter="$testname" $filepath
./phpunit-splitter 2 0 --tests-file=tests/fixtures/tests.xml --results-file=tests/fixtures/.phpunit.result.cache | xargs ./vendor/bin/phpunit
done
```

Output the test list as JSON:

```bash
./phpunit-splitter 2 0 --json --tests-file=tests/fixtures/tests.xml --results-file=tests/fixtures/.phpunit.result.cache
```
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"php": "^8.1",
"ext-simplexml": "*",
"phpunit/phpunit": "^9.6",
"previousnext/phpunit-finder": "^2.0",
"symfony/console": "^6.3"
},
"autoload": {
Expand Down
19 changes: 14 additions & 5 deletions src/SplitterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ class SplitterCommand extends Command {
protected function configure(): void {
$this->addArgument('splits', InputArgument::OPTIONAL, "The number of splits", 1);
$this->addArgument('index', InputArgument::OPTIONAL, "The index of the current split", 0);
$this->addOption('tests-file', 't', InputOption::VALUE_REQUIRED, "The xml file listing all tests.", getcwd() . './tests.xml');
$this->addOption('tests-file', 't', InputOption::VALUE_REQUIRED, "The xml file listing all tests.", getcwd() . '/tests.xml');
$this->addOption('results-file', 'f', InputOption::VALUE_REQUIRED, "The results cache file.", getcwd() . '/.phpunit.result.cache', );
$this->addOption('bootstrap-file', 'b', InputOption::VALUE_OPTIONAL, "The tests bootstrap file.", getcwd() . '/tests/bootstrap.php');
$this->addOption('prefix', 'p', InputOption::VALUE_OPTIONAL, "The prefix to remove from the file names.", getcwd() . '/');
$this->addOption('json', 'j', InputOption::VALUE_NONE, "Output the result as json.");
}

/**
Expand All @@ -39,15 +41,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$index = (int) $input->getArgument('index');
$testsFile = $input->getOption('tests-file');
$resultsFile = $input->getOption('results-file');
$prefix = $input->getOption('prefix');
$json = $input->getOption('json');

$mapper = new TestMapper($testsFile, $resultsFile);
$mapper = new TestMapper($testsFile, $resultsFile, $prefix);
$map = $mapper->sortMap($mapper->getMap());

foreach ($this->split($map, $splits, $index) as $testName => $test) {
$output->writeln(\addslashes($testName) . ' ' . $test['path']);
$split = $this->split($map, $splits, $index);
if ($json) {
$output->writeln(\json_encode($split));
return Command::SUCCESS;
}
foreach ($split as $testPath => $test) {
$output->writeln($testPath);
}

return 0;
return Command::SUCCESS;
}

private function split(array $map, int $splits, int $index): array {
Expand Down
29 changes: 20 additions & 9 deletions src/TestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ final class TestMapper {
private \SimpleXMLElement|FALSE $testsXml;
private TestResultCache $resultCache;

public function __construct(string $testListFilePath, string $testResultFilePath) {
private string $prefix;

public function __construct(string $testListFilePath, string $testResultFilePath, string $prefix) {
$this->testsXml = \simplexml_load_file($testListFilePath);
$this->resultCache = new DefaultTestResultCache($testResultFilePath);
$this->prefix = $prefix;
}

public function getMap(): array {
Expand All @@ -28,23 +31,31 @@ public function getMap(): array {
$className = (string) $class->attributes()['name'];
try {
$reflection = new \ReflectionClass($className);
}
catch (\ReflectionException $e) {
} catch (\ReflectionException $e) {
// Couldn't find the class.
continue;
}
$filename = $reflection->getFileName();
if (\str_starts_with($filename, $this->prefix)) {
$filename = \substr($filename, \strlen($this->prefix));
}
$map[$filename] = [
'className' => $className,
'time' => 0.0,
];
$testCases = $class->xpath('testCaseMethod');
foreach ($testCases as $testCase) {
$testName = $reflection->getShortName() . '::' . $testCase->attributes()['name'];
$shortName = (string) $testCase->attributes()['name'];
$fullName = $reflection->getName() . '::' . $shortName;
$dataSet = $testCase->attributes()['dataSet'] ?? NULL;
$cacheKey = $fullName;
if ($dataSet !== NULL) {
$testName .= " with data set $dataSet";
$cacheKey .= " with data set $dataSet";
$shortName .= "@$dataSet";
}
$map[$testName] = [
'path' => $filename,
'time' => $this->resultCache->getTime($testName),
];
$time = $this->resultCache->getTime($cacheKey);
$map[$filename][$shortName] = ['time' => $time];
$map[$filename]['time'] += $time;
}
}
return $map;
Expand Down
38 changes: 7 additions & 31 deletions tests/PhpUnitSplitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,20 @@ class PhpUnitSplitterTest extends TestCase {

public function testSplitter(): void {
$fixtures = dirname(__DIR__) . '/tests/fixtures';
$mapper = new TestMapper("$fixtures/tests.xml", "$fixtures/.phpunit.result.cache");
$mapper = new TestMapper("$fixtures/tests.xml", "$fixtures/.phpunit.result.cache", \getcwd() . '/');
$map = $mapper->getMap();

$this->assertSame([
'PhpUnitSplitter\\Tests\\fixtures\\Test\\FastTestsTest::testOne',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\FastTestsTest::testTwo',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\FastTestsTest::testThree',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\FastTestsTest::testFour',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\FastTestsTest::testFive',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\ProviderTest::testProvider with data set "one"',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\ProviderTest::testProvider with data set "two"',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\ProviderTest::testProvider with data set "three"',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\ProviderTest::testProvider with data set "four"',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\ProviderTest::testProvider with data set "five"',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\SlowTestsTest::testOne',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\SlowTestsTest::testTwo',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\SlowTestsTest::testThree',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\SlowTestsTest::testFour',
'PhpUnitSplitter\\Tests\\fixtures\\Test\\SlowTestsTest::testFive',
'fixtures/Test/FastTestsTest.php',
'fixtures/Test/ProviderTest.php',
'fixtures/Test/SlowTestsTest.php',
], array_keys($map));

$sorted = $mapper->sortMap($map);
$this->assertSame([
'PhpUnitSplitter\Tests\fixtures\Test\FastTestsTest::testOne',
'PhpUnitSplitter\Tests\fixtures\Test\FastTestsTest::testTwo',
'PhpUnitSplitter\Tests\fixtures\Test\FastTestsTest::testThree',
'PhpUnitSplitter\Tests\fixtures\Test\FastTestsTest::testFour',
'PhpUnitSplitter\Tests\fixtures\Test\FastTestsTest::testFive',
'PhpUnitSplitter\Tests\fixtures\Test\SlowTestsTest::testOne',
'PhpUnitSplitter\Tests\fixtures\Test\ProviderTest::testProvider with data set "one"',
'PhpUnitSplitter\Tests\fixtures\Test\SlowTestsTest::testTwo',
'PhpUnitSplitter\Tests\fixtures\Test\ProviderTest::testProvider with data set "three"',
'PhpUnitSplitter\Tests\fixtures\Test\SlowTestsTest::testThree',
'PhpUnitSplitter\Tests\fixtures\Test\ProviderTest::testProvider with data set "four"',
'PhpUnitSplitter\Tests\fixtures\Test\SlowTestsTest::testFour',
'PhpUnitSplitter\Tests\fixtures\Test\ProviderTest::testProvider with data set "two"',
'PhpUnitSplitter\Tests\fixtures\Test\SlowTestsTest::testFive',
'PhpUnitSplitter\Tests\fixtures\Test\ProviderTest::testProvider with data set "five"',
'fixtures/Test/FastTestsTest.php',
'fixtures/Test/SlowTestsTest.php',
'fixtures/Test/ProviderTest.php',
], array_keys($sorted));

}
Expand Down