Show only API key name in short URLs list

This commit is contained in:
Alejandro Celaya 2024-11-05 22:52:01 +01:00
parent a094be2b9e
commit 9f6975119e
3 changed files with 69 additions and 68 deletions

View File

@ -118,14 +118,9 @@ class ListShortUrlsCommand extends Command
'show-api-key', 'show-api-key',
'k', 'k',
InputOption::VALUE_NONE, InputOption::VALUE_NONE,
'Whether to display the API key from which the URL was generated or not.',
)
->addOption(
'show-api-key-name',
'm',
InputOption::VALUE_NONE,
'Whether to display the API key name from which the URL was generated or not.', 'Whether to display the API key name from which the URL was generated or not.',
) )
->addOption('show-api-key-name', 'm', InputOption::VALUE_NONE, '[DEPRECATED] Use show-api-key')
->addOption( ->addOption(
'all', 'all',
'a', 'a',
@ -242,11 +237,7 @@ class ListShortUrlsCommand extends Command
$columnsMap['Domain'] = static fn (array $_, ShortUrl $shortUrl): string => $columnsMap['Domain'] = static fn (array $_, ShortUrl $shortUrl): string =>
$shortUrl->getDomain()?->authority ?? Domain::DEFAULT_AUTHORITY; $shortUrl->getDomain()?->authority ?? Domain::DEFAULT_AUTHORITY;
} }
if ($input->getOption('show-api-key')) { if ($input->getOption('show-api-key') || $input->getOption('show-api-key-name')) {
$columnsMap['API Key'] = static fn (array $_, ShortUrl $shortUrl): string =>
$shortUrl->authorApiKey?->key ?? '';
}
if ($input->getOption('show-api-key-name')) {
$columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): string|null => $columnsMap['API Key Name'] = static fn (array $_, ShortUrl $shortUrl): string|null =>
$shortUrl->authorApiKey?->name; $shortUrl->authorApiKey?->name;
} }

View File

