From 4c769fac66542c3084a712820c7de8d9f501f96c Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 10:58:48 +0000 Subject: [PATCH 1/7] feat(parallel): Adds support for plugins to filter parallel arguments --- docker/Dockerfile | 2 +- src/Contracts/Plugins/HandlesArguments.php | 2 +- src/Plugins/Parallel.php | 22 ---------------------- 3 files changed, 2 insertions(+), 24 deletions(-) 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..f4992d40 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -15,9 +15,7 @@ use Pest\Support\Container; use Pest\TestSuite; use function Pest\version; 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 { @@ -86,12 +84,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 +120,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. */ From 7433cc5565a42928071686db9400c12d9492e59e Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:09:53 +0000 Subject: [PATCH 2/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index f4992d40..dbbaa9b9 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -14,6 +14,7 @@ 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\Input\ArgvInput; @@ -21,6 +22,8 @@ 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, @@ -57,6 +60,26 @@ final class Parallel implements HandlesArguments return ((int) $argvValue) === 1; } + public static function setGlobal(string $key, string|int|bool|Stringable $value): void + { + $data = ['value' => $value instanceof Stringable ? $value->__toString() : $value]; + + $_SERVER[self::GLOBAL_PREFIX.$key] = json_encode($data); + } + + 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)) { + return json_decode($location[self::GLOBAL_PREFIX.$key])['value'] ?? null; + } + } + + return null; + } + /** * {@inheritdoc} */ From a4833bbfe46033eafa703ec9482888fbf6b827de Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:13:39 +0000 Subject: [PATCH 3/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index dbbaa9b9..a6980b23 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -73,7 +73,7 @@ final class Parallel implements HandlesArguments foreach ($placesToCheck as $location) { if (array_key_exists(self::GLOBAL_PREFIX.$key, $location)) { - return json_decode($location[self::GLOBAL_PREFIX.$key])['value'] ?? null; + return json_decode($location[self::GLOBAL_PREFIX.$key], true)['value'] ?? null; } } From 1915ad368a5f1927d6b3eb3f8bcbc28e4d5b9ea1 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:17:11 +0000 Subject: [PATCH 4/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index a6980b23..7b2d1b9f 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -64,7 +64,7 @@ final class Parallel implements HandlesArguments { $data = ['value' => $value instanceof Stringable ? $value->__toString() : $value]; - $_SERVER[self::GLOBAL_PREFIX.$key] = json_encode($data); + $_ENV[self::GLOBAL_PREFIX.$key] = json_encode($data); } public static function getGlobal(string $key): string|int|bool|null From 0539d2ba62386db045fa6bd2666d17cfec5919af Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:25:33 +0000 Subject: [PATCH 5/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index 7b2d1b9f..e93c34a4 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -60,6 +60,9 @@ 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]; @@ -67,6 +70,9 @@ final class Parallel implements HandlesArguments $_ENV[self::GLOBAL_PREFIX.$key] = json_encode($data); } + /** + * Returns the given global value if one has been set. + */ public static function getGlobal(string $key): string|int|bool|null { $placesToCheck = [$_SERVER, $_ENV]; From ff82596158a630bb660dfb738945b03f3afa9f81 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:27:26 +0000 Subject: [PATCH 6/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index e93c34a4..baecfe1a 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -79,6 +79,7 @@ final class Parallel implements HandlesArguments foreach ($placesToCheck as $location) { if (array_key_exists(self::GLOBAL_PREFIX.$key, $location)) { + // @phpstan-ignore-next-line return json_decode($location[self::GLOBAL_PREFIX.$key], true)['value'] ?? null; } } From 1c673fcff9f934213f2419fc8d8537bd5a5209c7 Mon Sep 17 00:00:00 2001 From: Luke Downing Date: Wed, 22 Mar 2023 11:30:53 +0000 Subject: [PATCH 7/7] feat(parallel): Adds support for plugins to filter parallel arguments --- src/Plugins/Parallel.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Plugins/Parallel.php b/src/Plugins/Parallel.php index baecfe1a..1e610c5f 100644 --- a/src/Plugins/Parallel.php +++ b/src/Plugins/Parallel.php @@ -67,7 +67,7 @@ final class Parallel implements HandlesArguments { $data = ['value' => $value instanceof Stringable ? $value->__toString() : $value]; - $_ENV[self::GLOBAL_PREFIX.$key] = json_encode($data); + $_ENV[self::GLOBAL_PREFIX.$key] = json_encode($data, JSON_THROW_ON_ERROR); } /** @@ -80,7 +80,7 @@ final class Parallel implements HandlesArguments foreach ($placesToCheck as $location) { if (array_key_exists(self::GLOBAL_PREFIX.$key, $location)) { // @phpstan-ignore-next-line - return json_decode($location[self::GLOBAL_PREFIX.$key], true)['value'] ?? null; + return json_decode((string) $location[self::GLOBAL_PREFIX.$key], true, 512, JSON_THROW_ON_ERROR)['value'] ?? null; } }