shlink/module/CLI/test/Command/ShortUrl/GetShortUrlVisitsCommandTest.php

121 lines
4.5 KiB
PHP
Raw Normal View History

2016-07-30 15:30:30 +03:00
<?php
2019-10-05 18:26:10 +03:00
2017-10-12 11:13:20 +03:00
declare(strict_types=1);
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
2016-07-30 15:30:30 +03:00
use Cake\Chronos\Chronos;
use Pagerfanta\Adapter\ArrayAdapter;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\MockObject\MockObject;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2022-05-23 21:47:37 +03:00
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetShortUrlVisitsCommand;
use Shlinkio\Shlink\Common\Paginator\Paginator;
2016-07-30 15:30:30 +03:00
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\Util\CliTestUtils;
2016-07-30 15:30:30 +03:00
use Symfony\Component\Console\Tester\CommandTester;
2022-05-24 18:43:13 +03:00
use function Shlinkio\Shlink\Common\buildDateRange;
use function sprintf;
2022-05-24 18:43:13 +03:00
class GetShortUrlVisitsCommandTest extends TestCase
2016-07-30 15:30:30 +03:00
{
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
2022-10-24 20:59:03 +03:00
private MockObject & VisitsStatsHelperInterface $visitsHelper;
2016-07-30 15:30:30 +03:00
protected function setUp(): void
2016-07-30 15:30:30 +03:00
{
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
$command = new GetShortUrlVisitsCommand($this->visitsHelper);
$this->commandTester = CliTestUtils::testerForCommand($command);
2016-07-30 15:30:30 +03:00
}
#[Test]
public function noDateFlagsTriesToListWithoutDateRange(): void
2016-07-30 15:30:30 +03:00
{
$shortCode = 'abc123';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(DateRange::allTime()),
)->willReturn(new Paginator(new ArrayAdapter([])));
2016-07-30 15:30:30 +03:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 15:30:30 +03:00
}
#[Test]
public function providingDateFlagsTheListGetsFiltered(): void
2016-07-30 15:30:30 +03:00
{
$shortCode = 'abc123';
$startDate = '2016-01-01';
$endDate = '2016-02-01';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(buildDateRange(Chronos::parse($startDate), Chronos::parse($endDate))),
)->willReturn(new Paginator(new ArrayAdapter([])));
2016-07-30 15:30:30 +03:00
$this->commandTester->execute([
'shortCode' => $shortCode,
'--start-date' => $startDate,
'--end-date' => $endDate,
2016-07-30 15:30:30 +03:00
]);
}
#[Test]
public function providingInvalidDatesPrintsWarning(): void
{
$shortCode = 'abc123';
$startDate = 'foo';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(DateRange::allTime()),
)->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->execute([
'shortCode' => $shortCode,
'--start-date' => $startDate,
]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString(
sprintf('Ignored provided "start-date" since its value "%s" is not a valid date', $startDate),
2020-01-01 22:48:31 +03:00
$output,
);
}
#[Test]
public function outputIsProperlyGenerated(): void
2016-07-30 15:30:30 +03:00
{
$visit = Visit::forValidShortUrl(ShortUrl::createFake(), new Visitor('bar', 'foo', '', ''))->locate(
2022-05-24 18:43:13 +03:00
VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
);
2016-07-30 15:30:30 +03:00
$shortCode = 'abc123';
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
$this->anything(),
)->willReturn(new Paginator(new ArrayAdapter([$visit])));
2016-07-30 15:30:30 +03:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 15:30:30 +03:00
$output = $this->commandTester->getDisplay();
2022-05-24 18:43:13 +03:00
self::assertEquals(
<<<OUTPUT
+---------+---------------------------+------------+---------+--------+
| Referer | Date | User agent | Country | City |
+---------+---------------------------+------------+---------+--------+
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid |
+---------+---------------------------+------------+---------+--------+
OUTPUT,
$output,
);
2016-07-30 15:30:30 +03:00
}
}