Skip to content

Commit

Permalink
Use more performant way of building --filter option, not escaping n…
Browse files Browse the repository at this point in the history
…on-unique test cases and using `foreach`
  • Loading branch information
maks-rafalko committed Jul 19, 2021
1 parent b351122 commit a4c5b0f
Showing 1 changed file with 22 additions and 8 deletions.
Expand Up @@ -35,17 +35,16 @@

namespace Infection\TestFramework\PhpUnit\CommandLine;

use function array_key_exists;
use function array_map;
use function array_merge;
use function array_unique;
use function count;
use function escapeshellcmd;
use function explode;
use function implode;
use Infection\AbstractTestFramework\Coverage\TestLocation;
use Infection\TestFramework\CommandLineArgumentsAndOptionsBuilder;
use function ltrim;
use function Safe\sprintf;
use function rtrim;

/**
* @internal
Expand All @@ -71,18 +70,33 @@ public function buildForInitialTestsRun(string $configPath, string $extraOptions
return $options;
}

/**
* @param TestLocation[] $tests
*/
public function buildForMutant(string $configPath, string $extraOptions, array $tests): array
{
$options = $this->buildForInitialTestsRun($configPath, $extraOptions);

if (count($tests) > 0) {
$escapedTests = array_map(
static fn (TestLocation $testLocation): string => escapeshellcmd($testLocation->getMethod()),
$tests
);
$filterString = '/';
$usedTestCases = [];

foreach ($tests as $testLocation) {
$testCaseString = $testLocation->getMethod();

if (array_key_exists($testCaseString, $usedTestCases)) {
continue;
}

$usedTestCases[$testCaseString] = true;

$filterString .= escapeshellcmd($testCaseString) . '|';
}

$filterString = rtrim($filterString, '|') . '/';

$options[] = '--filter';
$options[] = sprintf('/%s/', implode('|', array_unique($escapedTests)));
$options[] = $filterString;
}

return $options;
Expand Down

0 comments on commit a4c5b0f

Please sign in to comment.