Skip to content

Instantly share code, notes, and snippets.

@markstory
Created March 18, 2015 17:34
Show Gist options
  • Save markstory/8d90dfd1332fe1612159 to your computer and use it in GitHub Desktop.
Save markstory/8d90dfd1332fe1612159 to your computer and use it in GitHub Desktop.
<?php
namespace Test;
use PHPUnit_Framework_TestSuite;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RegexIterator;
class TestSuite extends PHPUnit_Framework_TestSuite
{
/**
* Returns a suite containing all tests that will be run in this segment.
*
* @return void
*/
public static function suite()
{
$suite = new self('All Tests');
$segment = getenv('TEST_SEGMENT');
$segments = getenv('TEST_SEGMENTS');
$directory = new RecursiveDirectoryIterator(__DIR__, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\Test.php$/i', RegexIterator::GET_MATCH);
$files = [];
foreach ($regex as $name => $file) {
if (strpos('./FreshTest', $name) !== false) {
continue;
}
$files[] = $name;
}
if (!$segment || !$segments) {
foreach ($files as $name) {
$suite->addTestFile($file);
}
return $suite;
}
echo "Running segment $segment of $segments\n";
$chunked = array_chunk($files, ceil(count($files) / $segments));
foreach ($chunked[$segment - 1] as $name) {
$suite->addTestFile($name);
}
return $suite;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment