diff --git a/docker/Dockerfile b/docker/Dockerfile index ff10bcb2..f7d37ee8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,7 +2,7 @@ ARG PHP=8.1 FROM php:${PHP}-cli-alpine RUN apk update \ - && apk add zip libzip-dev icu-dev + && apk add zip libzip-dev icu-dev git RUN docker-php-ext-configure zip RUN docker-php-ext-install zip diff --git a/src/Contracts/Plugins/HandlesArguments.php b/src/Contracts/Plugins/HandlesArguments.php index 72c9d722..50c3a30e 100644 --- a/src/Contracts/Plugins/HandlesArguments.php +++ b/src/Contracts/Plugins/HandlesArguments.php @@ -10,7 +10,7 @@ namespace Pest\Contracts\Plugins; interface HandlesArguments { /** - * Adds arguments before of the Test Suite execution. + * Adds arguments before the Test Suite execution. * * @param array $arguments * @return array diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index 8f53801a..1e610c5f 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -14,15 +14,16 @@ use Pest\Support\Arr; use Pest\Support\Container; use Pest\TestSuite; use function Pest\version; +use Stringable; use Symfony\Component\Console\Application; -use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\ArgvInput; -use Symfony\Component\Console\Output\OutputInterface; final class Parallel implements HandlesArguments { use HandleArguments; + private const GLOBAL_PREFIX = 'PEST_PARALLEL_GLOBAL_'; + private const HANDLERS = [ Parallel\Handlers\Parallel::class, Parallel\Handlers\Pest::class, @@ -59,6 +60,33 @@ final class Parallel implements HandlesArguments return ((int) $argvValue) === 1; } + /** + * Sets a global value that can be accessed by the parent process and all workers. + */ + public static function setGlobal(string $key, string|int|bool|Stringable $value): void + { + $data = ['value' => $value instanceof Stringable ? $value->__toString() : $value]; + + $_ENV[self::GLOBAL_PREFIX.$key] = json_encode($data, JSON_THROW_ON_ERROR); + } + + /** + * Returns the given global value if one has been set. + */ + public static function getGlobal(string $key): string|int|bool|null + { + $placesToCheck = [$_SERVER, $_ENV]; + + foreach ($placesToCheck as $location) { + if (array_key_exists(self::GLOBAL_PREFIX.$key, $location)) { + // @phpstan-ignore-next-line + return json_decode((string) $location[self::GLOBAL_PREFIX.$key], true, 512, JSON_THROW_ON_ERROR)['value'] ?? null; + } + } + + return null; + } + /** * {@inheritdoc} */ @@ -86,12 +114,6 @@ final class Parallel implements HandlesArguments */ private function runTestSuiteInParallel(array $arguments): int { - if (! class_exists(ParaTestCommand::class)) { - $this->askUserToInstallParatest(); - - return Command::FAILURE; - } - $handlers = array_filter( array_map(fn ($handler): object|string => Container::getInstance()->get($handler), self::HANDLERS), fn ($handler): bool => $handler instanceof HandlesArguments, @@ -128,20 +150,6 @@ final class Parallel implements HandlesArguments ); } - /** - * Outputs a message to the user asking them to install ParaTest as a dev dependency. - */ - private function askUserToInstallParatest(): void - { - /** @var OutputInterface $output */ - $output = Container::getInstance()->get(OutputInterface::class); - - $output->writeln([ - 'Pest Parallel requires ParaTest to run.', - 'Please run composer require --dev brianium/paratest.', - ]); - } - /** * Builds an instance of the Paratest command. */