2016-07-30 07:30:30 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-09-16 11:36:02 -05:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-07-30 07:30:30 -05:00
|
|
|
|
2018-09-29 05:52:32 -05:00
|
|
|
use Cake\Chronos\Chronos;
|
2021-01-23 07:37:34 -06:00
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 07:30:30 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2022-05-23 13:47:37 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetShortUrlVisitsCommand;
|
2021-01-23 07:37:34 -06:00
|
|
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
2016-07-30 07:30:30 -05:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2018-10-28 10:20:10 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 07:30:30 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2018-09-14 12:12:23 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2020-02-01 10:34:16 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2018-10-28 10:20:10 -05:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2018-11-27 14:09:27 -06:00
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams;
|
2021-02-08 12:46:51 -06:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
2019-08-10 06:42:37 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2021-04-08 06:42:56 -05:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-07-30 07:30:30 -05:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2021-04-08 07:09:26 -05:00
|
|
|
|
2022-05-24 10:43:13 -05:00
|
|
|
use function Shlinkio\Shlink\Common\buildDateRange;
|
2019-12-17 02:59:54 -06:00
|
|
|
use function sprintf;
|
|
|
|
|
2022-05-24 10:43:13 -05:00
|
|
|
class GetShortUrlVisitsCommandTest extends TestCase
|
2016-07-30 07:30:30 -05:00
|
|
|
{
|
2021-04-08 06:42:56 -05:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 04:50:19 -06:00
|
|
|
|
2019-12-29 15:27:00 -06:00
|
|
|
private CommandTester $commandTester;
|
2021-02-08 12:46:51 -06:00
|
|
|
private ObjectProphecy $visitsHelper;
|
2016-07-30 07:30:30 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-30 07:30:30 -05:00
|
|
|
{
|
2021-02-08 12:46:51 -06:00
|
|
|
$this->visitsHelper = $this->prophesize(VisitsStatsHelperInterface::class);
|
2022-05-23 13:47:37 -05:00
|
|
|
$command = new GetShortUrlVisitsCommand($this->visitsHelper->reveal());
|
2021-04-08 06:42:56 -05:00
|
|
|
$this->commandTester = $this->testerForCommand($command);
|
2016-07-30 07:30:30 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-17 02:59:54 -06:00
|
|
|
public function noDateFlagsTriesToListWithoutDateRange(): void
|
2016-07-30 07:30:30 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2021-02-08 12:46:51 -06:00
|
|
|
$this->visitsHelper->visitsForShortUrl(
|
2022-04-23 07:00:47 -05:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-08-04 04:27:33 -05:00
|
|
|
new VisitsParams(DateRange::allTime()),
|
2020-02-01 10:34:16 -06:00
|
|
|
)
|
|
|
|
->willReturn(new Paginator(new ArrayAdapter([])))
|
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 07:30:30 -05:00
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 07:30:30 -05:00
|
|
|
}
|
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-12-17 02:59:54 -06:00
|
|
|
public function providingDateFlagsTheListGetsFiltered(): void
|
2016-07-30 07:30:30 -05:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = '2016-01-01';
|
|
|
|
$endDate = '2016-02-01';
|
2021-02-08 12:46:51 -06:00
|
|
|
$this->visitsHelper->visitsForShortUrl(
|
2022-04-23 07:00:47 -05:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-05-24 10:43:13 -05:00
|
|
|
new VisitsParams(buildDateRange(Chronos::parse($startDate), Chronos::parse($endDate))),
|
2018-11-27 14:09:27 -06:00
|
|
|
)
|
2018-11-28 13:39:08 -06:00
|
|
|
->willReturn(new Paginator(new ArrayAdapter([])))
|
2018-11-11 06:18:21 -06:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 07:30:30 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'shortCode' => $shortCode,
|
2021-01-30 04:25:20 -06:00
|
|
|
'--start-date' => $startDate,
|
|
|
|
'--end-date' => $endDate,
|
2016-07-30 07:30:30 -05:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-17 02:59:54 -06:00
|
|
|
/** @test */
|
|
|
|
public function providingInvalidDatesPrintsWarning(): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = 'foo';
|
2021-02-08 12:46:51 -06:00
|
|
|
$info = $this->visitsHelper->visitsForShortUrl(
|
2022-04-23 07:00:47 -05:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-08-04 04:27:33 -05:00
|
|
|
new VisitsParams(DateRange::allTime()),
|
2021-02-08 12:46:51 -06:00
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
2019-12-17 02:59:54 -06:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'shortCode' => $shortCode,
|
2021-01-30 04:25:20 -06:00
|
|
|
'--start-date' => $startDate,
|
2019-12-17 02:59:54 -06:00
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$info->shouldHaveBeenCalledOnce();
|
2020-10-03 17:35:14 -05:00
|
|
|
self::assertStringContainsString(
|
2021-01-30 04:17:13 -06:00
|
|
|
sprintf('Ignored provided "start-date" since its value "%s" is not a valid date', $startDate),
|
2020-01-01 13:48:31 -06:00
|
|
|
$output,
|
2019-12-17 02:59:54 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
/** @test */
|
|
|
|
public function outputIsProperlyGenerated(): void
|
2016-07-30 07:30:30 -05:00
|
|
|
{
|
2022-05-24 10:43:13 -05:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('bar', 'foo', '', ''))->locate(
|
|
|
|
VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
|
|
|
|
);
|
2016-07-30 07:30:30 -05:00
|
|
|
$shortCode = 'abc123';
|
2022-04-23 07:00:47 -05:00
|
|
|
$this->visitsHelper->visitsForShortUrl(
|
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
Argument::any(),
|
|
|
|
)->willReturn(
|
2022-05-24 10:43:13 -05:00
|
|
|
new Paginator(new ArrayAdapter([$visit])),
|
2018-11-28 13:39:08 -06:00
|
|
|
)->shouldBeCalledOnce();
|
2016-07-30 07:30:30 -05:00
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 07:30:30 -05:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2022-05-24 10:43:13 -05:00
|
|
|
|
|
|
|
self::assertEquals(
|
|
|
|
<<<OUTPUT
|
|
|
|
+---------+---------------------------+------------+---------+--------+
|
|
|
|
| Referer | Date | User agent | Country | City |
|
|
|
|
+---------+---------------------------+------------+---------+--------+
|
|
|
|
| foo | {$visit->getDate()->toAtomString()} | bar | Spain | Madrid |
|
|
|
|
+---------+---------------------------+------------+---------+--------+
|
|
|
|
|
|
|
|
OUTPUT,
|
|
|
|
$output,
|
|
|
|
);
|
2016-07-30 07:30:30 -05:00
|
|
|
}
|
|
|
|
}
|