Skip to content

Instantly share code, notes, and snippets.

@mfn
Last active March 11, 2021 15:18
Show Gist options
  • Save mfn/256636242cbe8a51252ce28181a6b074 to your computer and use it in GitHub Desktop.
Save mfn/256636242cbe8a51252ce28181a6b074 to your computer and use it in GitHub Desktop.
Feed the output of `phpunit --list-tests-xml <file.xml>` and slice it up into smaller chunks
#!/usr/bin/env php
<?php declare(strict_types = 1);
// Source: https://gist.github.com/mfn/256636242cbe8a51252ce28181a6b074
$command = array_shift($argv);
if (2 !== count($argv)) {
echo "Please provide a XML file and a slice like `$command tests.xml 2/4`\n";
exit(1);
}
if (!preg_match('/^([0-9]+)\/([0-9]+)$/', $argv[1], $parts)) {
echo "Please provide the slice in form like `$command tests.xml 2/4`\n";
exit(1);
}
$tests = [];
$inputXml = simplexml_load_file($argv[0]);
foreach ($inputXml->testCaseClass as $class) {
foreach ($class->children() as $method) {
$tests[] = [$class, $method];
}
}
foreach ($inputXml->phptFile as $phptFile) {
$tests[] = $phptFile;
}
// Source: \Wizaplace\PHPUnit\Slicer\TestSuiteSlicer::slice
$slices = $parts[2];
$current = $parts[1] - 1; // 0 indexed. Slice 1 is in reality slice 0
$total = count($tests);
$testsPerSlice = (int) ceil($total / $slices);
$offset = $testsPerSlice * $current;
$slicedTests = array_slice($tests, $offset, $testsPerSlice);
$outputXml = new XMLWriter();
$outputXml->openMemory();
$outputXml->setIndent(true);
$outputXml->startDocument('1.0', 'utf-8');
$outputXml->startElement('tests');
foreach ($slicedTests as $slicedTest) {
if (is_array($slicedTest)) {
[$class, $method] = $slicedTest;
$outputXml->startElement('testCaseClass');
$outputXml->writeAttribute('name', $class['name']->__toString());
$outputXml->startElement('testCaseMethod');
$outputXml->writeAttribute('name', $method['name']->__toString());
if ($method['groups']) {
$outputXml->writeAttribute('groups', $method['groups']->__toString());
}
if ($method['dataSet']) {
$outputXml->writeAttribute('dataSet', $method['dataSet']->__toString());
}
$outputXml->endElement();
$outputXml->endElement();
} else {
$outputXml->startElement('phptFile');
$outputXml->writeAttribute('path', $slicedTest['path']->__toString());
$outputXml->endElement();
}
}
$outputXml->endElement();
$outputXml->endDocument();
echo $outputXml->outputMemory();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment