2016-07-30 15:42:09 +03:00
|
|
|
<?php
|
2019-10-05 18:26:10 +03:00
|
|
|
|
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;
|
2020-11-02 13:50:19 +03:00
|
|
|
use Prophecy\PhpUnit\ProphecyTrait;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Prophecy\Prophecy\ObjectProphecy;
|
2019-04-14 10:10:00 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
|
2019-04-14 18:59:22 +03:00
|
|
|
use Shlinkio\Shlink\CLI\Exception\GeolocationDbUpdateFailedException;
|
|
|
|
use Shlinkio\Shlink\CLI\Util\GeolocationDbUpdaterInterface;
|
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;
|
2018-10-28 18:20:10 +03:00
|
|
|
use Shlinkio\Shlink\Core\Model\Visitor;
|
2020-03-28 10:05:15 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitGeolocationHelperInterface;
|
2020-03-27 00:17:13 +03:00
|
|
|
use Shlinkio\Shlink\Core\Visit\VisitLocator;
|
2019-08-11 00:26:39 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
|
2019-08-10 14:42:37 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Model\Location;
|
2019-10-25 21:00:26 +03:00
|
|
|
use Shlinkio\Shlink\IpGeolocation\Resolver\IpLocationResolverInterface;
|
2016-07-30 15:42:09 +03:00
|
|
|
use Symfony\Component\Console\Application;
|
2020-03-28 12:23:34 +03:00
|
|
|
use Symfony\Component\Console\Exception\RuntimeException;
|
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;
|
2019-02-27 00:56:43 +03:00
|
|
|
|
2018-11-17 16:10:38 +03:00
|
|
|
use function sprintf;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2020-03-28 12:23:34 +03:00
|
|
|
use const PHP_EOL;
|
|
|
|
|
2019-04-14 10:10:00 +03:00
|
|
|
class LocateVisitsCommandTest extends TestCase
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2020-11-02 13:50:19 +03:00
|
|
|
use ProphecyTrait;
|
|
|
|
|
2019-12-30 00:27:00 +03:00
|
|
|
private CommandTester $commandTester;
|
|
|
|
private ObjectProphecy $visitService;
|
|
|
|
private ObjectProphecy $ipResolver;
|
|
|
|
private ObjectProphecy $lock;
|
|
|
|
private ObjectProphecy $dbUpdater;
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2019-02-16 12:53:45 +03:00
|
|
|
public function setUp(): void
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2020-03-27 00:17:13 +03:00
|
|
|
$this->visitService = $this->prophesize(VisitLocator::class);
|
2019-10-25 21:00:26 +03:00
|
|
|
$this->ipResolver = $this->prophesize(IpLocationResolverInterface::class);
|
2019-04-14 18:59:22 +03:00
|
|
|
$this->dbUpdater = $this->prophesize(GeolocationDbUpdaterInterface::class);
|
2018-08-03 00:02:48 +03:00
|
|
|
|
2020-11-02 13:50:19 +03:00
|
|
|
$locker = $this->prophesize(Lock\LockFactory::class);
|
2018-11-17 16:10:38 +03:00
|
|
|
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
2019-08-04 12:16:46 +03:00
|
|
|
$this->lock->acquire(false)->willReturn(true);
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->lock->release()->will(function (): void {
|
2018-11-17 16:10:38 +03:00
|
|
|
});
|
2021-02-13 00:59:40 +03:00
|
|
|
$locker->createLock(Argument::type('string'), 600.0, false)->willReturn($this->lock->reveal());
|
2018-11-17 16:10:38 +03:00
|
|
|
|
2019-04-14 10:10:00 +03:00
|
|
|
$command = new LocateVisitsCommand(
|
2016-07-30 15:42:09 +03:00
|
|
|
$this->visitService->reveal(),
|
|
|
|
$this->ipResolver->reveal(),
|
2020-11-02 13:50:19 +03:00
|
|
|
$locker->reveal(),
|
2020-01-01 22:48:31 +03:00
|
|
|
$this->dbUpdater->reveal(),
|
2016-07-30 15:42:09 +03:00
|
|
|
);
|
|
|
|
$app = new Application();
|
|
|
|
$app->add($command);
|
|
|
|
|
|
|
|
$this->commandTester = new CommandTester($command);
|
|
|
|
}
|
|
|
|
|
2020-03-28 12:23:34 +03:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideArgs
|
|
|
|
*/
|
|
|
|
public function expectedSetOfVisitsIsProcessedBasedOnArgs(
|
|
|
|
int $expectedUnlocatedCalls,
|
|
|
|
int $expectedEmptyCalls,
|
|
|
|
int $expectedAllCalls,
|
|
|
|
bool $expectWarningPrint,
|
|
|
|
array $args
|
|
|
|
): void {
|
2021-02-07 23:31:12 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
|
2019-02-17 14:59:55 +03:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2020-03-28 12:23:34 +03:00
|
|
|
$mockMethodBehavior = $this->invokeHelperMethods($visit, $location);
|
2018-10-28 18:20:10 +03:00
|
|
|
|
2020-03-28 12:23:34 +03:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will($mockMethodBehavior);
|
|
|
|
$locateEmptyVisits = $this->visitService->locateVisitsWithEmptyLocation(Argument::cetera())->will(
|
|
|
|
$mockMethodBehavior,
|
2018-11-17 11:42:15 +03:00
|
|
|
);
|
2020-03-28 12:23:34 +03:00
|
|
|
$locateAllVisits = $this->visitService->locateAllVisits(Argument::cetera())->will($mockMethodBehavior);
|
2019-02-17 14:59:55 +03:00
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn(
|
2020-01-01 22:48:31 +03:00
|
|
|
Location::emptyInstance(),
|
2019-02-17 14:59:55 +03:00
|
|
|
);
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2020-03-28 12:23:34 +03:00
|
|
|
$this->commandTester->setInputs(['y']);
|
|
|
|
$this->commandTester->execute($args);
|
2016-07-30 15:42:09 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('Processing IP 1.2.3.0', $output);
|
2020-03-28 12:23:34 +03:00
|
|
|
if ($expectWarningPrint) {
|
2020-10-25 19:59:37 +03:00
|
|
|
self::assertStringContainsString('Continue at your own', $output);
|
2020-03-28 12:23:34 +03:00
|
|
|
} else {
|
2020-10-25 19:59:37 +03:00
|
|
|
self::assertStringNotContainsString('Continue at your own', $output);
|
2020-03-28 12:23:34 +03:00
|
|
|
}
|
|
|
|
$locateVisits->shouldHaveBeenCalledTimes($expectedUnlocatedCalls);
|
|
|
|
$locateEmptyVisits->shouldHaveBeenCalledTimes($expectedEmptyCalls);
|
|
|
|
$locateAllVisits->shouldHaveBeenCalledTimes($expectedAllCalls);
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledTimes(
|
|
|
|
$expectedUnlocatedCalls + $expectedEmptyCalls + $expectedAllCalls,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideArgs(): iterable
|
|
|
|
{
|
|
|
|
yield 'no args' => [1, 0, 0, false, []];
|
|
|
|
yield 'retry' => [1, 1, 0, false, ['--retry' => true]];
|
|
|
|
yield 'all' => [0, 0, 1, true, ['--retry' => true, '--all' => true]];
|
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
|
|
|
*/
|
2019-02-17 14:59:55 +03:00
|
|
|
public function localhostAndEmptyAddressesAreIgnored(?string $address, string $message): void
|
2016-07-30 15:42:09 +03:00
|
|
|
{
|
2021-02-07 23:31:12 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', $address, ''));
|
2019-02-17 14:59:55 +03:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2019-02-17 15:21:07 +03:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
2020-03-28 10:05:15 +03:00
|
|
|
$this->invokeHelperMethods($visit, $location),
|
2018-11-17 11:42:15 +03:00
|
|
|
);
|
2019-02-17 14:59:55 +03:00
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn(
|
2020-01-01 22:48:31 +03:00
|
|
|
Location::emptyInstance(),
|
2019-02-17 14:59:55 +03:00
|
|
|
);
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString($message, $output);
|
2019-02-27 00:31:07 +03:00
|
|
|
if (empty($address)) {
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringNotContainsString('Processing IP', $output);
|
2019-02-27 00:31:07 +03:00
|
|
|
} else {
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('Processing IP', $output);
|
2019-02-27 00:31:07 +03:00
|
|
|
}
|
2019-02-17 14:59:55 +03:00
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
2018-11-17 11:42:15 +03:00
|
|
|
}
|
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
public function provideIgnoredAddresses(): iterable
|
2018-11-17 11:42:15 +03:00
|
|
|
{
|
2019-02-17 14:59:55 +03: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 11:42:15 +03:00
|
|
|
}
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
/** @test */
|
|
|
|
public function errorWhileLocatingIpIsDisplayed(): void
|
2018-11-17 11:42:15 +03:00
|
|
|
{
|
2021-02-07 23:31:12 +03:00
|
|
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
|
2019-02-17 14:59:55 +03:00
|
|
|
$location = new VisitLocation(Location::emptyInstance());
|
2016-07-30 15:42:09 +03:00
|
|
|
|
2019-02-17 15:21:07 +03:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
2020-03-28 10:05:15 +03:00
|
|
|
$this->invokeHelperMethods($visit, $location),
|
2018-11-17 11:42:15 +03:00
|
|
|
);
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willThrow(WrongIpException::class);
|
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2019-02-17 14:59:55 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
2018-11-17 11:42:15 +03:00
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('An error occurred while locating IP. Skipped', $output);
|
2019-02-17 14:59:55 +03:00
|
|
|
$locateVisits->shouldHaveBeenCalledOnce();
|
|
|
|
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|
2018-11-17 16:10:38 +03:00
|
|
|
|
2020-03-28 10:05:15 +03:00
|
|
|
private function invokeHelperMethods(Visit $visit, VisitLocation $location): callable
|
|
|
|
{
|
|
|
|
return function (array $args) use ($visit, $location): void {
|
|
|
|
/** @var VisitGeolocationHelperInterface $helper */
|
|
|
|
[$helper] = $args;
|
|
|
|
|
|
|
|
$helper->geolocateVisit($visit);
|
|
|
|
$helper->onVisitLocated($location, $visit);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-17 22:28:34 +03:00
|
|
|
/** @test */
|
2019-08-04 12:16:46 +03:00
|
|
|
public function noActionIsPerformedIfLockIsAcquired(): void
|
2018-11-17 16:10:38 +03:00
|
|
|
{
|
2019-08-04 12:16:46 +03:00
|
|
|
$this->lock->acquire(false)->willReturn(false);
|
2018-11-17 16:10:38 +03:00
|
|
|
|
2020-01-01 22:48:31 +03:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void {
|
2018-11-17 16:10:38 +03:00
|
|
|
});
|
|
|
|
$resolveIpLocation = $this->ipResolver->resolveIpLocation(Argument::any())->willReturn([]);
|
|
|
|
|
2019-04-14 23:20:58 +03:00
|
|
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
2018-11-17 16:10:38 +03:00
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(
|
2019-08-04 12:16:46 +03:00
|
|
|
sprintf('Command "%s" is already in progress. Skipping.', LocateVisitsCommand::NAME),
|
2020-01-01 22:48:31 +03:00
|
|
|
$output,
|
2018-11-17 16:10:38 +03:00
|
|
|
);
|
|
|
|
$locateVisits->shouldNotHaveBeenCalled();
|
|
|
|
$resolveIpLocation->shouldNotHaveBeenCalled();
|
|
|
|
}
|
2019-04-14 18:59:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideParams
|
|
|
|
*/
|
|
|
|
public function showsProperMessageWhenGeoLiteUpdateFails(bool $olderDbExists, string $expectedMessage): void
|
|
|
|
{
|
2020-01-01 22:48:31 +03:00
|
|
|
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void {
|
2019-04-14 18:59:22 +03:00
|
|
|
});
|
|
|
|
$checkDbUpdate = $this->dbUpdater->checkDbUpdate(Argument::cetera())->will(
|
2020-01-01 22:48:31 +03:00
|
|
|
function (array $args) use ($olderDbExists): void {
|
2019-04-14 18:59:22 +03:00
|
|
|
[$mustBeUpdated, $handleProgress] = $args;
|
|
|
|
|
|
|
|
$mustBeUpdated($olderDbExists);
|
|
|
|
$handleProgress(100, 50);
|
|
|
|
|
2021-02-06 23:37:58 +03:00
|
|
|
throw $olderDbExists
|
|
|
|
? GeolocationDbUpdateFailedException::withOlderDb()
|
|
|
|
: GeolocationDbUpdateFailedException::withoutOlderDb();
|
2020-01-01 22:48:31 +03:00
|
|
|
},
|
2019-04-14 18:59:22 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->commandTester->execute([]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString(
|
2019-04-14 18:59:22 +03:00
|
|
|
sprintf('%s GeoLite2 database...', $olderDbExists ? 'Updating' : 'Downloading'),
|
2020-01-01 22:48:31 +03:00
|
|
|
$output,
|
2019-04-14 18:59:22 +03:00
|
|
|
);
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString($expectedMessage, $output);
|
2019-04-14 18:59:22 +03:00
|
|
|
$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.'];
|
|
|
|
}
|
2020-03-28 12:23:34 +03:00
|
|
|
|
|
|
|
/** @test */
|
|
|
|
public function providingAllFlagOnItsOwnDisplaysNotice(): void
|
|
|
|
{
|
|
|
|
$this->commandTester->execute(['--all' => true]);
|
|
|
|
$output = $this->commandTester->getDisplay();
|
|
|
|
|
2020-10-04 01:35:14 +03:00
|
|
|
self::assertStringContainsString('The --all flag has no effect on its own', $output);
|
2020-03-28 12:23:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
* @dataProvider provideAbortInputs
|
|
|
|
*/
|
|
|
|
public function processingAllCancelsCommandIfUserDoesNotActivelyAgreeToConfirmation(array $inputs): void
|
|
|
|
{
|
|
|
|
$this->expectException(RuntimeException::class);
|
|
|
|
$this->expectExceptionMessage('Execution aborted');
|
|
|
|
|
|
|
|
$this->commandTester->setInputs($inputs);
|
|
|
|
$this->commandTester->execute(['--all' => true, '--retry' => true]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideAbortInputs(): iterable
|
|
|
|
{
|
|
|
|
yield 'n' => [['n']];
|
|
|
|
yield 'no' => [['no']];
|
|
|
|
yield 'default' => [[PHP_EOL]];
|
|
|
|
}
|
2016-07-30 15:42:09 +03:00
|
|
|
}
|