diff --git a/src/Process/EnvCommandCreator.php b/src/Process/EnvCommandCreator.php index a897006..1e80372 100644 --- a/src/Process/EnvCommandCreator.php +++ b/src/Process/EnvCommandCreator.php @@ -43,10 +43,51 @@ public function execute( continue; } - $res[$key] = $value; - $mergedArgs[strtolower($key)] = true; + if (!is_array($value)) { + $formattedValue = self::formatValueForEnv($value); + if (null !== $formattedValue) { + $res[$key] = $value; + $mergedArgs[strtolower($key)] = true; + } + } else { + self::decomposeRecursively($key, $value, $res, $mergedArgs); + } } return $res; } + + /** + * @param array $source + * @param array $globalArray + * @param array $mergedArgs + */ + public static function decomposeRecursively(string $mainKey, array $source, array &$globalArray, array &$mergedArgs): void + { + foreach ($source as $childKey => $value) { + $newKey = $mainKey . '_' . $childKey; + if (is_array($value)) { + self::decomposeRecursively($newKey, $value, $globalArray, $mergedArgs); + } elseif (!array_key_exists($newKey, $globalArray)) { + $formattedValue = self::formatValueForEnv($value); + if (null !== $formattedValue) { + $globalArray[$newKey] = $formattedValue; + $mergedArgs[strtolower($newKey)] = true; + } + } + } + } + + public static function formatValueForEnv(mixed $value): ?string + { + if (is_scalar($value)) { + return (string) $value; + } + + if (is_object($value) && method_exists($value, '__toString')) { + return (string) $value; + } + + return null; + } } diff --git a/tests/Process/EnvCommandCreatorTest.php b/tests/Process/EnvCommandCreatorTest.php index f19317b..e1e6a36 100644 --- a/tests/Process/EnvCommandCreatorTest.php +++ b/tests/Process/EnvCommandCreatorTest.php @@ -4,10 +4,13 @@ namespace Liuggio\Fastest\Process; +use Liuggio\Fastest\Trait\ServerDataTrait; use PHPUnit\Framework\TestCase; class EnvCommandCreatorTest extends TestCase { + use ServerDataTrait; + /** * @test */ @@ -93,8 +96,7 @@ public function shouldMergeEnvVariables(): void $res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true); - unset($_ENV['A_VARIABLE']); - unset($_ENV['another_variable']); + unset($_ENV['A_VARIABLE'], $_ENV['another_variable']); $this->assertEquals( [ @@ -104,8 +106,53 @@ public function shouldMergeEnvVariables(): void EnvCommandCreator::ENV_TEST_ARGUMENT => 'exec_test_command', EnvCommandCreator::ENV_TEST_INCREMENTAL_NUMBER => 4, EnvCommandCreator::ENV_TEST_IS_FIRST_ON_CHANNEL => 1, - ] + $_SERVER + $_ENV, + ] + $this->getServerWithDecomposeArgv() + $_ENV, $res ); } + + /** + * @test + */ + public function shouldMergeArrayEnvVariables(): void + { + $_SERVER['my_custom_array'] = [ + 'sub_array' => [ + 'another_array' => 'value_env', + 'another_array_2' => 'value_env_2', + ], + 'sub_array2' => 'value_env', + ]; + + $envCommandCreator = new EnvCommandCreator(); + + $res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true); + + $this->assertArrayHasKey('my_custom_array_sub_array_another_array', $res); + $this->assertEquals('value_env', $res['my_custom_array_sub_array_another_array']); + $this->assertArrayHasKey('my_custom_array_sub_array_another_array_2', $res); + $this->assertEquals('value_env_2', $res['my_custom_array_sub_array_another_array_2']); + $this->assertArrayHasKey('my_custom_array_sub_array2', $res); + $this->assertEquals('value_env', $res['my_custom_array_sub_array2']); + $this->assertArrayNotHasKey('my_custom_array', $res); + } + + /** + * @test + */ + public function shouldntMergeArrayEnvVariables(): void + { + $_SERVER['my_custom_array'] = 'my_important_value_env'; + $_SERVER['my_custom'] = [ + 'array' => 'my_useless_value_env' + ]; + + $envCommandCreator = new EnvCommandCreator(); + + $res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true); + + $this->assertArrayHasKey('my_custom_array', $res); + $this->assertEquals('my_important_value_env', $res['my_custom_array']); + $this->assertArrayNotHasKey('my_custom', $res); + } } \ No newline at end of file diff --git a/tests/Process/ProcessFactoryTest.php b/tests/Process/ProcessFactoryTest.php index e82f7a4..9d75347 100644 --- a/tests/Process/ProcessFactoryTest.php +++ b/tests/Process/ProcessFactoryTest.php @@ -2,10 +2,13 @@ namespace Liuggio\Fastest\Process; +use Liuggio\Fastest\Trait\ServerDataTrait; use PHPUnit\Framework\TestCase; class ProcessFactoryTest extends TestCase { + use ServerDataTrait; + /** * @test */ @@ -23,7 +26,7 @@ public function shouldCreateACommandUsingParallelTests(): void 'ENV_TEST_ARGUMENT' => 'fileA', 'ENV_TEST_INC_NUMBER' => 10, 'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1, - ] + $_SERVER + $_ENV, + ] + $this->getServerWithDecomposeArgv() + $_ENV, $process->getenv() ); } @@ -84,7 +87,7 @@ public function shouldCreateACommandUsingParallelTestsWithOptions(): void 'ENV_TEST_ARGUMENT' => 'fileA', 'ENV_TEST_INC_NUMBER' => 12, 'ENV_TEST_IS_FIRST_ON_CHANNEL' => 0, - ] + $_SERVER + $_ENV, + ] + $this->getServerWithDecomposeArgv() + $_ENV, $process->getenv() ); } @@ -106,7 +109,7 @@ public function shouldReplaceThePlaceholder(): void 'ENV_TEST_ARGUMENT' => 'fileA', 'ENV_TEST_INC_NUMBER' => 13, 'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1, - ] + $_SERVER + $_ENV, + ] + $this->getServerWithDecomposeArgv() + $_ENV, $process->getenv() ); } diff --git a/tests/Trait/ServerDataTrait.php b/tests/Trait/ServerDataTrait.php new file mode 100644 index 0000000..ab946be --- /dev/null +++ b/tests/Trait/ServerDataTrait.php @@ -0,0 +1,29 @@ + + */ + protected function getServerWithDecomposeArgv(): array + { + $server = $_SERVER; + + if (isset($server['argv']) && is_array($server['argv'])) { + $mergedArgs = array_fill_keys( + array_map('strtolower', array_keys($server)), + true + ); + EnvCommandCreator::decomposeRecursively('argv', $server['argv'], $server, $mergedArgs); + unset($server['argv']); + } + + return $server; + } +} \ No newline at end of file