Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Allow basic parameters to be late bound #201

Merged
merged 3 commits into from
Apr 16, 2024
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
15 changes: 8 additions & 7 deletions src/Configuration/UnleashConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,9 +23,9 @@ final class UnleashConfiguration
* @param array<string,string> $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,
Expand Down Expand Up @@ -71,7 +72,7 @@ public function getUrl(): string
$url .= '/';
}

return $url;
return (string) $url;
}

public function getAppName(): string
Expand Down Expand Up @@ -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;

Expand Down
44 changes: 44 additions & 0 deletions tests/Configuration/UnleashConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,48 @@ 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 = 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);
$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());
}
}
Loading