@ -25,7 +25,6 @@ use Shlinkio\Shlink\Rest\Entity\ApiKey;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils; use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Console\Tester\CommandTester;
use function count;
use function explode; use function explode;
class ListShortUrlsCommandTest extends TestCase class ListShortUrlsCommandTest extends TestCase
@ -105,94 +104,100 @@ class ListShortUrlsCommandTest extends TestCase
#[Test, DataProvider('provideOptionalFlags')] #[Test, DataProvider('provideOptionalFlags')]
public function provideOptionalFlagsMakesNewColumnsToBeIncluded( public function provideOptionalFlagsMakesNewColumnsToBeIncluded(
array $input, array $input,
array $expectedContents, string $expectedOutput,
array $notExpectedContents, ShortUrl $shortUrl,
ApiKey $apiKey,
): void { ): void {
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with( $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
ShortUrlsParams::empty(), ShortUrlsParams::empty(),
)->willReturn(new Paginator(new ArrayAdapter([ )->willReturn(new Paginator(new ArrayAdapter([
ShortUrlWithDeps::fromShortUrl( ShortUrlWithDeps::fromShortUrl($shortUrl),
ShortUrl::create(ShortUrlCreation::fromRawData([
'longUrl' => 'https://foo.com',
'tags' => ['foo', 'bar', 'baz'],
'apiKey' => $apiKey,
])),
),
]))); ])));
$this->commandTester->setInputs(['y']); $this->commandTester->setInputs(['y']);
$this->commandTester->execute($input); $this->commandTester->execute($input);
$output = $this->commandTester->getDisplay(); $output = $this->commandTester->getDisplay();
if (count($expectedContents) === 0 && count($notExpectedContents) === 0) { self::assertStringContainsString($expectedOutput, $output);
self::fail('No expectations were run');
}
foreach ($expectedContents as $column) {
self::assertStringContainsString($column, $output);
}
foreach ($notExpectedContents as $column) {
self::assertStringNotContainsString($column, $output);
}
} }
public static function provideOptionalFlags(): iterable public static function provideOptionalFlags(): iterable
{ {
$apiKey = ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'my api key')); $shortUrl = ShortUrl::create(ShortUrlCreation::fromRawData([
$key = $apiKey->key; 'longUrl' => 'https://foo.com',
'tags' => ['foo', 'bar', 'baz'],
'apiKey' => ApiKey::fromMeta(ApiKeyMeta::fromParams(name: 'my api key')),
]));
$shortCode = $shortUrl->getShortCode();
$created = $shortUrl->dateCreated()->toAtomString();
// phpcs:disable Generic.Files.LineLength
yield 'tags only' => [ yield 'tags only' => [
['--show-tags' => true], ['--show-tags' => true],
['| Tags ', '| foo, bar, baz'], <<<OUTPUT
['| API Key ', '| API Key Name |', $key, '| my api key', '| Domain', '| DEFAULT'], +------------+-------+-------------+-----------------+---------------------------+--------------+---------------+
$apiKey, | Short Code | Title | Short URL | Long URL | Date created | Visits count | Tags |
+------------+-------+-------------+-----------------+---------------------------+--------------+---------------+
| {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | foo, bar, baz |
+------------+-------+-------------+-------------- Page 1 of 1 ------------------+--------------+---------------+
OUTPUT,
$shortUrl,
]; ];
yield 'domain only' => [ yield 'domain only' => [
['--show-domain' => true], ['--show-domain' => true],
['| Domain', '| DEFAULT'], <<<OUTPUT
['| Tags ', '| foo, bar, baz', '| API Key ', '| API Key Name |', $key, '| my api key'], +------------+-------+-------------+-----------------+---------------------------+--------------+---------+
$apiKey, | Short Code | Title | Short URL | Long URL | Date created | Visits count | Domain |
+------------+-------+-------------+-----------------+---------------------------+--------------+---------+
| {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | DEFAULT |
+------------+-------+-------------+----------- Page 1 of 1 ---------------------+--------------+---------+
OUTPUT,
$shortUrl,
]; ];
yield 'api key only' => [ yield 'api key only' => [
['--show-api-key' => true], ['--show-api-key' => true],
['| API Key ', $key], <<<OUTPUT
['| Tags ', '| foo, bar, baz', '| API Key Name |', '| my api key', '| Domain', '| DEFAULT'], +------------+-------+-------------+-----------------+---------------------------+--------------+--------------+
$apiKey, | Short Code | Title | Short URL | Long URL | Date created | Visits count | API Key Name |
]; +------------+-------+-------------+-----------------+---------------------------+--------------+--------------+
yield 'api key name only' => [ | {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | my api key |
['--show-api-key-name' => true], +------------+-------+-------------+------------- Page 1 of 1 -------------------+--------------+--------------+
['| API Key Name |', '| my api key'], OUTPUT,
['| Tags ', '| foo, bar, baz', '| API Key ', $key], $shortUrl,
$apiKey,
]; ];
yield 'tags and api key' => [ yield 'tags and api key' => [
['--show-tags' => true, '--show-api-key' => true], ['--show-tags' => true, '--show-api-key' => true],
['| API Key ', '| Tags ', '| foo, bar, baz', $key], <<<OUTPUT
['| API Key Name |', '| my api key'], +------------+-------+-------------+-----------------+---------------------------+--------------+---------------+--------------+
$apiKey, | Short Code | Title | Short URL | Long URL | Date created | Visits count | Tags | API Key Name |
+------------+-------+-------------+-----------------+---------------------------+--------------+---------------+--------------+
| {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | foo, bar, baz | my api key |
+------------+-------+-------------+-----------------+--- Page 1 of 1 -----------+--------------+---------------+--------------+
OUTPUT,
$shortUrl,
]; ];
yield 'tags and domain' => [ yield 'tags and domain' => [
['--show-tags' => true, '--show-domain' => true], ['--show-tags' => true, '--show-domain' => true],
['| Tags ', '| foo, bar, baz', '| Domain', '| DEFAULT'], <<<OUTPUT
['| API Key Name |', '| my api key'], +------------+-------+-------------+-----------------+---------------------------+--------------+---------------+---------+
$apiKey, | Short Code | Title | Short URL | Long URL | Date created | Visits count | Tags | Domain |
+------------+-------+-------------+-----------------+---------------------------+--------------+---------------+---------+
| {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | foo, bar, baz | DEFAULT |
+------------+-------+-------------+-----------------+- Page 1 of 1 -------------+--------------+---------------+---------+
OUTPUT,
$shortUrl,
]; ];
yield 'all' => [ yield 'all' => [
['--show-tags' => true, '--show-domain' => true, '--show-api-key' => true, '--show-api-key-name' => true], ['--show-tags' => true, '--show-domain' => true, '--show-api-key' => true],
[ <<<OUTPUT
'| API Key ', +------------+-------+-------------+-----------------+---------------------------+--------------+---------------+---------+--------------+
'| Tags ', | Short Code | Title | Short URL | Long URL | Date created | Visits count | Tags | Domain | API Key Name |
'| API Key Name |', +------------+-------+-------------+-----------------+---------------------------+--------------+---------------+---------+--------------+
'| foo, bar, baz', | {$shortCode} | | http:/{$shortCode} | https://foo.com | {$created} | 0 | foo, bar, baz | DEFAULT | my api key |
$key, +------------+-------+-------------+-----------------+-------- Page 1 of 1 ------+--------------+---------------+---------+--------------+
'| my api key', OUTPUT,
'| Domain', $shortUrl,
'| DEFAULT',
],
[],
$apiKey,
]; ];
// phpcs:enable
} }
#[Test, DataProvider('provideArgs')] #[Test, DataProvider('provideArgs')]

View File

@ -200,6 +200,11 @@ class ShortUrl extends AbstractEntity
return $this->title; return $this->title;
} }
public function dateCreated(): Chronos
{
return $this->dateCreated;
}
public function reachedVisits(int $visitsAmount): bool public function reachedVisits(int $visitsAmount): bool
{ {
return count($this->visits) >= $visitsAmount; return count($this->visits) >= $visitsAmount;