2016-07-30 07:30:30 -05:00
|
|
|
<?php
|
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;
|
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;
|
2018-09-16 11:36:02 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\ShortUrl\GetVisitsCommand;
|
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;
|
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;
|
2016-07-30 07:30:30 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitsTrackerInterface;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2018-11-28 13:39:08 -06:00
|
|
|
use Zend\Paginator\Adapter\ArrayAdapter;
|
|
|
|
use Zend\Paginator\Paginator;
|
2016-07-30 07:30:30 -05:00
|
|
|
|
|
|
|
class GetVisitsCommandTest extends TestCase
|
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var CommandTester */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $commandTester;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-20 12:37:22 -06:00
|
|
|
private $visitsTracker;
|
2016-07-30 07:30:30 -05:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->visitsTracker = $this->prophesize(VisitsTrackerInterface::class);
|
2018-11-18 09:02:52 -06:00
|
|
|
$command = new GetVisitsCommand($this->visitsTracker->reveal());
|
2016-07-30 07:30:30 -05:00
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function noDateFlagsTriesToListWithoutDateRange()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2018-11-28 13:39:08 -06:00
|
|
|
$this->visitsTracker->info($shortCode, new VisitsParams(new DateRange(null, null)))->willReturn(
|
|
|
|
new Paginator(new ArrayAdapter([]))
|
|
|
|
)->shouldBeCalledOnce();
|
2016-07-30 07:30:30 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function providingDateFlagsTheListGetsFiltered()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
|
|
|
$startDate = '2016-01-01';
|
|
|
|
$endDate = '2016-02-01';
|
2018-11-27 14:09:27 -06:00
|
|
|
$this->visitsTracker->info(
|
|
|
|
$shortCode,
|
|
|
|
new VisitsParams(new DateRange(Chronos::parse($startDate), Chronos::parse($endDate)))
|
|
|
|
)
|
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([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
'--startDate' => $startDate,
|
|
|
|
'--endDate' => $endDate,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function outputIsProperlyGenerated()
|
|
|
|
{
|
|
|
|
$shortCode = 'abc123';
|
2018-11-28 13:39:08 -06:00
|
|
|
$this->visitsTracker->info($shortCode, Argument::any())->willReturn(
|
|
|
|
new Paginator(new ArrayAdapter([
|
|
|
|
(new Visit(new ShortUrl(''), new Visitor('bar', 'foo', '')))->locate(
|
|
|
|
new VisitLocation(['country_name' => 'Spain'])
|
|
|
|
),
|
|
|
|
]))
|
|
|
|
)->shouldBeCalledOnce();
|
2016-07-30 07:30:30 -05:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'shortcode:visits',
|
|
|
|
'shortCode' => $shortCode,
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-28 13:39:08 -06:00
|
|
|
$this->assertContains('foo', $output);
|
|
|
|
$this->assertContains('Spain', $output);
|
|
|
|
$this->assertContains('bar', $output);
|
2016-07-30 07:30:30 -05:00
|
|
|
}
|
|
|
|
}
|