Ensured base path is honored when stringifying short URLs with a custom domain

This commit is contained in:
Alejandro Celaya
2021-02-01 23:18:19 +01:00
parent 4b4a859722
commit 8fa0c95f5a
3 changed files with 25 additions and 5 deletions

View File

@@ -121,7 +121,7 @@ return [
],
ShortUrl\Resolver\PersistenceShortUrlRelationResolver::class => ['em'],
ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain'],
ShortUrl\Helper\ShortUrlStringifier::class => ['config.url_shortener.domain', 'config.router.base_path'],
ShortUrl\Transformer\ShortUrlDataTransformer::class => [ShortUrl\Helper\ShortUrlStringifier::class],
Mercure\MercureUpdatesGenerator::class => [ShortUrl\Transformer\ShortUrlDataTransformer::class],

View File

@@ -7,13 +7,17 @@ namespace Shlinkio\Shlink\Core\ShortUrl\Helper;
use Laminas\Diactoros\Uri;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use function sprintf;
class ShortUrlStringifier implements ShortUrlStringifierInterface
{
private array $domainConfig;
private string $basePath;
public function __construct(array $domainConfig)
public function __construct(array $domainConfig, string $basePath = '')
{
$this->domainConfig = $domainConfig;
$this->basePath = $basePath;
}
public function stringify(ShortUrl $shortUrl): string
@@ -31,6 +35,6 @@ class ShortUrlStringifier implements ShortUrlStringifierInterface
return $this->domainConfig['hostname'] ?? '';
}
return $domain->getAuthority();
return sprintf('%s%s', $domain->getAuthority(), $this->basePath);
}
}

View File

@@ -17,10 +17,11 @@ class ShortUrlStringifierTest extends TestCase
*/
public function generatesExpectedOutputBasedOnConfigAndShortUrl(
array $config,
string $basePath,
ShortUrl $shortUrl,
string $expected
): void {
$stringifier = new ShortUrlStringifier($config);
$stringifier = new ShortUrlStringifier($config, $basePath);
self::assertEquals($expected, $stringifier->stringify($shortUrl));
}
@@ -35,21 +36,36 @@ class ShortUrlStringifierTest extends TestCase
]),
);
yield 'no config' => [[], $shortUrlWithShortCode('foo'), 'http:/foo'];
yield 'no config' => [[], '', $shortUrlWithShortCode('foo'), 'http:/foo'];
yield 'hostname in config' => [
['hostname' => 'example.com'],
'',
$shortUrlWithShortCode('bar'),
'http://example.com/bar',
];
yield 'hostname with base path in config' => [
['hostname' => 'example.com/foo/bar'],
'',
$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'],
'',
$shortUrlWithShortCode('baz', 'mydom.es'),
'https://mydom.es/baz',
];
yield 'custom domain with base path' => [
['schema' => 'https', 'hostname' => 'foo.com'],
'/foo/bar',
$shortUrlWithShortCode('baz', 'mydom.es'),
'https://mydom.es/foo/bar/baz',
];
}
}