Load specific env file when running API tests

This commit is contained in:
Alejandro Celaya
2024-10-23 09:16:52 +02:00
parent c0200317dd
commit 46601443f5
25 changed files with 59 additions and 83 deletions

View File

@@ -15,10 +15,11 @@ use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
final readonly class UrlShortenerOptions
{
/**
* @param array{schema: ?string, hostname: ?string} $domain
* @param 'http'|'https' $schema
*/
public function __construct(
public array $domain = ['schema' => null, 'hostname' => null],
public string $defaultDomain = '',
public string $schema = 'http',
public int $defaultShortCodesLength = DEFAULT_SHORT_CODES_LENGTH,
public bool $autoResolveTitles = false,
public bool $appendExtraPath = false,
@@ -37,10 +38,8 @@ final readonly class UrlShortenerOptions
$mode = EnvVars::SHORT_URL_MODE->loadFromEnv();
return new self(
domain: [
'schema' => ((bool) EnvVars::IS_HTTPS_ENABLED->loadFromEnv()) ? 'https' : 'http',
'hostname' => EnvVars::DEFAULT_DOMAIN->loadFromEnv(),
],
defaultDomain: EnvVars::DEFAULT_DOMAIN->loadFromEnv(),
schema: ((bool) EnvVars::IS_HTTPS_ENABLED->loadFromEnv()) ? 'https' : 'http',
defaultShortCodesLength: $shortCodesLength,
autoResolveTitles: (bool) EnvVars::AUTO_RESOLVE_TITLES->loadFromEnv(),
appendExtraPath: (bool) EnvVars::REDIRECT_APPEND_EXTRA_PATH->loadFromEnv(),
@@ -54,9 +53,4 @@ final readonly class UrlShortenerOptions
{
return $this->mode === ShortUrlMode::LOOSE;
}
public function defaultDomain(): string
{
return $this->domain['hostname'] ?? '';
}
}

View File

@@ -37,7 +37,7 @@ readonly class DomainService implements DomainServiceInterface
return [
DomainItem::forDefaultDomain(
$this->urlShortenerOptions->defaultDomain(),
$this->urlShortenerOptions->defaultDomain,
$default ?? new EmptyNotFoundRedirectConfig(),
),
...$mappedDomains,
@@ -56,7 +56,7 @@ readonly class DomainService implements DomainServiceInterface
$restOfDomains = [];
foreach ($allDomains as $domain) {
if ($domain->authority === $this->urlShortenerOptions->defaultDomain()) {
if ($domain->authority === $this->urlShortenerOptions->defaultDomain) {
$defaultDomain = $domain;
} else {
$restOfDomains[] = $domain;

View File

@@ -20,8 +20,7 @@ readonly class ShortUrlStringifier implements ShortUrlStringifierInterface
public function stringify(ShortUrl $shortUrl): string
{
$domainConfig = $this->urlShortenerOptions->domain;
$uriWithoutShortCode = (new Uri())->withScheme($domainConfig['schema'] ?? 'http')
$uriWithoutShortCode = (new Uri())->withScheme($this->urlShortenerOptions->schema)
->withHost($this->resolveDomain($shortUrl))
->withPath($this->basePath)
->__toString();
@@ -32,7 +31,6 @@ readonly class ShortUrlStringifier implements ShortUrlStringifierInterface
private function resolveDomain(ShortUrl $shortUrl): string
{
$domainConfig = $this->urlShortenerOptions->domain;
return $shortUrl->getDomain()?->authority ?? $domainConfig['hostname'] ?? '';
return $shortUrl->getDomain()?->authority ?? $this->urlShortenerOptions->defaultDomain;
}
}

View File

@@ -40,7 +40,7 @@ class PersistenceShortUrlRelationResolver implements ShortUrlRelationResolverInt
public function resolveDomain(?string $domain): ?Domain
{
if ($domain === null || $domain === $this->options->defaultDomain()) {
if ($domain === null || $domain === $this->options->defaultDomain) {
return null;
}

View File

@@ -24,7 +24,7 @@ readonly class ShortUrlListService implements ShortUrlListServiceInterface
*/
public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator
{
$defaultDomain = $this->urlShortenerOptions->defaultDomain();
$defaultDomain = $this->urlShortenerOptions->defaultDomain;
$paginator = new Paginator(new ShortUrlRepositoryAdapter($this->repo, $params, $apiKey, $defaultDomain));
$paginator->setMaxPerPage($params->itemsPerPage)
->setCurrentPage($params->page);