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

121 lines
4.1 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;
2017-03-24 22:34:18 +03:00
use PHPUnit\Framework\TestCase;
2016-07-30 15:30:30 +03:00
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetVisitsCommand;
use Shlinkio\Shlink\Common\Paginator\Paginator;
2016-07-30 15:30:30 +03:00
use Shlinkio\Shlink\Common\Util\DateRange;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
2016-07-30 15:30:30 +03:00
use Shlinkio\Shlink\Core\Entity\Visit;
use Shlinkio\Shlink\Core\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
use Shlinkio\Shlink\Core\Model\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
2016-07-30 15:30:30 +03:00
use Symfony\Component\Console\Tester\CommandTester;
use function sprintf;
2016-07-30 15:30:30 +03:00
class GetVisitsCommandTest extends TestCase
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $visitsHelper;
2016-07-30 15:30:30 +03:00
2019-02-16 12:53:45 +03:00
public function setUp(): void
2016-07-30 15:30:30 +03:00
{
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class);
$command = new GetVisitsCommand($this->visitsHelper->reveal());
$this->commandTester = $this->testerForCommand($command);
2016-07-30 15:30:30 +03:00
}
2019-02-17 22:28:34 +03:00
/** @test */
public function noDateFlagsTriesToListWithoutDateRange(): void
2016-07-30 15:30:30 +03:00
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(DateRange::emptyInstance()),
)
->willReturn(new Paginator(new ArrayAdapter([])))
->shouldBeCalledOnce();
2016-07-30 15:30:30 +03:00
$this->commandTester->execute(['shortCode' => $shortCode]);
2016-07-30 15:30:30 +03:00
}
2019-02-17 22:28:34 +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->visitsForShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(DateRange::withStartAndEndDate(Chronos::parse($startDate), Chronos::parse($endDate))),
)
2018-11-28 22:39:08 +03:00
->willReturn(new Paginator(new ArrayAdapter([])))
2018-11-11 15:18:21 +03:00
->shouldBeCalledOnce();
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';
$info = $this->visitsHelper->visitsForShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
new VisitsParams(DateRange::emptyInstance()),
)->willReturn(new Paginator(new ArrayAdapter([])));
$this->commandTester->execute([
'shortCode' => $shortCode,
'--start-date' => $startDate,
]);
$output = $this->commandTester->getDisplay();
$info->shouldHaveBeenCalledOnce();
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
{
$shortCode = 'abc123';
$this->visitsHelper->visitsForShortUrl(
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
Argument::any(),
)->willReturn(
2018-11-28 22:39:08 +03:00
new Paginator(new ArrayAdapter([
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('bar', 'foo', '', ''))->locate(
VisitLocation::fromGeolocation(new Location('', 'Spain', '', '', 0, 0, '')),
2018-11-28 22:39:08 +03:00
),
2020-01-01 22:48:31 +03:00
])),
2018-11-28 22:39:08 +03:00
)->shouldBeCalledOnce();
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();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('foo', $output);
self::assertStringContainsString('Spain', $output);
self::assertStringContainsString('bar', $output);
2016-07-30 15:30:30 +03:00
}
}