2016-07-30 07:42:09 -05:00
|
|
|
<?php
|
2019-10-05 10:26:10 -05:00
|
|
|
|
2017-10-12 03:13:20 -05:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
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;
|
2019-04-14 02:10:00 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
|
2019-04-14 10:59:22 -05:00
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
|
|
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
2018-11-17 02:42:15 -06:00
|
|
|
use Shlinkio\Shlink\Common\Util\IpAddress;
|
2018-10-28 10:20:10 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\ShortUrl;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Shlinkio\Shlink\Core\Entity\Visit;
|
2018-11-17 02:42:15 -06:00
|
|
|
use Shlinkio\Shlink\Core\Entity\VisitLocation;
|
2018-10-28 10:20:10 -05:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Shlinkio\Shlink\Core\Service\VisitService;
|
2019-08-10 16:26:39 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
2019-08-10 06:42:37 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-08-10 06:56:06 -05:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpApiLocationResolver;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Symfony\Component\Console\Application;
|
2018-10-18 12:19:42 -05:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-07-30 07:42:09 -05:00
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
2018-11-17 07:10:38 -06:00
|
|
|
use Symfony\Component\Lock;
|
2019-02-26 15:56:43 -06:00
|
|
|
|
2018-11-17 02:42:15 -06:00
|
|
|
use function array_shift;
|
2018-11-17 07:10:38 -06:00
|
|
|
use function sprintf;
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2019-04-14 02:10:00 -05:00
|
|
|
class LocateVisitsCommandTest extends TestCase
|
2016-07-30 07:42:09 -05:00
|
|
|
{
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var CommandTester */
|
2018-11-12 13:37:30 -06:00
|
|
|
private $commandTester;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-12 13:37:30 -06:00
|
|
|
private $visitService;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-12 13:37:30 -06:00
|
|
|
private $ipResolver;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-17 07:10:38 -06:00
|
|
|
private $locker;
|
2018-11-20 12:30:27 -06:00
|
|
|
/** @var ObjectProphecy */
|
2018-11-17 07:10:38 -06:00
|
|
|
private $lock;
|
2019-04-14 10:59:22 -05:00
|
|
|
/** @var ObjectProphecy */
|
|
|
|
private $dbUpdater;
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
public function setUp(): void
|
2016-07-30 07:42:09 -05:00
|
|
|
{
|
|
|
|
$this->visitService = $this->prophesize(VisitService::class);
|
2018-07-31 13:24:13 -05:00
|
|
|
$this->ipResolver = $this->prophesize(IpApiLocationResolver::class);
|
2019-04-14 10:59:22 -05:00
|
|
|
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
2018-08-02 16:02:48 -05:00
|
|
|
|
2018-11-17 07:10:38 -06:00
|
|
|
$this->locker = $this->prophesize(Lock\Factory::class);
|
|
|
|
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
2019-08-04 04:16:46 -05:00
|
|
|
$this->lock->acquire(false)->willReturn(true);
|
2018-11-17 07:10:38 -06:00
|
|
|
$this->lock->release()->will(function () {
|
|
|
|
});
|
2019-08-04 04:16:46 -05:00
|
|
|
$this->locker->createLock(Argument::type('string'), 90.0, false)->willReturn($this->lock->reveal());
|
2018-11-17 07:10:38 -06:00
|
|
|
|
2019-04-14 02:10:00 -05:00
|
|
|
$command = new LocateVisitsCommand(
|
2016-07-30 07:42:09 -05:00
|
|
|
$this->visitService->reveal(),
|
|
|
|
$this->ipResolver->reveal(),
|
2019-04-14 10:59:22 -05:00
|
|
|
$this->locker->reveal(),
|
|
|
|
$this->dbUpdater->reveal()
|
2016-07-30 07:42:09 -05:00
|
|
|
);
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
/** @test */
|
|
|
|
public function allPendingVisitsAreProcessed(): void
|
2016-07-30 07:42:09 -05:00
|
|
|
{
|
2018-11-17 02:42:15 -06:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', '1.2.3.4'));
|
2019-02-17 05:59:55 -06:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2018-10-28 10:20:10 -05:00
|
|
|
|
2019-02-17 06:21:07 -06:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
2018-11-17 02:42:15 -06:00
|
|
|
function (array $args) use ($visit, $location) {
|
|
|
|
$firstCallback = array_shift($args);
|
|
|
|
$firstCallback($visit);
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2018-11-17 02:42:15 -06:00
|
|
|
$secondCallback = array_shift($args);
|
|
|
|
$secondCallback($location, $visit);
|
|
|
|
}
|
|
|
|
);
|
2019-02-17 05:59:55 -06:00
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn(
|
|
|
|
Location::emptyInstance()
|
|
|
|
);
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([]);
|
2016-07-30 07:42:09 -05:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
$this->assertStringContainsString('Processing IP 1.2.3.0', $output);
|
2018-11-17 02:42:15 -06:00
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
2016-07-30 07:42:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
2018-11-17 02:42:15 -06:00
|
|
|
* @dataProvider provideIgnoredAddresses
|
2016-07-30 07:42:09 -05:00
|
|
|
*/
|
2019-02-17 05:59:55 -06:00
|
|
|
public function localhostAndEmptyAddressesAreIgnored(?string $address, string $message): void
|
2016-07-30 07:42:09 -05:00
|
|
|
{
|
2018-11-17 02:42:15 -06:00
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', $address));
|
2019-02-17 05:59:55 -06:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-02-17 06:21:07 -06:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
2018-11-17 02:42:15 -06:00
|
|
|
function (array $args) use ($visit, $location) {
|
|
|
|
$firstCallback = array_shift($args);
|
|
|
|
$firstCallback($visit);
|
|
|
|
|
|
|
|
$secondCallback = array_shift($args);
|
|
|
|
$secondCallback($location, $visit);
|
|
|
|
}
|
|
|
|
);
|
2019-02-17 05:59:55 -06:00
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn(
|
|
|
|
Location::emptyInstance()
|
|
|
|
);
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
$this->assertStringContainsString($message, $output);
|
2019-02-26 15:31:07 -06:00
|
|
|
if (empty($address)) {
|
|
|
|
$this->assertStringNotContainsString('Processing IP', $output);
|
|
|
|
} else {
|
|
|
|
$this->assertStringContainsString('Processing IP', $output);
|
|
|
|
}
|
2019-02-17 05:59:55 -06:00
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
2018-11-17 02:42:15 -06:00
|
|
|
}
|
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
public function provideIgnoredAddresses(): iterable
|
2018-11-17 02:42:15 -06:00
|
|
|
{
|
2019-02-17 05:59:55 -06:00
|
|
|
yield 'with empty address' => ['', 'Ignored visit with no IP address'];
|
|
|
|
yield 'with null address' => [null, 'Ignored visit with no IP address'];
|
|
|
|
yield 'with localhost address' => [IpAddress::LOCALHOST, 'Ignored localhost address'];
|
2018-11-17 02:42:15 -06:00
|
|
|
}
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
/** @test */
|
|
|
|
public function errorWhileLocatingIpIsDisplayed(): void
|
2018-11-17 02:42:15 -06:00
|
|
|
{
|
|
|
|
$visit = new Visit(new ShortUrl(''), new Visitor('', '', '1.2.3.4'));
|
2019-02-17 05:59:55 -06:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2016-07-30 07:42:09 -05:00
|
|
|
|
2019-02-17 06:21:07 -06:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
2018-11-17 02:42:15 -06:00
|
|
|
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);
|
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 02:42:15 -06:00
|
|
|
|
2019-02-17 05:59:55 -06:00
|
|
|
$this->assertStringContainsString('An error occurred while locating IP. Skipped', $output);
|
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
2016-07-30 07:42:09 -05:00
|
|
|
}
|
2018-11-17 07:10:38 -06:00
|
|
|
|
2019-02-17 13:28:34 -06:00
|
|
|
/** @test */
|
2019-08-04 04:16:46 -05:00
|
|
|
public function noActionIsPerformedIfLockIsAcquired(): void
|
2018-11-17 07:10:38 -06:00
|
|
|
{
|
2019-08-04 04:16:46 -05:00
|
|
|
$this->lock->acquire(false)->willReturn(false);
|
2018-11-17 07:10:38 -06:00
|
|
|
|
2019-02-17 06:21:07 -06:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function () {
|
2018-11-17 07:10:38 -06:00
|
|
|
});
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
|
|
|
|
2019-04-14 15:20:58 -05:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 07:10:38 -06:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2019-02-16 03:53:45 -06:00
|
|
|
$this->assertStringContainsString(
|
2019-08-04 04:16:46 -05:00
|
|
|
sprintf('Command "%s" is already in progress. Skipping.', LocateVisitsCommand::NAME),
|
2018-11-17 07:10:38 -06:00
|
|
|
$output
|
|
|
|
);
|
|
|
|
$locateVisits->shouldNotHaveBeenCalled();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
|
|
|
}
|
2019-04-14 10:59:22 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideParams
|
|
|
|
*/
|
|
|
|
public function showsProperMessageWhenGeoLiteUpdateFails(bool $olderDbExists, string $expectedMessage): void
|
|
|
|
{
|
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function () {
|
|
|
|
});
|
|
|
|
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
|
|
|
function (array $args) use ($olderDbExists) {
|
|
|
|
[$mustBeUpdated, $handleProgress] = $args;
|
|
|
|
|
|
|
|
$mustBeUpdated($olderDbExists);
|
|
|
|
$handleProgress(100, 50);
|
|
|
|
|
|
|
|
throw GeolocationDbUpdateFailedException::create($olderDbExists);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
|
|
|
$this->assertStringContainsString(
|
|
|
|
sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'),
|
|
|
|
$output
|
|
|
|
);
|
|
|
|
$this->assertStringContainsString($expectedMessage, $output);
|
|
|
|
$locateVisits->shouldHaveBeenCalledTimes((int) $olderDbExists);
|
|
|
|
$checkDbUpdate->shouldHaveBeenCalledOnce();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideParams(): iterable
|
|
|
|
{
|
|
|
|
yield [true, '[Warning] GeoLite2 database update failed. Proceeding with old version.'];
|
|
|
|
yield [false, 'GeoLite2 database download failed. It is not possible to locate visits.'];
|
|
|
|
}
|
2016-07-30 07:42:09 -05:00
|
|
|
}
|