Skip to content

Commit 3c53c11

Browse files
authored
Fix "Array to string conversion" for Symfony 7.4+ compatibility (#195)
* fix: Symfony 7.4 compatibility I don't know if this convention about naming is the right one, I mean, maybe it would have been better to not carry around the prefixes and all the things but I got your point so, as long as I don't have a specific and concrete reason to ask you for another change, I merge this and tag a new version soon. Thanks for the contribution.
1 parent f9a0e4d commit 3c53c11

File tree

4 files changed

+128
-8
lines changed

4 files changed

+128
-8
lines changed

src/Process/EnvCommandCreator.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,51 @@ public function execute(
4343
continue;
4444
}
4545

46-
$res[$key] = $value;
47-
$mergedArgs[strtolower($key)] = true;
46+
if (!is_array($value)) {
47+
$formattedValue = self::formatValueForEnv($value);
48+
if (null !== $formattedValue) {
49+
$res[$key] = $value;
50+
$mergedArgs[strtolower($key)] = true;
51+
}
52+
} else {
53+
self::decomposeRecursively($key, $value, $res, $mergedArgs);
54+
}
4855
}
4956

5057
return $res;
5158
}
59+
60+
/**
61+
* @param array<mixed> $source
62+
* @param array<mixed> $globalArray
63+
* @param array<mixed> $mergedArgs
64+
*/
65+
public static function decomposeRecursively(string $mainKey, array $source, array &$globalArray, array &$mergedArgs): void
66+
{
67+
foreach ($source as $childKey => $value) {
68+
$newKey = $mainKey . '_' . $childKey;
69+
if (is_array($value)) {
70+
self::decomposeRecursively($newKey, $value, $globalArray, $mergedArgs);
71+
} elseif (!array_key_exists($newKey, $globalArray)) {
72+
$formattedValue = self::formatValueForEnv($value);
73+
if (null !== $formattedValue) {
74+
$globalArray[$newKey] = $formattedValue;
75+
$mergedArgs[strtolower($newKey)] = true;
76+
}
77+
}
78+
}
79+
}
80+
81+
public static function formatValueForEnv(mixed $value): ?string
82+
{
83+
if (is_scalar($value)) {
84+
return (string) $value;
85+
}
86+
87+
if (is_object($value) && method_exists($value, '__toString')) {
88+
return (string) $value;
89+
}
90+
91+
return null;
92+
}
5293
}

tests/Process/EnvCommandCreatorTest.php

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Liuggio\Fastest\Process;
66

7+
use Liuggio\Fastest\Trait\ServerDataTrait;
78
use PHPUnit\Framework\TestCase;
89

910
class EnvCommandCreatorTest extends TestCase
1011
{
12+
use ServerDataTrait;
13+
1114
/**
1215
* @test
1316
*/
@@ -93,8 +96,7 @@ public function shouldMergeEnvVariables(): void
9396

9497
$res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true);
9598

96-
unset($_ENV['A_VARIABLE']);
97-
unset($_ENV['another_variable']);
99+
unset($_ENV['A_VARIABLE'], $_ENV['another_variable']);
98100

