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-17 11:42:15 +03:00
|
|
|
use Shlinkio\Shlink\Common\Exception\WrongIpException;
|
2018-11-11 14:44:57 +03:00
|
|
|
use Shlinkio\Shlink\Common\IpGeolocation\IpApiLocationResolver;
|
2018-11-17 11:42:15 +03:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
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-11-17 11:42:15 +03:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
|
|
|
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
|
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;
|
2018-11-17 16:10:38 +03:00
|
|
|
use Symfony\Component\Lock;
|
2018-11-17 11:42:15 +03:00
|
|
|
use Throwable;
|
|
|
|
use function array_shift;
|
2018-11-17 16:10:38 +03:00
|
|
|
use function sprintf;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
class ProcessVisitsCommandTest extends TestCase
|
|
|
|
{
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var CommandTester */
|
2018-11-12 22:37:30 +03:00
|
|
|
private $commandTester;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-12 22:37:30 +03:00
|
|
|
private $visitService;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-12 22:37:30 +03:00
|
|
|
private $ipResolver;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-17 16:10:38 +03:00
|
|
|
private $locker;
|
2018-11-20 21:30:27 +03:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-17 16:10:38 +03:00
|
|
|
private $lock;
|
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
|
|
|
|
2018-11-17 16:10:38 +03:00
|
|
|
$this->locker = $this->prophesize(Lock\Factory::class);
|
|
|
|
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
|
|
|
$this->lock->acquire()->willReturn(true);
|
|
|
|
$this->lock->release()->will(function () {
|
|
|
|
});
|
|
|
|
$this->locker->createLock(Argument::type('string'))->willReturn($this->lock->reveal());
|
|
|
|
|
2016-07-30 15:42:09 +03:00
|
|
|
$command = new ProcessVisitsCommand(
|
|
|
|
$this->visitService->reveal(),
|
|
|
|
$this->ipResolver->reveal(),
|
2018-11-18 18:02:52 +03:00
|
|
|
$this->locker->reveal()
|
2016-07-30 15:42:09 +03:00
|
|
|
);
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2018-11-17 11:42:15 +03:00
|
|
|
public function allPendingVisitsAreProcessed()
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2018-11-17 11:42:15 +03:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', '1.2.3.4'));
|
|
|
|
$location = new VisitLocation([]);
|
2018-10-28 18:20:10 +03:00
|
|
|
|
2018-11-17 11:42:15 +03:00
|
|
|
$locateVisits = $this->visitService->locateVisits(Argument::cetera())->will(
|
|
|
|
function (array $args) use ($visit, $location) {
|
|
|
|
$firstCallback = array_shift($args);
|
|
|
|
$firstCallback($visit);
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2018-11-17 11:42:15 +03:00
|
|
|
$secondCallback = array_shift($args);
|
|
|
|
$secondCallback($location, $visit);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
2016-07-30 15:42:09 +03:00
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2018-10-18 20:19:42 +03:00
|
|
|
$this->assertContains('Processing IP 1.2.3.0', $output);
|
2018-11-17 11:42:15 +03:00
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2018-11-17 11:42:15 +03:00
|
|
|
* @dataProvider provideIgnoredAddresses
|
2016-07-30 15:42:09 +03:00
|
|
|
*/
|
2018-11-17 11:42:15 +03:00
|
|
|
public function localhostAndEmptyAddressesAreIgnored(?string $address, string $message)
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2018-11-17 11:42:15 +03:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', $address));
|
|
|
|
$location = new VisitLocation([]);
|
|
|
|
|
|
|
|
$locateVisits = $this->visitService->locateVisits(Argument::cetera())->will(
|
|
|
|
function (array $args) use ($visit, $location) {
|
|
|
|
$firstCallback = array_shift($args);
|
|
|
|
$firstCallback($visit);
|
|
|
|
|
|
|
|
$secondCallback = array_shift($args);
|
|
|
|
$secondCallback($location, $visit);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(IpCannotBeLocatedException::class, $e);
|
|
|
|
|
|
|
|
$this->assertContains($message, $output);
|
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideIgnoredAddresses(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['', 'Ignored visit with no IP address'],
|
|
|
|
[null, 'Ignored visit with no IP address'],
|
|
|
|
[IpAddress::LOCALHOST, 'Ignored localhost address'],
|
2016-07-30 15:42:09 +03:00
|
|
|
];
|
2018-11-17 11:42:15 +03:00
|
|
|
}
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2018-11-17 11:42:15 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function errorWhileLocatingIpIsDisplayed()
|
|
|
|
{
|
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', '1.2.3.4'));
|
|
|
|
$location = new VisitLocation([]);
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2018-11-17 11:42:15 +03:00
|
|
|
$locateVisits = $this->visitService->locateVisits(Argument::cetera())->will(
|
|
|
|
function (array $args) use ($visit, $location) {
|
|
|
|
$firstCallback = array_shift($args);
|
|
|
|
$firstCallback($visit);
|
|
|
|
|
|
|
|
$secondCallback = array_shift($args);
|
|
|
|
$secondCallback($location, $visit);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willThrow(WrongIpException::class);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(IpCannotBeLocatedException::class, $e);
|
|
|
|
|
|
|
|
$this->assertContains('An error occurred while locating IP. Skipped', $output);
|
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|
2018-11-17 16:10:38 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function noActionIsPerformedIfLockIsAcquired()
|
|
|
|
{
|
|
|
|
$this->lock->acquire()->willReturn(false);
|
|
|
|
|
|
|
|
$locateVisits = $this->visitService->locateVisits(Argument::cetera())->will(function () {
|
|
|
|
});
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
|
|
|
|
|
|
|
$this->commandTester->execute([
|
|
|
|
'command' => 'visit:process',
|
|
|
|
], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertContains(
|
|
|
|
sprintf('There is already an instance of the "%s" command', ProcessVisitsCommand::NAME),
|
|
|
|
$output
|
|
|
|
);
|
|
|
|
$locateVisits->shouldNotHaveBeenCalled();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
|
|
|
}
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|