Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/Process/EnvCommandCreator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $source
* @param array<mixed> $globalArray
* @param array<mixed> $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;
}
}
53 changes: 50 additions & 3 deletions tests/Process/EnvCommandCreatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Liuggio\Fastest\Process;

use Liuggio\Fastest\Trait\ServerDataTrait;
use PHPUnit\Framework\TestCase;

class EnvCommandCreatorTest extends TestCase
{
use ServerDataTrait;

/**
* @test
*/
Expand Down Expand Up @@ -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(
[
Expand All @@ -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);
}
}
9 changes: 6 additions & 3 deletions tests/Process/ProcessFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Liuggio\Fastest\Process;

use Liuggio\Fastest\Trait\ServerDataTrait;
use PHPUnit\Framework\TestCase;

class ProcessFactoryTest extends TestCase
{
use ServerDataTrait;

/**
* @test
*/
Expand All @@ -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()
);
}
Expand Down Expand Up @@ -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()
);
}
Expand All @@ -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()
);
}
Expand Down
29 changes: 29 additions & 0 deletions tests/Trait/ServerDataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Liuggio\Fastest\Trait;

use Liuggio\Fastest\Process\EnvCommandCreator;

trait ServerDataTrait
{
/**
* @return array<scalar>
*/
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;
}
}
Loading