mirror of
https://github.com/pestphp/pest.git
synced 2026-03-09 01:07:23 +01:00
WIP
This commit is contained in:
committed by
Nuno Maduro
parent
d69f61c8d3
commit
48309931ef
74
src/Plugins/Parallel.php
Normal file
74
src/Plugins/Parallel.php
Normal file
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Pest\Plugins;
|
||||
|
||||
use ParaTest\ParaTestCommand;
|
||||
use Pest\Contracts\Plugins\HandlesArguments;
|
||||
use Pest\Plugins\Concerns\HandleArguments;
|
||||
use Pest\Support\Arr;
|
||||
use Pest\Support\Container;
|
||||
use Pest\TestSuite;
|
||||
use Symfony\Component\Console\Input\ArgvInput;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
|
||||
final class Parallel implements HandlesArguments
|
||||
{
|
||||
use HandleArguments;
|
||||
|
||||
private array $handlers = [
|
||||
\Pest\Plugins\Parallel\Handlers\Parallel::class,
|
||||
\Pest\Plugins\Parallel\Handlers\Laravel::class,
|
||||
];
|
||||
|
||||
public function handleArguments(array $arguments): array
|
||||
{
|
||||
if ($this->argumentsContainParallelFlags($arguments)) {
|
||||
$exitCode = $this->runTestSuiteInParallel($arguments);
|
||||
exit($exitCode);
|
||||
}
|
||||
|
||||
$this->markTestSuiteAsParallelSubProcessIfRequired();
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
private function argumentsContainParallelFlags(array $arguments): bool
|
||||
{
|
||||
return $this->hasArgument('--parallel', $arguments)
|
||||
|| $this->hasArgument('-p', $arguments);
|
||||
}
|
||||
|
||||
private function runTestSuiteInParallel(array $arguments): int
|
||||
{
|
||||
if (! class_exists(ParaTestCommand::class)) {
|
||||
$this->askUserToInstallParatest();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
$filteredArguments = array_reduce(
|
||||
$this->handlers,
|
||||
fn($arguments, $handler) => (new $handler())->handle($arguments),
|
||||
$arguments
|
||||
);
|
||||
|
||||
$testSuite = TestSuite::getInstance();
|
||||
|
||||
return ParaTestCommand::applicationFactory($testSuite->rootPath)->run(new ArgvInput($filteredArguments));
|
||||
}
|
||||
|
||||
private function markTestSuiteAsParallelSubProcessIfRequired(): void
|
||||
{
|
||||
if ((int) Arr::get($_SERVER, 'PARATEST') === 1) {
|
||||
$_SERVER['PEST_PARALLEL'] = 1;
|
||||
}
|
||||
}
|
||||
|
||||
private function askUserToInstallParatest(): void
|
||||
{
|
||||
Container::getInstance()->get(OutputInterface::class)->writeln([
|
||||
'<fg=red>Parallel support requires ParaTest, which is not installed.</>',
|
||||
'Please run <fg=yellow>composer require --dev brianium/paratest</> to install ParaTest.',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user