Fixes on static check and unit tests

This commit is contained in:
Alejandro Celaya 2024-03-27 09:33:19 +01:00
parent 10e941cea6
commit 8417498f08
5 changed files with 30 additions and 16 deletions

View file

@ -177,6 +177,9 @@ class ListShortUrlsCommand extends Command
return ExitCode::EXIT_SUCCESS; return ExitCode::EXIT_SUCCESS;
} }
/**
* @param array<string, callable(array $serializedShortUrl, ShortUrl $shortUrl): ?string> $columnsMap
*/
private function renderPage( private function renderPage(
OutputInterface $output, OutputInterface $output,
array $columnsMap, array $columnsMap,
@ -186,8 +189,8 @@ class ListShortUrlsCommand extends Command
$shortUrls = $this->shortUrlService->listShortUrls($params); $shortUrls = $this->shortUrlService->listShortUrls($params);
$rows = map([...$shortUrls], function (ShortUrlWithVisitsSummary $shortUrl) use ($columnsMap) { $rows = map([...$shortUrls], function (ShortUrlWithVisitsSummary $shortUrl) use ($columnsMap) {
$rawShortUrl = $this->transformer->transform($shortUrl); $serializedShortUrl = $this->transformer->transform($shortUrl);
return map($columnsMap, fn (callable $call) => $call($rawShortUrl, $shortUrl)); return map($columnsMap, fn (callable $call) => $call($serializedShortUrl, $shortUrl->shortUrl));
}); });
ShlinkTable::default($output)->render( ShlinkTable::default($output)->render(
@ -210,6 +213,9 @@ class ListShortUrlsCommand extends Command
return $dir === null ? $field : sprintf('%s-%s', $field, $dir); return $dir === null ? $field : sprintf('%s-%s', $field, $dir);
} }
/**
* @return array<string, callable(array $serializedShortUrl, ShortUrl $shortUrl): ?string>
*/
private function resolveColumnsMap(InputInterface $input): array private function resolveColumnsMap(InputInterface $input): array
{ {
$pickProp = static fn (string $prop): callable => static fn (array $shortUrl) => $shortUrl[$prop]; $pickProp = static fn (string $prop): callable => static fn (array $shortUrl) => $shortUrl[$prop];

View file

@ -16,6 +16,7 @@ use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier; use Shlinkio\Shlink\Core\ShortUrl\Helper\ShortUrlStringifier;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlCreation;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Core\ShortUrl\Model\TagsMode; use Shlinkio\Shlink\Core\ShortUrl\Model\TagsMode;
use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface; use Shlinkio\Shlink\Core\ShortUrl\ShortUrlListServiceInterface;
use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer; use Shlinkio\Shlink\Core\ShortUrl\Transformer\ShortUrlDataTransformer;
@ -47,7 +48,7 @@ class ListShortUrlsCommandTest extends TestCase
// The paginator will return more than one page // The paginator will return more than one page
$data = []; $data = [];
for ($i = 0; $i < 50; $i++) { for ($i = 0; $i < 50; $i++) {
$data[] = ShortUrl::withLongUrl('https://url_' . $i); $data[] = ShortUrlWithVisitsSummary::fromShortUrl(ShortUrl::withLongUrl('https://url_' . $i));
} }
$this->shortUrlService->expects($this->exactly(3))->method('listShortUrls')->withAnyParameters() $this->shortUrlService->expects($this->exactly(3))->method('listShortUrls')->withAnyParameters()
@ -69,7 +70,7 @@ class ListShortUrlsCommandTest extends TestCase
// The paginator will return more than one page // The paginator will return more than one page
$data = []; $data = [];
for ($i = 0; $i < 30; $i++) { for ($i = 0; $i < 30; $i++) {
$data[] = ShortUrl::withLongUrl('https://url_' . $i); $data[] = ShortUrlWithVisitsSummary::fromShortUrl(ShortUrl::withLongUrl('https://url_' . $i));
} }
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with( $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
@ -111,11 +112,13 @@ class ListShortUrlsCommandTest extends TestCase
$this->shortUrlService->expects($this->once())->method('listShortUrls')->with( $this->shortUrlService->expects($this->once())->method('listShortUrls')->with(
ShortUrlsParams::emptyInstance(), ShortUrlsParams::emptyInstance(),
)->willReturn(new Paginator(new ArrayAdapter([ )->willReturn(new Paginator(new ArrayAdapter([
ShortUrl::create(ShortUrlCreation::fromRawData([ ShortUrlWithVisitsSummary::fromShortUrl(
'longUrl' => 'https://foo.com', ShortUrl::create(ShortUrlCreation::fromRawData([
'tags' => ['foo', 'bar', 'baz'], 'longUrl' => 'https://foo.com',
'apiKey' => $apiKey, 'tags' => ['foo', 'bar', 'baz'],
])), 'apiKey' => $apiKey,
])),
),
]))); ])));
$this->commandTester->setInputs(['y']); $this->commandTester->setInputs(['y']);

View file

@ -9,7 +9,7 @@ use Shlinkio\Shlink\Core\Visit\Model\VisitsSummary;
final readonly class ShortUrlWithVisitsSummary final readonly class ShortUrlWithVisitsSummary
{ {
private function __construct(public ShortUrl $shortUrl, public VisitsSummary $visitsSummary) private function __construct(public ShortUrl $shortUrl, private ?VisitsSummary $visitsSummary = null)
{ {
} }
@ -24,6 +24,11 @@ final readonly class ShortUrlWithVisitsSummary
)); ));
} }
public static function fromShortUrl(ShortUrl $shortUrl): self
{
return new self($shortUrl);
}
public function toArray(): array public function toArray(): array
{ {
return $this->shortUrl->toArray($this->visitsSummary); return $this->shortUrl->toArray($this->visitsSummary);

View file

@ -5,14 +5,14 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Core\ShortUrl; namespace Shlinkio\Shlink\Core\ShortUrl;
use Shlinkio\Shlink\Common\Paginator\Paginator; use Shlinkio\Shlink\Common\Paginator\Paginator;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams; use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlsParams;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlWithVisitsSummary;
use Shlinkio\Shlink\Rest\Entity\ApiKey; use Shlinkio\Shlink\Rest\Entity\ApiKey;
interface ShortUrlListServiceInterface interface ShortUrlListServiceInterface
{ {
/** /**
* @return ShortUrl[]|Paginator * @return ShortUrlWithVisitsSummary[]|Paginator
*/ */
public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator; public function listShortUrls(ShortUrlsParams $params, ?ApiKey $apiKey = null): Paginator;
} }

View file

@ -48,14 +48,14 @@ final class ShortUrlVisitsCountTracker
*/ */
private function trackVisitCount(EntityManagerInterface $em, object $entity): void private function trackVisitCount(EntityManagerInterface $em, object $entity): void
{ {
// This is not a non-orphan visit // This is not a visit
if (!$entity instanceof Visit || $entity->shortUrl === null) { if (!$entity instanceof Visit) {
return; return;
} }
$visit = $entity; $visit = $entity;
// The short URL is not persisted yet // The short URL is not persisted yet or this is an orphan visit
$shortUrlId = $visit->shortUrl->getId(); $shortUrlId = $visit->shortUrl?->getId();
if ($shortUrlId === null || $shortUrlId === '') { if ($shortUrlId === null || $shortUrlId === '') {
return; return;
} }