Added meta property to ShortUrlDataTransformer

This commit is contained in:
Alejandro Celaya 2019-07-08 18:23:38 +02:00
parent 35950a6294
commit 46a27a9d0a
5 changed files with 117 additions and 3 deletions

View File

@ -17,6 +17,8 @@ use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Zend\Paginator\Paginator;
use function array_flip;
use function array_intersect_key;
use function array_values;
use function count;
use function explode;
@ -29,6 +31,14 @@ class ListShortUrlsCommand extends Command
public const NAME = 'short-url:list';
private const ALIASES = ['shortcode:list', 'short-code:list'];
private const COLUMNS_WHITELIST = [
'shortCode',
'shortUrl',
'longUrl',
'dateCreated',
'visitsCount',
'tags',
];
/** @var ShortUrlServiceInterface */
private $shortUrlService;
@ -125,8 +135,7 @@ class ListShortUrlsCommand extends Command
unset($shortUrl['tags']);
}
unset($shortUrl['originalUrl']);
$rows[] = array_values($shortUrl);
$rows[] = array_values(array_intersect_key($shortUrl, array_flip(self::COLUMNS_WHITELIST)));
}
ShlinkTable::fromOutput($output)->render($headers, $rows, $this->formatCurrentPageMessage(

View File

@ -8,6 +8,7 @@ use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Util\ShortUrlBuilderTrait;
use function Functional\invoke;
use function Functional\invoke_if;
class ShortUrlDataTransformer implements DataTransformerInterface
{
@ -36,9 +37,23 @@ class ShortUrlDataTransformer implements DataTransformerInterface
'dateCreated' => $value->getDateCreated()->toAtomString(),
'visitsCount' => $value->getVisitsCount(),
'tags' => invoke($value->getTags(), '__toString'),
'meta' => $this->buildMeta($value),
// Deprecated
'originalUrl' => $longUrl,
];
}
private function buildMeta(ShortUrl $shortUrl): array
{
$validSince = $shortUrl->getValidSince();
$validUntil = $shortUrl->getValidUntil();
$maxVisits = $shortUrl->getMaxVisits();
return [
'validSince' => invoke_if($validSince, 'toAtomString'),
'validUntil' => invoke_if($validUntil, 'toAtomString'),
'maxVisits' => $maxVisits,
];
}
}

View File

@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\Transformer;
use Cake\Chronos\Chronos;
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Model\ShortUrlMeta;
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
use function random_int;
class ShortUrlDataTransformerTest extends TestCase
{
/** @var ShortUrlDataTransformer */
private $transformer;
public function setUp(): void
{
$this->transformer = new ShortUrlDataTransformer([]);
}
/**
* @test
* @dataProvider provideShortUrls
*/
public function properMetadataIsReturned(ShortUrl $shortUrl, array $expectedMeta): void
{
['meta' => $meta] = $this->transformer->transform($shortUrl);
$this->assertEquals($expectedMeta, $meta);
}
public function provideShortUrls(): iterable
{
$maxVisits = random_int(1, 1000);
$now = Chronos::now();
yield 'no metadata' => [new ShortUrl('', ShortUrlMeta::createEmpty()), [
'validSince' => null,
'validUntil' => null,
'maxVisits' => null,
]];
yield 'max visits only' => [new ShortUrl('', ShortUrlMeta::createFromParams(null, null, null, $maxVisits)), [
'validSince' => null,
'validUntil' => null,
'maxVisits' => $maxVisits,
]];
yield 'max visits and valid since' => [
new ShortUrl('', ShortUrlMeta::createFromParams($now, null, null, $maxVisits)),
[
'validSince' => $now->toAtomString(),
'validUntil' => null,
'maxVisits' => $maxVisits,
],
];
yield 'both dates' => [
new ShortUrl('', ShortUrlMeta::createFromParams($now, $now->subDays(10))),
[
'validSince' => $now->toAtomString(),
'validUntil' => $now->subDays(10)->toAtomString(),
'maxVisits' => null,
],
];
yield 'everything' => [
new ShortUrl('', ShortUrlMeta::createFromParams($now, $now->subDays(5), null, $maxVisits)),
[
'validSince' => $now->toAtomString(),
'validUntil' => $now->subDays(5)->toAtomString(),
'maxVisits' => $maxVisits,
],
];
}
}

View File

@ -24,6 +24,11 @@ class ListShortUrlsTest extends ApiTestCase
'dateCreated' => '2019-01-01T00:00:00+00:00',
'visitsCount' => 3,
'tags' => ['foo'],
'meta' => [
'validSince' => null,
'validUntil' => null,
'maxVisits' => null,
],
'originalUrl' => 'https://shlink.io',
],
[
@ -35,6 +40,11 @@ class ListShortUrlsTest extends ApiTestCase
'dateCreated' => '2019-01-01T00:00:00+00:00',
'visitsCount' => 2,
'tags' => ['foo', 'bar'],
'meta' => [
'validSince' => '2020-05-01T00:00:00+00:00',
'validUntil' => null,
'maxVisits' => null,
],
'originalUrl' =>
'https://blog.alejandrocelaya.com/2017/12/09'
. '/acmailer-7-0-the-most-important-release-in-a-long-time/',
@ -46,6 +56,11 @@ class ListShortUrlsTest extends ApiTestCase
'dateCreated' => '2019-01-01T00:00:00+00:00',
'visitsCount' => 0,
'tags' => [],
'meta' => [
'validSince' => null,
'validUntil' => null,
'maxVisits' => 2,
],
'originalUrl' => 'https://shlink.io',
],
],

View File

@ -24,7 +24,7 @@ class ShortUrlsFixture extends AbstractFixture
$defShortUrl = $this->setShortUrlDate(new ShortUrl(
'https://blog.alejandrocelaya.com/2017/12/09/acmailer-7-0-the-most-important-release-in-a-long-time/',
ShortUrlMeta::createFromParams(Chronos::now()->addDays(3))
ShortUrlMeta::createFromParams(Chronos::parse('2020-05-01'))
))->setShortCode('def456');
$manager->persist($defShortUrl);