99101
$this->assertEquals(
100102
[
@@ -104,8 +106,53 @@ public function shouldMergeEnvVariables(): void
104106
EnvCommandCreator::ENV_TEST_ARGUMENT => 'exec_test_command',
105107
EnvCommandCreator::ENV_TEST_INCREMENTAL_NUMBER => 4,
106108
EnvCommandCreator::ENV_TEST_IS_FIRST_ON_CHANNEL => 1,
107-
] + $_SERVER + $_ENV,
109+
] + $this->getServerWithDecomposeArgv() + $_ENV,
108110
$res
109111
);
110112
}
113+
114+
/**
115+
* @test
116+
*/
117+
public function shouldMergeArrayEnvVariables(): void
118+
{
119+
$_SERVER['my_custom_array'] = [
120+
'sub_array' => [
121+
'another_array' => 'value_env',
122+
'another_array_2' => 'value_env_2',
123+
],
124+
'sub_array2' => 'value_env',
125+
];
126+
127+
$envCommandCreator = new EnvCommandCreator();
128+
129+
$res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true);
130+
131+
$this->assertArrayHasKey('my_custom_array_sub_array_another_array', $res);
132+
$this->assertEquals('value_env', $res['my_custom_array_sub_array_another_array']);
133+
$this->assertArrayHasKey('my_custom_array_sub_array_another_array_2', $res);
134+
$this->assertEquals('value_env_2', $res['my_custom_array_sub_array_another_array_2']);
135+
$this->assertArrayHasKey('my_custom_array_sub_array2', $res);
136+
$this->assertEquals('value_env', $res['my_custom_array_sub_array2']);
137+
$this->assertArrayNotHasKey('my_custom_array', $res);
138+
}
139+
140+
/**
141+
* @test
142+
*/
143+
public function shouldntMergeArrayEnvVariables(): void
144+
{
145+
$_SERVER['my_custom_array'] = 'my_important_value_env';
146+
$_SERVER['my_custom'] = [
147+
'array' => 'my_useless_value_env'
148+
];
149+
150+
$envCommandCreator = new EnvCommandCreator();
151+
152+
$res = $envCommandCreator->execute(1, 5, 'exec_test_command', 4, true);
153+
154+
$this->assertArrayHasKey('my_custom_array', $res);
155+
$this->assertEquals('my_important_value_env', $res['my_custom_array']);
156+
$this->assertArrayNotHasKey('my_custom', $res);
157+
}
111158
}

tests/Process/ProcessFactoryTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
namespace Liuggio\Fastest\Process;
44

5+
use Liuggio\Fastest\Trait\ServerDataTrait;
56
use PHPUnit\Framework\TestCase;
67

78
class ProcessFactoryTest extends TestCase
89
{
10+
use ServerDataTrait;
11+
912
/**
1013
* @test
1114
*/
@@ -23,7 +26,7 @@ public function shouldCreateACommandUsingParallelTests(): void
2326
'ENV_TEST_ARGUMENT' => 'fileA',
2427
'ENV_TEST_INC_NUMBER' => 10,
2528
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
26-
] + $_SERVER + $_ENV,
29+
] + $this->getServerWithDecomposeArgv() + $_ENV,
2730
$process->getenv()
2831
);
2932
}
@@ -84,7 +87,7 @@ public function shouldCreateACommandUsingParallelTestsWithOptions(): void
8487
'ENV_TEST_ARGUMENT' => 'fileA',
8588
'ENV_TEST_INC_NUMBER' => 12,
8689
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 0,
87-
] + $_SERVER + $_ENV,
90+
] + $this->getServerWithDecomposeArgv() + $_ENV,
8891
$process->getenv()
8992
);
9093
}
@@ -106,7 +109,7 @@ public function shouldReplaceThePlaceholder(): void
106109
'ENV_TEST_ARGUMENT' => 'fileA',
107110
'ENV_TEST_INC_NUMBER' => 13,
108111
'ENV_TEST_IS_FIRST_ON_CHANNEL' => 1,
109-
] + $_SERVER + $_ENV,
112+
] + $this->getServerWithDecomposeArgv() + $_ENV,
110113
$process->getenv()
111114
);
112115
}

tests/Trait/ServerDataTrait.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Liuggio\Fastest\Trait;
6+
7+
use Liuggio\Fastest\Process\EnvCommandCreator;
8+
9+
trait ServerDataTrait
10+
{
11+
/**
12+
* @return array<scalar>
13+
*/
14+
protected function getServerWithDecomposeArgv(): array
15+
{
16+
$server = $_SERVER;
17+
18+
if (isset($server['argv']) && is_array($server['argv'])) {
19+
$mergedArgs = array_fill_keys(
20+
array_map('strtolower', array_keys($server)),
21+
true
22+
);
23+
EnvCommandCreator::decomposeRecursively('argv', $server['argv'], $server, $mergedArgs);
24+
unset($server['argv']);
25+
}
26+
27+
return $server;
28+
}
29+
}

0 commit comments

Comments
 (0)