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

90 lines
3 KiB
PHP
Raw Normal View History

2016-07-30 15:30:30 +03:00
<?php
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;
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;
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\Visitor;
use Shlinkio\Shlink\Core\Model\VisitsParams;
2016-07-30 15:30:30 +03:00
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
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;
2018-11-28 22:39:08 +03:00
use Zend\Paginator\Adapter\ArrayAdapter;
use Zend\Paginator\Paginator;
2016-07-30 15:30:30 +03:00
class GetVisitsCommandTest extends TestCase
{
/** @var CommandTester */
private $commandTester;
/** @var ObjectProphecy */
private $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 */
2016-07-30 15:30:30 +03:00
public function noDateFlagsTriesToListWithoutDateRange()
{
$shortCode = 'abc123';
2018-11-28 22:39:08 +03:00
$this->visitsTracker->info($shortCode, new VisitsParams(new DateRange(null, null)))->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 */
2016-07-30 15:30:30 +03:00
public function providingDateFlagsTheListGetsFiltered()
{
$shortCode = 'abc123';
$startDate = '2016-01-01';
$endDate = '2016-02-01';
$this->visitsTracker->info(
$shortCode,
new VisitsParams(new DateRange(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,
'--startDate' => $startDate,
'--endDate' => $endDate,
]);
}
/** @test */
public function outputIsProperlyGenerated(): void
2016-07-30 15:30:30 +03:00
{
$shortCode = 'abc123';
2018-11-28 22:39:08 +03:00
$this->visitsTracker->info($shortCode, Argument::any())->willReturn(
new Paginator(new ArrayAdapter([
(new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->locate(
new VisitLocation(new Location('', 'Spain', '', '', 0, 0, ''))
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();
2019-02-16 12:53:45 +03:00
$this->assertStringContainsString('foo', $output);
$this->assertStringContainsString('Spain', $output);
$this->assertStringContainsString('bar', $output);
2016-07-30 15:30:30 +03:00
}
}