Skip to content

Commit 55e707b

Browse files
committed
fix(options): Correct host and port handling in DSN normalization
- Enable host and port configuration for DSN address - Update normalization logic to derive addr from host and port only if addr is not set - Ensure consistency in test snapshots to reflect changes in DSN format
1 parent cda80f6 commit 55e707b

File tree

5 files changed

+39
-37
lines changed

5 files changed

+39
-37
lines changed

src/Concerns/HasOptions.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,8 @@ private function normalizeDsn(string $name, array $dsn): ?string
561561
// 'user' => '',
562562
// 'password' => '',
563563
// 'addr' => '127.0.0.1:3306',
564-
// 'host' => '127.0.0.1',
565-
// 'port' => 3306,
564+
'host' => '127.0.0.1',
565+
'port' => 3306,
566566
// 'schema' => 'dbname',
567567
'disable' => false,
568568
];
@@ -571,7 +571,7 @@ private function normalizeDsn(string $name, array $dsn): ?string
571571
return null;
572572
}
573573

574-
if (!isset($dsn['addr']) && isset($dsn['host'], $dsn['port'])) {
574+
if (!isset($dsn['addr'])) {
575575
$dsn['addr'] = "{$dsn['host']}:{$dsn['port']}";
576576
}
577577

tests/Concerns/HasOptionsTest.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,33 +86,34 @@
8686
})->group(__DIR__, __FILE__)->throws(InvalidOptionException::class, 'object');
8787

8888
it('can normalize options', function (): void {
89-
$this->assertMatchesJsonSnapshot(
90-
(fn (): array => $this->getNormalizedOptions())->call(Soar::make(
91-
array_merge(require __DIR__.'/../../examples/soar-options.php', [
92-
'-explain-format' => new class {
93-
public function __invoke(): string
94-
{
95-
return 'traditional';
96-
}
97-
},
98-
'-explain-type' => new class {
99-
public function __toString(): string
100-
{
101-
return 'extended';
102-
}
103-
},
104-
'-test-dsn' => [
105-
'user' => 'you_user',
106-
'password' => 'you_password',
107-
// 'addr' => '127.0.0.1:3306',
108-
'host' => 'you_host',
109-
'port' => 'you_port',
110-
'schema' => 'you_dbname',
111-
'disable' => false,
112-
],
113-
])
114-
))
115-
);
89+
$normalizedOptions = (fn (): array => $this->getNormalizedOptions())->call(Soar::make(
90+
array_merge(require __DIR__.'/../../examples/soar-options.php', [
91+
'-explain-format' => new class {
92+
public function __invoke(): string
93+
{
94+
return 'traditional';
95+
}
96+
},
97+
'-explain-type' => new class {
98+
public function __toString(): string
99+
{
100+
return 'extended';
101+
}
102+
},
103+
'-test-dsn' => [
104+
'user' => 'you_user',
105+
'password' => 'you_password',
106+
// 'addr' => '127.0.0.1:3306',
107+
'host' => 'you_host',
108+
'port' => 'you_port',
109+
'schema' => 'you_dbname',
110+
'disable' => false,
111+
],
112+
])
113+
));
114+
115+
expect($normalizedOptions)->each->toBeString();
116+
$this->assertMatchesJsonSnapshot($normalizedOptions);
116117
})->group(__DIR__, __FILE__);
117118

118119
it('will throw InvalidOptionException when normalize invalid dsn', function (): void {

tests/Concerns/WithRunableTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
namespace Guanguans\SoarPHPTests\Concerns;
2424

2525
use Guanguans\SoarPHP\Soar;
26+
use Guanguans\SoarPHP\Support\OS;
2627
use Symfony\Component\Process\Exception\ProcessFailedException;
2728
use Symfony\Component\Process\Process;
2829

@@ -45,7 +46,7 @@
4546
(new class extends Soar {
4647
protected function shouldApplySudoPassword(): bool
4748
{
48-
return true;
49+
return OS::isUnix();
4950
}
5051
})->setSudoPassword('foo')->help();
5152
})

tests/Pest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ function fake(string $locale = Factory::DEFAULT_LOCALE): Generator
105105
return Factory::create($locale);
106106
}
107107

108+
function running_in_github_action(): bool
109+
{
110+
return getenv('GITHUB_ACTIONS') === 'true';
111+
}
112+
108113
function soar_options(): array
109114
{
110115
static $options;
@@ -121,8 +126,3 @@ function soar_options_example(): array
121126

122127
return $options ??= require __DIR__.'/../examples/soar-options-example.php';
123128
}
124-
125-
function running_in_github_action(): bool
126-
{
127-
return getenv('GITHUB_ACTIONS') === 'true';
128-
}

tests/__snapshots__/HasOptionsTest__it_can_normalize_options__1.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"-config": "-config",
3-
"-test-dsn": "-test-dsn=you_user:you_password@you_host:you_port\/you_dbname?host=you_host&port=you_port",
3+
"-test-dsn": "-test-dsn=you_user:you_password@you_host:you_port\/you_dbname",
44
"-online-dsn": "-online-dsn=:********@127.0.0.1:3306\/information_schema?net=tcp&charset=utf8&collation=utf8mb4_general_ci&loc=UTC&tls=&server-public-key=&max-allowed-packet=4194304&params[charset]=utf8&timeout=3s&read-timeout=0s&write-timeout=0s&allow-native-passwords=1&allow-old-passwords=0",
55
"-allow-online-as-test": "-allow-online-as-test=false",
66
"-blacklist": "-blacklist=",

0 commit comments

Comments
 (0)