From 541d5950f2c56350e79d5e1066c7c233ab068df5 Mon Sep 17 00:00:00 2001 From: Dominik Chrastecky Date: Tue, 16 Apr 2024 12:54:41 +0200 Subject: [PATCH 1/3] Feat: Allow basic parameters to be late bound --- src/Configuration/UnleashConfiguration.php | 15 +++---- .../UnleashConfigurationTest.php | 39 +++++++++++++++++++ 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/src/Configuration/UnleashConfiguration.php b/src/Configuration/UnleashConfiguration.php index 6104ca0d..f978715f 100755 --- a/src/Configuration/UnleashConfiguration.php +++ b/src/Configuration/UnleashConfiguration.php @@ -5,6 +5,7 @@ use JetBrains\PhpStorm\Pure; use LogicException; use Psr\SimpleCache\CacheInterface; +use Stringable; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Unleash\Client\Bootstrap\BootstrapHandler; @@ -22,9 +23,9 @@ final class UnleashConfiguration * @param array $headers */ public function __construct( - private string $url, - private string $appName, - private string $instanceId, + private string|Stringable $url, + private string|Stringable $appName, + private string|Stringable $instanceId, private ?CacheInterface $cache = null, private int $ttl = 30, private int $metricsInterval = 30_000, @@ -71,7 +72,7 @@ public function getUrl(): string $url .= '/'; } - return $url; + return (string) $url; } public function getAppName(): string @@ -146,21 +147,21 @@ public function isMetricsEnabled(): bool return $this->metricsEnabled; } - public function setUrl(string $url): self + public function setUrl(string|Stringable $url): self { $this->url = $url; return $this; } - public function setAppName(string $appName): self + public function setAppName(string|Stringable $appName): self { $this->appName = $appName; return $this; } - public function setInstanceId(string $instanceId): self + public function setInstanceId(string|Stringable $instanceId): self { $this->instanceId = $instanceId; diff --git a/tests/Configuration/UnleashConfigurationTest.php b/tests/Configuration/UnleashConfigurationTest.php index 23f2ed92..4582b945 100755 --- a/tests/Configuration/UnleashConfigurationTest.php +++ b/tests/Configuration/UnleashConfigurationTest.php @@ -111,4 +111,43 @@ public function testGetMetricsUrl() $resolvedMetricsUrl = $proxyInstance->getMetricsUrl(); self::assertSame($resolvedMetricsUrl, 'http://localhost:3063/api/frontend/client/metrics'); } + + public function testStringable() + { + $realUrl = 'http://localhost:3063/api/'; + $realAppName = 'TestApp'; + $realInstanceId = '123'; + + $shadowed = fn (string &$value) => new class($value) { + private string $realUrl; + + public function __construct( + string &$realUrl, + ) { + $this->realUrl = &$realUrl; + } + + public function __toString(): string + { + return $this->realUrl; + } + }; + + $url = $shadowed($realUrl); + $appName = $shadowed($realAppName); + $instanceId = $shadowed($realInstanceId); + + $instance = new UnleashConfiguration($url, $appName, $instanceId); + self::assertSame($realUrl, (string) $instance->getUrl()); + self::assertSame($realAppName, (string) $instance->getAppName()); + self::assertSame($realInstanceId, (string) $instance->getInstanceId()); + + $realUrl = 'http://localhost:3063/api/v2/'; + $realAppName = 'TestApp2'; + $realInstanceId = '456'; + + self::assertSame($realUrl, (string) $instance->getUrl()); + self::assertSame($realAppName, (string) $instance->getAppName()); + self::assertSame($realInstanceId, (string) $instance->getInstanceId()); + } } From 6ed546c84c28b9cf3fafd6473a42d1e06815461f Mon Sep 17 00:00:00 2001 From: Dominik Chrastecky Date: Tue, 16 Apr 2024 12:59:53 +0200 Subject: [PATCH 2/3] Make test use 7.2 syntax --- .../UnleashConfigurationTest.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/tests/Configuration/UnleashConfigurationTest.php b/tests/Configuration/UnleashConfigurationTest.php index 4582b945..f8039ea8 100755 --- a/tests/Configuration/UnleashConfigurationTest.php +++ b/tests/Configuration/UnleashConfigurationTest.php @@ -118,19 +118,24 @@ public function testStringable() $realAppName = 'TestApp'; $realInstanceId = '123'; - $shadowed = fn (string &$value) => new class($value) { - private string $realUrl; - - public function __construct( - string &$realUrl, - ) { - $this->realUrl = &$realUrl; - } - - public function __toString(): string - { - return $this->realUrl; - } + $shadowed = function (string &$value) { + return new class($value) { + /** + * @var string + */ + private $realUrl; + + public function __construct( + string &$realUrl, + ) { + $this->realUrl = &$realUrl; + } + + public function __toString(): string + { + return $this->realUrl; + } + }; }; $url = $shadowed($realUrl); From d20536ea68e04a9616c8081d69d79ddd9943dd59 Mon Sep 17 00:00:00 2001 From: Dominik Chrastecky Date: Tue, 16 Apr 2024 13:01:15 +0200 Subject: [PATCH 3/3] Make test use 7.2 syntax --- tests/Configuration/UnleashConfigurationTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Configuration/UnleashConfigurationTest.php b/tests/Configuration/UnleashConfigurationTest.php index f8039ea8..441b1a95 100755 --- a/tests/Configuration/UnleashConfigurationTest.php +++ b/tests/Configuration/UnleashConfigurationTest.php @@ -126,7 +126,7 @@ public function testStringable() private $realUrl; public function __construct( - string &$realUrl, + string &$realUrl ) { $this->realUrl = &$realUrl; }