2016-07-30 15:42:09 +03:00
|
|
|
<?php
|
2017-10-12 11:13:20 +03:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2016-08-15 12:01:55 +03:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2017-03-24 22:34:18 +03:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-08-08 10:38:50 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\ProcessVisitsCommand;
|
2018-11-11 14:44:57 +03:00
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\IpApiLocationResolver;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService;
|
|
|
|
use Symfony\Component\Console\Application;
|
2018-10-18 20:19:42 +03:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
2018-10-18 20:19:42 +03:00
|
|
|
use function count;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
class ProcessVisitsCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var CommandTester
|
|
|
|
*/
|
2018-11-12 22:37:30 +03:00
|
|
|
private $commandTester;
|
2016-07-30 15:42:09 +03:00
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
2018-11-12 22:37:30 +03:00
|
|
|
private $visitService;
|
2016-07-30 15:42:09 +03:00
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
2018-11-12 22:37:30 +03:00
|
|
|
private $ipResolver;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->visitService = $this->prophesize(VisitService::class);
|
2018-07-31 21:24:13 +03:00
|
|
|
$this->ipResolver = $this->prophesize(IpApiLocationResolver::class);
|
2018-08-03 00:02:48 +03:00
|
|
|
|
2016-07-30 15:42:09 +03:00
|
|
|
$command = new ProcessVisitsCommand(
|
|
|
|
$this->visitService->reveal(),
|
|
|
|
$this->ipResolver->reveal(),
|
|
|
|
Translator::factory([])
|
|
|
|
);
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function allReturnedVisitsIpsAreProcessed()
|
|
|
|
{
|
2018-10-28 18:20:10 +03:00
|
|
|
$shortUrl = new ShortUrl('');
|
|
|
|
|
2016-07-30 15:42:09 +03:00
|
|
|
$visits = [
|
2018-10-28 18:20:10 +03:00
|
|
|
new Visit($shortUrl, new Visitor('', '', '1.2.3.4')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '4.3.2.1')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '12.34.56.78')),
|
2016-07-30 15:42:09 +03:00
|
|
|
];
|
|
|
|
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits));
|
|
|
|
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
|
|
|
|
->shouldBeCalledTimes(count($visits));
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-10-18 20:19:42 +03:00
|
|
|
$this->assertContains('Processing IP 1.2.3.0', $output);
|
|
|
|
$this->assertContains('Processing IP 4.3.2.0', $output);
|
|
|
|
$this->assertContains('Processing IP 12.34.56.0', $output);
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2018-10-18 20:19:42 +03:00
|
|
|
public function localhostAndEmptyAddressIsIgnored()
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2018-10-28 18:20:10 +03:00
|
|
|
$shortUrl = new ShortUrl('');
|
|
|
|
|
2016-07-30 15:42:09 +03:00
|
|
|
$visits = [
|
2018-10-28 18:20:10 +03:00
|
|
|
new Visit($shortUrl, new Visitor('', '', '1.2.3.4')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '4.3.2.1')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '12.34.56.78')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '127.0.0.1')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '127.0.0.1')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', '')),
|
|
|
|
new Visit($shortUrl, new Visitor('', '', null)),
|
2016-07-30 15:42:09 +03:00
|
|
|
];
|
|
|
|
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
2018-11-11 15:18:21 +03:00
|
|
|
->shouldBeCalledOnce();
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2018-10-18 20:19:42 +03:00
|
|
|
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 4);
|
2016-07-30 15:42:09 +03:00
|
|
|
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
|
2018-10-18 20:19:42 +03:00
|
|
|
->shouldBeCalledTimes(count($visits) - 4);
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
2018-10-18 20:19:42 +03:00
|
|
|
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2016-07-30 15:42:09 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-10-18 20:19:42 +03:00
|
|
|
$this->assertContains('Ignored localhost address', $output);
|
|
|
|
$this->assertContains('Ignored visit with no IP address', $output);
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|
|
|
|
}
|