From f3fb3f228ea467a6b7043451a6c4094f6e92b8ce Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 29 Mar 2024 09:25:19 +0100 Subject: [PATCH] added fork-if-possible annotation support --- .../Attributes/RunClassInSeparateProcess.php | 11 +++++++++++ .../Attributes/RunInSeparateProcess.php | 11 +++++++++++ .../Attributes/RunTestsInSeparateProcesses.php | 11 +++++++++++ src/Metadata/Metadata.php | 12 ++++++------ src/Metadata/Parser/AttributeParser.php | 18 +++++++++++++++--- src/Metadata/RunClassInSeparateProcess.php | 17 +++++++++++++++++ src/Metadata/RunInSeparateProcess.php | 17 +++++++++++++++++ src/Metadata/RunTestsInSeparateProcesses.php | 17 +++++++++++++++++ 8 files changed, 105 insertions(+), 9 deletions(-) diff --git a/src/Framework/Attributes/RunClassInSeparateProcess.php b/src/Framework/Attributes/RunClassInSeparateProcess.php index 1ccd3bc6fb2..e8918e5494c 100644 --- a/src/Framework/Attributes/RunClassInSeparateProcess.php +++ b/src/Framework/Attributes/RunClassInSeparateProcess.php @@ -19,4 +19,15 @@ #[Attribute(Attribute::TARGET_CLASS)] final readonly class RunClassInSeparateProcess { + private ?bool $forkIfPossible; + + public function __construct(bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } } diff --git a/src/Framework/Attributes/RunInSeparateProcess.php b/src/Framework/Attributes/RunInSeparateProcess.php index 838e267e2e4..d10cbba3efd 100644 --- a/src/Framework/Attributes/RunInSeparateProcess.php +++ b/src/Framework/Attributes/RunInSeparateProcess.php @@ -19,4 +19,15 @@ #[Attribute(Attribute::TARGET_METHOD)] final readonly class RunInSeparateProcess { + private ?bool $forkIfPossible; + + public function __construct(?bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } } diff --git a/src/Framework/Attributes/RunTestsInSeparateProcesses.php b/src/Framework/Attributes/RunTestsInSeparateProcesses.php index 19fea88239a..ed804fa2a70 100644 --- a/src/Framework/Attributes/RunTestsInSeparateProcesses.php +++ b/src/Framework/Attributes/RunTestsInSeparateProcesses.php @@ -19,4 +19,15 @@ #[Attribute(Attribute::TARGET_CLASS)] final readonly class RunTestsInSeparateProcesses { + private ?bool $forkIfPossible; + + public function __construct(?bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } } diff --git a/src/Metadata/Metadata.php b/src/Metadata/Metadata.php index df587b6ad4d..f42e6f7526c 100644 --- a/src/Metadata/Metadata.php +++ b/src/Metadata/Metadata.php @@ -382,19 +382,19 @@ public static function requiresSettingOnMethod(string $setting, string $value): return new RequiresSetting(self::METHOD_LEVEL, $setting, $value); } - public static function runClassInSeparateProcess(): RunClassInSeparateProcess + public static function runClassInSeparateProcess(?bool $forkIfPossible = null): RunClassInSeparateProcess { - return new RunClassInSeparateProcess(self::CLASS_LEVEL); + return new RunClassInSeparateProcess(self::CLASS_LEVEL, $forkIfPossible); } - public static function runTestsInSeparateProcesses(): RunTestsInSeparateProcesses + public static function runTestsInSeparateProcesses(?bool $forkIfPossible = null): RunTestsInSeparateProcesses { - return new RunTestsInSeparateProcesses(self::CLASS_LEVEL); + return new RunTestsInSeparateProcesses(self::CLASS_LEVEL, $forkIfPossible); } - public static function runInSeparateProcess(): RunInSeparateProcess + public static function runInSeparateProcess(?bool $forkIfPossible = null): RunInSeparateProcess { - return new RunInSeparateProcess(self::METHOD_LEVEL); + return new RunInSeparateProcess(self::METHOD_LEVEL, $forkIfPossible); } public static function test(): Test diff --git a/src/Metadata/Parser/AttributeParser.php b/src/Metadata/Parser/AttributeParser.php index d53f04419d4..b9eed3360ff 100644 --- a/src/Metadata/Parser/AttributeParser.php +++ b/src/Metadata/Parser/AttributeParser.php @@ -288,12 +288,20 @@ public function forClass(string $className): MetadataCollection break; case RunClassInSeparateProcess::class: - $result[] = Metadata::runClassInSeparateProcess(); + assert($attributeInstance instanceof RunClassInSeparateProcess); + + $result[] = Metadata::runClassInSeparateProcess( + $attributeInstance->forkIfPossible(), + ); break; case RunTestsInSeparateProcesses::class: - $result[] = Metadata::runTestsInSeparateProcesses(); + assert($attributeInstance instanceof RunTestsInSeparateProcesses); + + $result[] = Metadata::runTestsInSeparateProcesses( + $attributeInstance->forkIfPossible(), + ); break; @@ -622,7 +630,11 @@ public function forMethod(string $className, string $methodName): MetadataCollec break; case RunInSeparateProcess::class: - $result[] = Metadata::runInSeparateProcess(); + assert($attributeInstance instanceof RunInSeparateProcess); + + $result[] = Metadata::runInSeparateProcess( + $attributeInstance->forkIfPossible(), + ); break; diff --git a/src/Metadata/RunClassInSeparateProcess.php b/src/Metadata/RunClassInSeparateProcess.php index 907a45d4524..a5b6aa7f829 100644 --- a/src/Metadata/RunClassInSeparateProcess.php +++ b/src/Metadata/RunClassInSeparateProcess.php @@ -16,6 +16,23 @@ */ final readonly class RunClassInSeparateProcess extends Metadata { + private ?bool $forkIfPossible; + + /** + * @psalm-param 0|1 $level + */ + protected function __construct(int $level, ?bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + + parent::__construct($level); + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } + /** * @psalm-assert-if-true RunClassInSeparateProcess $this */ diff --git a/src/Metadata/RunInSeparateProcess.php b/src/Metadata/RunInSeparateProcess.php index 07febae09de..9b5cc237b01 100644 --- a/src/Metadata/RunInSeparateProcess.php +++ b/src/Metadata/RunInSeparateProcess.php @@ -16,6 +16,23 @@ */ final readonly class RunInSeparateProcess extends Metadata { + private ?bool $forkIfPossible; + + /** + * @psalm-param 0|1 $level + */ + protected function __construct(int $level, ?bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + + parent::__construct($level); + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } + /** * @psalm-assert-if-true RunInSeparateProcess $this */ diff --git a/src/Metadata/RunTestsInSeparateProcesses.php b/src/Metadata/RunTestsInSeparateProcesses.php index b71233d3dbe..5a8275d293f 100644 --- a/src/Metadata/RunTestsInSeparateProcesses.php +++ b/src/Metadata/RunTestsInSeparateProcesses.php @@ -16,6 +16,23 @@ */ final readonly class RunTestsInSeparateProcesses extends Metadata { + private ?bool $forkIfPossible; + + /** + * @psalm-param 0|1 $level + */ + protected function __construct(int $level, ?bool $forkIfPossible = null) + { + $this->forkIfPossible = $forkIfPossible; + + parent::__construct($level); + } + + public function forkIfPossible(): ?bool + { + return $this->forkIfPossible; + } + /** * @psalm-assert-if-true RunTestsInSeparateProcesses $this */