2016-07-30 15:30:30 +03:00
|
|
|
<?php
|
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
|
|
|
|
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;
|
2018-09-16 19:36:02 +03:00
|
|
|
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\Visit;
|
2018-09-14 20:12:23 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2016-07-30 15:30:30 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
|
|
|
class GetVisitsCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var CommandTester
|
|
|
|
*/
|
|
|
|
protected $commandTester;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $visitsTracker;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
|
|
|
|
$command = new GetVisitsCommand($this->visitsTracker->reveal(), Translator::factory([]));
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function noDateFlagsTriesToListWithoutDateRange()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->visitsTracker->info($shortCode, new DateRange(null, null))->willReturn([])
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function providingDateFlagsTheListGetsFiltered()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = '2016-01-01';
|
|
|
|
$endDate = '2016-02-01';
|
|
|
|
$this->visitsTracker->info($shortCode, new DateRange(new \DateTime($startDate), new \DateTime($endDate)))
|
|
|
|
->willReturn([])
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
'--startDate' => $startDate,
|
|
|
|
'--endDate' => $endDate,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function outputIsProperlyGenerated()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$this->visitsTracker->info($shortCode, Argument::any())->willReturn([
|
|
|
|
(new Visit())->setReferer('foo')
|
2018-09-14 20:12:23 +03:00
|
|
|
->setVisitLocation((new VisitLocation())->setCountryName('Spain'))
|
2016-07-30 15:30:30 +03:00
|
|
|
->setUserAgent('bar'),
|
|
|
|
])->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-09-14 20:12:23 +03:00
|
|
|
$this->assertGreaterThan(0, \strpos($output, 'foo'));
|
|
|
|
$this->assertGreaterThan(0, \strpos($output, 'Spain'));
|
|
|
|
$this->assertGreaterThan(0, \strpos($output, 'bar'));
|
2016-07-30 15:30:30 +03:00
|
|
|
}
|
|
|
|
}
|