2016-07-30 07:42:09 -05:00
|
|
|
<?php
|
2016-08-15 04:01:55 -05:00
|
|
|
namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2017-03-24 14:34:18 -05:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Prophecy\Argument;
|
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2016-08-08 02:38:50 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\ProcessVisitsCommand;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Shlinkio\Shlink\Common\Service\IpLocationResolver;
|
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService;
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
use Zend\I18n\Translator\Translator;
|
|
|
|
|
|
|
|
class ProcessVisitsCommandTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var CommandTester
|
|
|
|
*/
|
|
|
|
protected $commandTester;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $visitService;
|
|
|
|
/**
|
|
|
|
* @var ObjectProphecy
|
|
|
|
*/
|
|
|
|
protected $ipResolver;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
$this->visitService = $this->prophesize(VisitService::class);
|
|
|
|
$this->ipResolver = $this->prophesize(IpLocationResolver::class);
|
|
|
|
$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()
|
|
|
|
{
|
|
|
|
$visits = [
|
|
|
|
(new Visit())->setRemoteAddr('1.2.3.4'),
|
|
|
|
(new Visit())->setRemoteAddr('4.3.2.1'),
|
|
|
|
(new Visit())->setRemoteAddr('12.34.56.78'),
|
|
|
|
];
|
|
|
|
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
$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();
|
|
|
|
$this->assertTrue(strpos($output, 'Processing IP 1.2.3.4') === 0);
|
|
|
|
$this->assertTrue(strpos($output, 'Processing IP 4.3.2.1') > 0);
|
|
|
|
$this->assertTrue(strpos($output, 'Processing IP 12.34.56.78') > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function localhostAddressIsIgnored()
|
|
|
|
{
|
|
|
|
$visits = [
|
|
|
|
(new Visit())->setRemoteAddr('1.2.3.4'),
|
|
|
|
(new Visit())->setRemoteAddr('4.3.2.1'),
|
|
|
|
(new Visit())->setRemoteAddr('12.34.56.78'),
|
|
|
|
(new Visit())->setRemoteAddr('127.0.0.1'),
|
|
|
|
(new Visit())->setRemoteAddr('127.0.0.1'),
|
|
|
|
];
|
|
|
|
$this->visitService->getUnlocatedVisits()->willReturn($visits)
|
|
|
|
->shouldBeCalledTimes(1);
|
|
|
|
|
|
|
|
$this->visitService->saveVisit(Argument::any())->shouldBeCalledTimes(count($visits) - 2);
|
|
|
|
$this->ipResolver->resolveIpLocation(Argument::any())->willReturn([])
|
|
|
|
->shouldBeCalledTimes(count($visits) - 2);
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$this->assertTrue(strpos($output, 'Ignored localhost address') > 0);
|
|
|
|
}
|
|
|
|
}
|