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

@@ -40,7 +40,7 @@ readonly class RoleResolver implements RoleResolverInterface
private function resolveRoleForAuthority(string $domainAuthority): RoleDefinition
{
if ($domainAuthority === $this->urlShortenerOptions->defaultDomain()) {
if ($domainAuthority === $this->urlShortenerOptions->defaultDomain) {
throw InvalidRoleConfigException::forDomainOnlyWithDefaultDomain();
}

View File

@@ -25,10 +25,7 @@ class RoleResolverTest extends TestCase
protected function setUp(): void
{
$this->domainService = $this->createMock(DomainServiceInterface::class);
$this->resolver = new RoleResolver(
$this->domainService,
new UrlShortenerOptions(domain: ['hostname' => 'default.com']),
);
$this->resolver = new RoleResolver($this->domainService, new UrlShortenerOptions('default.com'));
}
#[Test, DataProvider('provideRoles')]

View File

@@ -37,10 +37,7 @@ class CreateShortUrlCommandTest extends TestCase
$command = new CreateShortUrlCommand(
$this->urlShortener,
$this->stringifier,
new UrlShortenerOptions(
domain: ['hostname' => 'example.com', 'schema' => ''],
defaultShortCodesLength: 5,
),
new UrlShortenerOptions(defaultDomain: 'example.com', defaultShortCodesLength: 5),
);
$this->commandTester = CliTestUtils::testerForCommand($command);
}

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);

View File

@@ -17,7 +17,6 @@ use Psr\Log\NullLogger;
use Shlinkio\Shlink\Common\Response\QrCodeResponse;
use Shlinkio\Shlink\Core\Action\QrCodeAction;
use Shlinkio\Shlink\Core\Config\Options\QrCodeOptions;
use Shlinkio\Shlink\Core\Config\Options\UrlShortenerOptions;
use Shlinkio\Shlink\Core\Exception\ShortUrlNotFoundException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;

View File

@@ -29,10 +29,7 @@ class DomainServiceTest extends TestCase
protected function setUp(): void
{
$this->em = $this->createMock(EntityManagerInterface::class);
$this->domainService = new DomainService(
$this->em,
new UrlShortenerOptions(domain: ['hostname' => 'default.com']),
);
$this->domainService = new DomainService($this->em, new UrlShortenerOptions(defaultDomain: 'default.com'));
}
#[Test, DataProvider('provideExcludedDomains')]

View File

@@ -37,7 +37,7 @@ class MatomoVisitSenderTest extends TestCase
$this->visitSender = new MatomoVisitSender(
$this->trackerBuilder,
new ShortUrlStringifier(new UrlShortenerOptions(domain: ['hostname' => 's2.test'])),
new ShortUrlStringifier(new UrlShortenerOptions(defaultDomain: 's2.test')),
$this->visitIterationRepository,
);
}

View File

