Skip to content

Commit d7ed634

Browse files
committed
refactor: ConfigToml: remove strict non-mode;
ConfigClient: rename `loadFromFile()` to `loadFromToml()`
1 parent 161e218 commit d7ed634

File tree

4 files changed

+23
-52
lines changed

4 files changed

+23
-52
lines changed

src/Common/EnvConfig/Client/ConfigToml.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ final class ConfigToml
5656
*/
5757
public function __construct(
5858
string $toml,
59-
private readonly bool $strict = true,
6059
) {
6160
\class_exists(Toml::class) or throw new TomlParserNotFoundException();
6261
$data = Toml::parseToArray($toml);
@@ -68,19 +67,21 @@ public function __construct(
6867
*
6968
* @param bool $condition The condition to assert.
7069
* @param non-empty-string $message The exception message if the assertion fails.
71-
* @return bool Returns true if the condition is false.
70+
* @return false Returns false if the assertion passes
71+
* @throws \InvalidArgumentException If the assertion fails.
7272
*/
7373
private function notAssert(bool $condition, string $message): bool
7474
{
75-
$this->strict and !$condition and throw new \InvalidArgumentException($message);
76-
return !$condition;
75+
$condition or throw new \InvalidArgumentException($message);
76+
return false;
7777
}
7878

7979
/**
8080
* Parse profiles from the given configuration array.
8181
*
8282
* @param mixed $profile The profile configuration data.
8383
* @return array<non-empty-string, ConfigProfile>
84+
* @throws \InvalidArgumentException If the configuration is invalid.
8485
*/
8586
private function parseProfiles(mixed $profile): array
8687
{

src/Common/EnvConfig/ConfigClient.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
*
2828
* @link https://github.com/temporalio/proposals/blob/master/all-sdk/external-client-configuration.md
2929
*
30-
* @internal
30+
* @internal Experimental
3131
* @psalm-internal Temporal\Common\EnvConfig
3232
*/
3333
final class ConfigClient
@@ -82,7 +82,7 @@ public static function load(
8282
$profile = null;
8383
$profiles = [];
8484
if ($configFile !== null) {
85-
$profiles = self::loadFromFile($configFile, $strict)->profiles;
85+
$profiles = self::loadFromToml($configFile, $strict)->profiles;
8686
$profile = $profiles[$profileNameLower] ?? null;
8787
}
8888

@@ -122,11 +122,10 @@ public static function loadFromEnv(EnvProvider $envProvider = new SystemEnvProvi
122122
* Load all profiles from a TOML configuration file.
123123
*
124124
* @param non-empty-string $source File path to TOML config or TOML content string
125-
* @param bool $strict Whether to use strict TOML parsing
126125
*
127126
* @throws InvalidConfigException If the file cannot be read or TOML is invalid
128127
*/
129-
public static function loadFromFile(string $source, bool $strict = true): self
128+
public static function loadFromToml(string $source): self
130129
{
131130
try {
132131
// Determine if source is a file path or TOML content
@@ -136,7 +135,7 @@ public static function loadFromFile(string $source, bool $strict = true): self
136135

137136
$toml === false and throw new InvalidConfigException("Failed to read configuration file: {$source}");
138137

139-
$config = new ConfigToml($toml, $strict);
138+
$config = new ConfigToml($toml);
140139
return new self(self::normalizeProfileNames($config->profiles));
141140
} catch (ConfigException $e) {
142141
throw $e;

tests/Unit/Common/EnvConfig/Client/ConfigTomlTest.php

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\Attributes\DataProvider;
99
use PHPUnit\Framework\TestCase;
10-
use Temporal\Common\EnvConfig\Client\ConfigCodec;
1110
use Temporal\Common\EnvConfig\Client\ConfigProfile;
1211
use Temporal\Common\EnvConfig\Client\ConfigTls;
1312
use Temporal\Common\EnvConfig\Client\ConfigToml;
@@ -383,7 +382,7 @@ public function testConstructorInStrictModeThrowsExceptionForDuplicateCertPath()
383382
$this->expectExceptionMessage('Cannot specify both `server_ca_cert_path` and `server_ca_cert_data`.');
384383

385384
// Act
386-
new ConfigToml($toml, strict: true);
385+
new ConfigToml($toml);
387386
}
388387

389388
public function testConstructorInStrictModeThrowsExceptionForDuplicateClientKeyPath(): void
@@ -402,7 +401,7 @@ public function testConstructorInStrictModeThrowsExceptionForDuplicateClientKeyP
402401
$this->expectExceptionMessage('Cannot specify both `client_key_path` and `client_key_data`.');
403402

404403
// Act
405-
new ConfigToml($toml, strict: true);
404+
new ConfigToml($toml);
406405
}
407406

408407
public function testConstructorInStrictModeThrowsExceptionForDuplicateClientCertPath(): void
@@ -421,7 +420,7 @@ public function testConstructorInStrictModeThrowsExceptionForDuplicateClientCert
421420
$this->expectExceptionMessage('Cannot specify both `client_cert_path` and `client_cert_data`.');
422421

423422
// Act
424-
new ConfigToml($toml, strict: true);
423+
new ConfigToml($toml);
425424
}
426425

427426
public function testConstructorInStrictModeThrowsExceptionForMissingClientKey(): void
@@ -439,7 +438,7 @@ public function testConstructorInStrictModeThrowsExceptionForMissingClientKey():
439438
$this->expectExceptionMessage('Both `client_key_*` and `client_cert_*` must be specified for mTLS.');
440439

441440
// Act
442-
new ConfigToml($toml, strict: true);
441+
new ConfigToml($toml);
443442
}
444443

445444
public function testConstructorInStrictModeThrowsExceptionForMissingClientCert(): void
@@ -457,35 +456,7 @@ public function testConstructorInStrictModeThrowsExceptionForMissingClientCert()
457456
$this->expectExceptionMessage('Both `client_key_*` and `client_cert_*` must be specified for mTLS.');
458457

459458
// Act
460-
new ConfigToml($toml, strict: true);
461-
}
462-
463-
public function testConstructorInNonStrictModeDoesNotThrowExceptionForDuplicates(): void
464-
{
465-
// Arrange
466-
$toml = <<<'TOML'
467-
[profile.valid]
468-
address = "localhost:1234"
469-
470-
[profile.with_duplicates]
471-
address = "localhost:5678"
472-
[profile.with_duplicates.tls]
473-
server_ca_cert_path = "path/to/ca.pem"
474-
server_ca_cert_data = "ca-pem-data"
475-
TOML;
476-
477-
// Act
478-
$config = new ConfigToml($toml, strict: false);
479-
480-
// Assert - in non-strict mode, both profiles are created despite validation issues
481-
self::assertCount(2, $config->profiles);
482-
self::assertArrayHasKey('valid', $config->profiles);
483-
self::assertArrayHasKey('with_duplicates', $config->profiles);
484-
485-
// The profile with duplicates uses the first value (cert_path takes precedence over cert_data)
486-
$invalidProfile = $config->profiles['with_duplicates'];
487-
self::assertInstanceOf(ConfigTls::class, $invalidProfile->tlsConfig);
488-
self::assertSame('path/to/ca.pem', $invalidProfile->tlsConfig->rootCerts);
459+
new ConfigToml($toml);
489460
}
490461

491462
#[DataProvider('provideInvalidProfileStructures')]
@@ -498,7 +469,7 @@ public function testConstructorInStrictModeThrowsExceptionForInvalidStructure(
498469
$this->expectExceptionMessage($expectedMessage);
499470

500471
// Act
501-
new ConfigToml($toml, strict: true);
472+
new ConfigToml($toml);
502473
}
503474

504475
#[DataProvider('provideComplexConfigurations')]

tests/Unit/Common/EnvConfig/ConfigClientTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testLoadFromFileWithValidTomlContent(): void
4646
TOML;
4747

4848
// Act
49-
$config = ConfigClient::loadFromFile($toml);
49+
$config = ConfigClient::loadFromToml($toml);
5050

5151
// Assert
5252
self::assertTrue($config->hasProfile('default'));
@@ -74,7 +74,7 @@ public function testLoadFromFileThrowsExceptionForInvalidToml(): void
7474
$this->expectExceptionMessage('Invalid TOML configuration');
7575

7676
// Act
77-
ConfigClient::loadFromFile($invalidToml);
77+
ConfigClient::loadFromToml($invalidToml);
7878
}
7979

8080
public function testLoadFromEnvWithSystemEnvProvider(): void
@@ -242,7 +242,7 @@ public function testGetProfileIsCaseInsensitive(string $storeName, string $acces
242242
{
243243
// Arrange
244244
$toml = "[profile.{$storeName}]\naddress = \"127.0.0.1:7233\"";
245-
$config = ConfigClient::loadFromFile($toml);
245+
$config = ConfigClient::loadFromToml($toml);
246246

247247
// Act
248248
$profile = $config->getProfile($accessName);
@@ -256,7 +256,7 @@ public function testHasProfileIsCaseInsensitive(string $storeName, string $acces
256256
{
257257
// Arrange
258258
$toml = "[profile.{$storeName}]\naddress = \"127.0.0.1:7233\"";
259-
$config = ConfigClient::loadFromFile($toml);
259+
$config = ConfigClient::loadFromToml($toml);
260260

261261
// Act & Assert
262262
self::assertTrue($config->hasProfile($accessName));
@@ -269,7 +269,7 @@ public function testGetProfileThrowsExceptionWhenNotFound(): void
269269
[profile.default]
270270
address = "127.0.0.1:7233"
271271
TOML;
272-
$config = ConfigClient::loadFromFile($toml);
272+
$config = ConfigClient::loadFromToml($toml);
273273

274274
// Assert (before Act for exceptions)
275275
$this->expectException(ProfileNotFoundException::class);
@@ -286,7 +286,7 @@ public function testHasProfileReturnsFalseWhenNotFound(): void
286286
[profile.default]
287287
address = "127.0.0.1:7233"
288288
TOML;
289-
$config = ConfigClient::loadFromFile($toml);
289+
$config = ConfigClient::loadFromToml($toml);
290290

291291
// Act & Assert
292292
self::assertFalse($config->hasProfile('nonexistent'));
@@ -308,7 +308,7 @@ public function testDuplicateCaseInsensitiveProfileNamesThrowException(): void
308308
$this->expectExceptionMessage("Duplicate profile name (case-insensitive): 'Default' conflicts with existing 'default'");
309309

310310
// Act
311-
ConfigClient::loadFromFile($toml);
311+
ConfigClient::loadFromToml($toml);
312312
}
313313

314314
public function testToServiceClientWithoutTls(): void
@@ -456,7 +456,7 @@ public function testProfilesPropertyIsReadonly(): void
456456
[profile.default]
457457
address = "127.0.0.1:7233"
458458
TOML;
459-
$config = ConfigClient::loadFromFile($toml);
459+
$config = ConfigClient::loadFromToml($toml);
460460

461461
// Assert
462462
self::assertIsArray($config->profiles);

0 commit comments

Comments
 (0)