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;
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Prophecy\Argument;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2018-09-16 19:36:02 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetVisitsCommand;
|
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;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2018-09-14 20:12:23 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2020-02-01 19:34:16 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2018-11-27 23:09:27 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\VisitsParams;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
2019-12-17 11:59:54 +03:00
|
|
|
use function sprintf;
|
|
|
|
|
2016-07-30 15:30:30 +03:00
|
|
|
class GetVisitsCommandTest extends TestCase
|
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
|
|
|
private ObjectProphecy $visitsTracker;
|
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->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
|
2018-11-18 18:02:52 +03:00
|
|
|
$command = new GetVisitsCommand($this->visitsTracker->reveal());
|
2016-07-30 15:30:30 +03:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
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';
|
2020-02-01 19:34:16 +03:00
|
|
|
$this->visitsTracker->info(
|
|
|
|
new ShortUrlIdentifier($shortCode),
|
|
|
|
new VisitsParams(new DateRange(null, null)),
|
|
|
|
)
|
|
|
|
->willReturn(new Paginator(new ArrayAdapter([])))
|
|
|
|
->shouldBeCalledOnce();
|
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';
|
2018-11-27 23:09:27 +03:00
|
|
|
$this->visitsTracker->info(
|
2020-02-01 19:34:16 +03:00
|
|
|
new ShortUrlIdentifier($shortCode),
|
2020-01-01 22:48:31 +03:00
|
|
|
new VisitsParams(new DateRange(Chronos::parse($startDate), Chronos::parse($endDate))),
|
2018-11-27 23:09:27 +03:00
|
|
|
)
|
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,
|
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';
|
2020-02-01 19:34:16 +03:00
|
|
|
$info = $this->visitsTracker->info(new ShortUrlIdentifier($shortCode), new VisitsParams(new DateRange()))
|
2019-12-17 11:59:54 +03:00
|
|
|
->willReturn(new Paginator(new ArrayAdapter([])));
|
|
|
|
|
|
|
|
$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();
|
|
|
|
|
|
|
|
$info->shouldHaveBeenCalledOnce();
|
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
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2020-02-01 19:34:16 +03:00
|
|
|
$this->visitsTracker->info(new ShortUrlIdentifier($shortCode), Argument::any())->willReturn(
|
2018-11-28 22:39:08 +03:00
|
|
|
new Paginator(new ArrayAdapter([
|
|
|
|
(new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->locate(
|
2020-01-01 22:48:31 +03:00
|
|
|
new VisitLocation(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
|
|
|
|
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();
|
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
|
|
|
}
|
|
|
|
}
|