@@ -14,14 +14,18 @@ use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
class ShortUrlStringifierTest extends TestCase
{
/**
* @param 'http'|'https' $schema
*/
#[Test, DataProvider('provideConfigAndShortUrls')]
public function generatesExpectedOutputBasedOnConfigAndShortUrl(
array $domainConfig,
string $defaultDomain,
string $schema,
string $basePath,
ShortUrl $shortUrl,
string $expected,
): void {
$stringifier = new ShortUrlStringifier(new UrlShortenerOptions($domainConfig), $basePath);
$stringifier = new ShortUrlStringifier(new UrlShortenerOptions($defaultDomain, $schema), $basePath);
self::assertEquals($expected, $stringifier->stringify($shortUrl));
}
@@ -36,45 +40,45 @@ class ShortUrlStringifierTest extends TestCase
]),
);
yield 'no config' => [[], '', $shortUrlWithShortCode('foo'), 'http:/foo'];
yield 'hostname in config' => [
['hostname' => 'example.com'],
yield 'no default domain' => ['', 'http', '', $shortUrlWithShortCode('foo'), 'http:/foo'];
yield 'default domain' => [
'example.com',
'http',
'',
$shortUrlWithShortCode('bar'),
'http://example.com/bar',
];
yield 'special chars in short code' => [
['hostname' => 'example.com'],
'example.com',
'http',
'',
$shortUrlWithShortCode('グーグル'),
'http://example.com/グーグル',
];
yield 'emojis in short code' => [
['hostname' => 'example.com'],
'example.com',
'http',
'',
$shortUrlWithShortCode('🦣-🍅'),
'http://example.com/🦣-🍅',
];
yield 'hostname with base path in config' => [
['hostname' => 'example.com/foo/bar'],
yield 'default domain with base path' => [
'example.com/foo/bar',
'http',
'',
$shortUrlWithShortCode('abc'),
'http://example.com/foo/bar/abc',
];
yield 'full config' => [
['schema' => 'https', 'hostname' => 'foo.com'],
'',
$shortUrlWithShortCode('baz'),
'https://foo.com/baz',
];
yield 'custom domain' => [
['schema' => 'https', 'hostname' => 'foo.com'],
'foo.com',
'https',
'',
$shortUrlWithShortCode('baz', 'mydom.es'),
'https://mydom.es/baz',
];
yield 'custom domain with base path' => [
['schema' => 'https', 'hostname' => 'foo.com'],
'foo.com',
'https',
'/foo/bar',
$shortUrlWithShortCode('baz', 'mydom.es'),
'https://mydom.es/foo/bar/baz',

View File

@@ -29,9 +29,7 @@ class PersistenceShortUrlRelationResolverTest extends TestCase
$this->em = $this->createMock(EntityManagerInterface::class);
$this->em->method('getEventManager')->willReturn(new EventManager());
$this->resolver = new PersistenceShortUrlRelationResolver($this->em, new UrlShortenerOptions(
domain: ['schema' => 'https', 'hostname' => 'default.com'],
));
$this->resolver = new PersistenceShortUrlRelationResolver($this->em, new UrlShortenerOptions('default.com'));
}
#[Test, DataProvider('provideDomainsThatEmpty')]

View File

@@ -38,7 +38,7 @@ class DomainVisitsAction extends AbstractRestAction
private function resolveDomainParam(Request $request): string
{
$domainParam = $request->getAttribute('domain', '');
if ($domainParam === $this->urlShortenerOptions->defaultDomain()) {
if ($domainParam === $this->urlShortenerOptions->defaultDomain) {
return 'DEFAULT';
}

View File

@@ -28,7 +28,7 @@ readonly class DropDefaultDomainFromRequestMiddleware implements MiddlewareInter
private function sanitizeDomainFromPayload(array $payload): array
{
if (isset($payload['domain']) && $payload['domain'] === $this->urlShortenerOptions->defaultDomain()) {
if (isset($payload['domain']) && $payload['domain'] === $this->urlShortenerOptions->defaultDomain) {
unset($payload['domain']);
}

View File

@@ -31,10 +31,7 @@ class ListShortUrlsActionTest extends TestCase
$this->service = $this->createMock(ShortUrlListServiceInterface::class);
$this->action = new ListShortUrlsAction($this->service, new ShortUrlDataTransformer(
new ShortUrlStringifier(new UrlShortenerOptions(domain: [
'hostname' => 's.test',
'schema' => 'https',
])),
new ShortUrlStringifier(new UrlShortenerOptions('s.test')),
));
}

View File

@@ -25,10 +25,7 @@ class DomainVisitsActionTest extends TestCase
protected function setUp(): void
{
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$this->action = new DomainVisitsAction(
$this->visitsHelper,
new UrlShortenerOptions(domain: ['hostname' => 'the_default.com']),
);
$this->action = new DomainVisitsAction($this->visitsHelper, new UrlShortenerOptions('the_default.com'));
}
#[Test, DataProvider('provideDomainAuthorities')]

View File

@@ -24,9 +24,7 @@ class DropDefaultDomainFromRequestMiddlewareTest extends TestCase
protected function setUp(): void
{
$this->next = $this->createMock(RequestHandlerInterface::class);
$this->middleware = new DropDefaultDomainFromRequestMiddleware(
new UrlShortenerOptions(domain: ['hostname' => 's.test']),
);
$this->middleware = new DropDefaultDomainFromRequestMiddleware(new UrlShortenerOptions('s.test'));
}
#[Test, DataProvider('provideQueryParams')]