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);
|
|
|
|
|
2018-09-16 19:36:02 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\ShortUrl;
|
2016-07-30 15:30:30 +03:00
|
|
|
|
2018-09-29 13:52:32 +03:00
|
|
|
use Cake\Chronos\Chronos;
|
2021-01-23 16:37:34 +03:00
|
|
|
use Pagerfanta\Adapter\ArrayAdapter;
|
2022-10-22 14:38:46 +03:00
|
|
|
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;
|
2021-01-23 16:37:34 +03:00
|
|
|
use Shlinkio\Shlink\Common\Paginator\Paginator;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\DateRange;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlIdentifier;
|
2022-09-23 20:03:32 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
|
2022-09-23 19:05:17 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
|
|
|
|
use Shlinkio\Shlink\Core\Visit\Model\VisitsParams;
|
2021-02-08 21:46:51 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitsStatsHelperInterface;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2021-04-08 14:42:56 +03:00
|
|
|
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2021-04-08 15:09:26 +03:00
|
|
|
|
2022-05-24 18:43:13 +03:00
|
|
|
use function Shlinkio\Shlink\Common\buildDateRange;
|
2019-12-17 11:59:54 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
2022-05-24 18:43:13 +03:00
|
|
|
class GetShortUrlVisitsCommandTest extends TestCase
|
2016-07-30 15:30:30 +03:00
|
|
|
{
|
2021-04-08 14:42:56 +03:00
|
|
|
use CliTestUtilsTrait;
|
2020-11-02 13:50:19 +03:00
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
2022-10-22 14:38:46 +03:00
|
|
|
private MockObject $visitsHelper;
|
2016-07-30 15:30:30 +03:00
|
|
|
|
2022-09-11 13:02:49 +03:00
|
|
|
protected function setUp(): void
|
2016-07-30 15:30:30 +03:00
|
|
|
{
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->visitsHelper = $this->createMock(VisitsStatsHelperInterface::class);
|
|
|
|
$command = new GetShortUrlVisitsCommand($this->visitsHelper);
|
2021-04-08 14:42:56 +03:00
|
|
|
$this->commandTester = $this->testerForCommand($command);
|
2016-07-30 15:30:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-12-17 11:59:54 +03:00
|
|
|
public function noDateFlagsTriesToListWithoutDateRange(): void
|
2016-07-30 15:30:30 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
new VisitsParams(DateRange::allTime()),
|
2022-10-22 14:38:46 +03:00
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
2016-07-30 15:30:30 +03:00
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute(['shortCode' => $shortCode]);
|
2016-07-30 15:30:30 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-12-17 11:59:54 +03:00
|
|
|
public function providingDateFlagsTheListGetsFiltered(): void
|
2016-07-30 15:30:30 +03:00
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = '2016-01-01';
|
|
|
|
$endDate = '2016-02-01';
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
new VisitsParams(buildDateRange(Chronos::parse($startDate), Chronos::parse($endDate))),
|
2022-10-22 14:38:46 +03:00
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
2016-07-30 15:30:30 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'shortCode' => $shortCode,
|
2021-01-30 13:25:20 +03:00
|
|
|
'--start-date' => $startDate,
|
|
|
|
'--end-date' => $endDate,
|
2016-07-30 15:30:30 +03:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2019-12-17 11:59:54 +03:00
|
|
|
/** @test */
|
|
|
|
public function providingInvalidDatesPrintsWarning(): void
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = 'foo';
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
|
|
|
new VisitsParams(DateRange::allTime()),
|
2021-02-08 21:46:51 +03:00
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([])));
|
2019-12-17 11:59:54 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'shortCode' => $shortCode,
|
2021-01-30 13:25:20 +03:00
|
|
|
'--start-date' => $startDate,
|
2019-12-17 11:59:54 +03:00
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(
|
2021-01-30 13:17:13 +03:00
|
|
|
sprintf('Ignored provided "start-date" since its value "%s" is not a valid date', $startDate),
|
2020-01-01 22:48:31 +03:00
|
|
|
$output,
|
2019-12-17 11:59:54 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
/** @test */
|
|
|
|
public function outputIsProperlyGenerated(): void
|
2016-07-30 15:30:30 +03:00
|
|
|
{
|
2022-05-24 18:43:13 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('bar', 'foo', '', ''))->locate(
|
|
|
|
VisitLocation::fromGeolocation(new Location('', 'Spain', '', 'Madrid', 0, 0, '')),
|
|
|
|
);
|
2016-07-30 15:30:30 +03:00
|
|
|
$shortCode = 'abc123';
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->visitsHelper->expects($this->once())->method('visitsForShortUrl')->with(
|
2022-10-23 19:15:57 +03:00
|
|
|
ShortUrlIdentifier::fromShortCodeAndDomain($shortCode),
|
2022-10-22 14:38:46 +03:00
|
|
|
$this->anything(),
|
|
|
|
)->willReturn(new Paginator(new ArrayAdapter([$visit])));
|
2016-07-30 15:30:30 +03:00
|
|
|
|
2019-04-14 23:20:58 +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
|
|
|
}
|
|
|
|
}
|