chore(cleanup): Tidy-up and tweaks of Pest Parallel integration.

This commit is contained in:
Luke Downing
2023-02-13 09:43:49 +00:00
parent b6fb81e506
commit 5ca4c5bca9
4 changed files with 27 additions and 26 deletions

View File

@ -31,22 +31,7 @@ final class Parallel implements HandlesArguments
]; ];
/** /**
* If the * If this code is running in a worker process rather than the main process.
*/
public static function isCommand(): bool
{
// get binary name
Arr::get($_SERVER, 'argv.0');
$argvValue = Arr::get($_ENV, 'PARATEST');
assert(is_string($argvValue) || is_int($argvValue) || is_null($argvValue));
return ((int) $argvValue) === 1;
}
/**
* If the
*/ */
public static function isWorker(): bool public static function isWorker(): bool
{ {
@ -57,23 +42,28 @@ final class Parallel implements HandlesArguments
return ((int) $argvValue) === 1; return ((int) $argvValue) === 1;
} }
/**
* {@inheritdoc}
*/
public function handleArguments(array $arguments): array public function handleArguments(array $arguments): array
{ {
if ($this->argumentsContainParallelFlags($arguments)) { if ($this->argumentsContainParallelOptions($arguments)) {
exit($this->runTestSuiteInParallel($arguments)); exit($this->runTestSuiteInParallel($arguments));
} }
if (self::isWorker()) { if (self::isWorker()) {
return $this->runWorkersHandlers($arguments); return $this->runWorkerHandlers($arguments);
} }
return $arguments; return $arguments;
} }
/** /**
* Whether the given command line arguments indicate that the test suite should be run in parallel.
*
* @param array<int, string> $arguments * @param array<int, string> $arguments
*/ */
private function argumentsContainParallelFlags(array $arguments): bool private function argumentsContainParallelOptions(array $arguments): bool
{ {
if ($this->hasArgument('--parallel', $arguments)) { if ($this->hasArgument('--parallel', $arguments)) {
return true; return true;
@ -83,6 +73,8 @@ final class Parallel implements HandlesArguments
} }
/** /**
* Runs the test suite in parallel. This method will exit the process upon completion.
*
* @param array<int, string> $arguments * @param array<int, string> $arguments
* *
* @throws JsonException * @throws JsonException
@ -114,10 +106,12 @@ final class Parallel implements HandlesArguments
} }
/** /**
* Runs any handlers that have been registered to handle worker arguments, and returns the modified arguments.
*
* @param array<int, string> $arguments * @param array<int, string> $arguments
* @return array<int, string> * @return array<int, string>
*/ */
private function runWorkersHandlers(array $arguments): array private function runWorkerHandlers(array $arguments): array
{ {
$handlers = array_filter( $handlers = array_filter(
array_map(fn ($handler): object|string => Container::getInstance()->get($handler), self::HANDLERS), array_map(fn ($handler): object|string => Container::getInstance()->get($handler), self::HANDLERS),
@ -131,6 +125,9 @@ final class Parallel implements HandlesArguments
); );
} }
/**
* Outputs a message to the user asking them to install ParaTest as a dev dependency.
*/
private function askUserToInstallParatest(): void private function askUserToInstallParatest(): void
{ {
/** @var OutputInterface $output */ /** @var OutputInterface $output */
@ -142,6 +139,9 @@ final class Parallel implements HandlesArguments
]); ]);
} }
/**
* Builds an instance of the Paratest command.
*/
private function paratestCommand(): Application private function paratestCommand(): Application
{ {
/** @var non-empty-string $rootPath */ /** @var non-empty-string $rootPath */

View File

@ -21,6 +21,9 @@ final class Laravel implements HandlesArguments
{ {
use HandleArguments; use HandleArguments;
/**
* {@inheritdoc}
*/
public function handleArguments(array $arguments): array public function handleArguments(array $arguments): array
{ {
return self::whenUsingLaravel($arguments, function (array $arguments): array { return self::whenUsingLaravel($arguments, function (array $arguments): array {
@ -43,10 +46,8 @@ final class Laravel implements HandlesArguments
{ {
$isLaravelApplication = InstalledVersions::isInstalled('laravel/framework', false); $isLaravelApplication = InstalledVersions::isInstalled('laravel/framework', false);
$isLaravelPackage = class_exists(\Orchestra\Testbench\TestCase::class); $isLaravelPackage = class_exists(\Orchestra\Testbench\TestCase::class);
if ($isLaravelApplication) {
return $closure($arguments); if ($isLaravelApplication && ! $isLaravelPackage) {
}
if ($isLaravelPackage) {
return $closure($arguments); return $closure($arguments);
} }

View File

@ -25,7 +25,7 @@ final class Parallel implements HandlesArguments
]; ];
/** /**
* Handles the arguments, removing the ones that are not needed, and adding the "runner" argument. * Handles the arguments, removing the ones that are not needed, and adds the "runner" argument.
*/ */
public function handleArguments(array $arguments): array public function handleArguments(array $arguments): array
{ {

View File

@ -33,7 +33,7 @@ final class AfterEachRepository
} }
/** /**
* Gets a after each closure by the given filename. * Gets an after each closure by the given filename.
*/ */
public function get(string $filename): Closure public function get(string $filename): Closure
{ {