shlink/module/CLI/test/Command/Visit/LocateVisitsCommandTest.php

246 lines
10 KiB
PHP
Raw Normal View History

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
use PHPUnit\Framework\MockObject\MockObject;
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;
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
2019-04-14 10:10:00 +03:00
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
use Shlinkio\Shlink\CLI\Util\ExitCodes;
use Shlinkio\Shlink\Core\Exception\IpCannotBeLocatedException;
use Shlinkio\Shlink\Core\ShortUrl\Entity\ShortUrl;
use Shlinkio\Shlink\Core\Visit\Entity\Visit;
use Shlinkio\Shlink\Core\Visit\Entity\VisitLocation;
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitGeolocationHelperInterface;
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitLocator;
use Shlinkio\Shlink\Core\Visit\Geolocation\VisitToLocationHelperInterface;
use Shlinkio\Shlink\Core\Visit\Model\Visitor;
use Shlinkio\Shlink\IpGeolocation\Exception\WrongIpException;
use Shlinkio\Shlink\IpGeolocation\Model\Location;
use ShlinkioTest\Shlink\CLI\CliTestUtilsTrait;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Output\OutputInterface;
2016-07-30 15:42:09 +03:00
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Lock;
use function sprintf;
2016-07-30 15:42:09 +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
{
use CliTestUtilsTrait;
2020-11-02 13:50:19 +03:00
2019-12-30 00:27:00 +03:00
private CommandTester $commandTester;
private ObjectProphecy $visitService;
private ObjectProphecy $visitToLocation;
2019-12-30 00:27:00 +03:00
private ObjectProphecy $lock;
private MockObject $downloadDbCommand;
2016-07-30 15:42:09 +03:00
protected function setUp(): void
2016-07-30 15:42:09 +03:00
{
$this->visitService = $this->prophesize(VisitLocator::class);
$this->visitToLocation = $this->prophesize(VisitToLocationHelperInterface::class);
2020-11-02 13:50:19 +03:00
$locker = $this->prophesize(Lock\LockFactory::class);
$this->lock = $this->prophesize(Lock\LockInterface::class);
$this->lock->acquire(false)->willReturn(true);
2020-01-01 22:48:31 +03:00
$this->lock->release()->will(function (): void {
});
$locker->createLock(Argument::type('string'), 600.0, false)->willReturn($this->lock->reveal());
2019-04-14 10:10:00 +03:00
$command = new LocateVisitsCommand(
2016-07-30 15:42:09 +03:00
$this->visitService->reveal(),
$this->visitToLocation->reveal(),
2020-11-02 13:50:19 +03:00
$locker->reveal(),
2016-07-30 15:42:09 +03:00
);
$this->downloadDbCommand = $this->createCommandMock(DownloadGeoLiteDbCommand::NAME);
$this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand);
2016-07-30 15:42:09 +03:00
}
/**
* @test
* @dataProvider provideArgs
*/
public function expectedSetOfVisitsIsProcessedBasedOnArgs(
int $expectedUnlocatedCalls,
int $expectedEmptyCalls,
int $expectedAllCalls,
bool $expectWarningPrint,
array $args,
): void {
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
$mockMethodBehavior = $this->invokeHelperMethods($visit, $location);
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will($mockMethodBehavior);
$locateEmptyVisits = $this->visitService->locateVisitsWithEmptyLocation(Argument::cetera())->will(
$mockMethodBehavior,
);
$locateAllVisits = $this->visitService->locateAllVisits(Argument::cetera())->will($mockMethodBehavior);
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willReturn(
2020-01-01 22:48:31 +03:00
Location::emptyInstance(),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
2016-07-30 15:42:09 +03:00
$this->commandTester->setInputs(['y']);
$this->commandTester->execute($args);
2016-07-30 15:42:09 +03:00
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('Processing IP 1.2.3.0', $output);
if ($expectWarningPrint) {
2020-10-25 19:59:37 +03:00
self::assertStringContainsString('Continue at your own', $output);
} else {
2020-10-25 19:59:37 +03:00
self::assertStringNotContainsString('Continue at your own', $output);
}
$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
* @dataProvider provideIgnoredAddresses
2016-07-30 15:42:09 +03:00
*/
public function localhostAndEmptyAddressesAreIgnored(IpCannotBeLocatedException $e, string $message): void
2016-07-30 15:42:09 +03:00
{
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
2019-02-17 15:21:07 +03:00
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
$this->invokeHelperMethods($visit, $location),
);
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow($e);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
self::assertStringContainsString('Processing IP', $output);
2020-10-04 01:35:14 +03:00
self::assertStringContainsString($message, $output);
$locateVisits->shouldHaveBeenCalledOnce();
$resolveIpLocation->shouldHaveBeenCalledOnce();
}
public function provideIgnoredAddresses(): iterable
{
yield 'empty address' => [IpCannotBeLocatedException::forEmptyAddress(), 'Ignored visit with no IP address'];
yield 'localhost address' => [IpCannotBeLocatedException::forLocalhost(), 'Ignored localhost address'];
}
2016-07-30 15:42:09 +03:00
/** @test */
public function errorWhileLocatingIpIsDisplayed(): void
{
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
$location = VisitLocation::fromGeolocation(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(
$this->invokeHelperMethods($visit, $location),
);
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow(
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
);
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString('An error occurred while locating IP. Skipped', $output);
$locateVisits->shouldHaveBeenCalledOnce();
$resolveIpLocation->shouldHaveBeenCalledOnce();
2016-07-30 15:42:09 +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 */
public function noActionIsPerformedIfLockIsAcquired(): void
{
$this->lock->acquire(false)->willReturn(false);
2020-01-01 22:48:31 +03:00
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void {
});
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any());
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
$output = $this->commandTester->getDisplay();
2020-10-04 01:35:14 +03:00
self::assertStringContainsString(
sprintf('Command "%s" is already in progress. Skipping.', LocateVisitsCommand::NAME),
2020-01-01 22:48:31 +03:00
$output,
);
$locateVisits->shouldNotHaveBeenCalled();
$resolveIpLocation->shouldNotHaveBeenCalled();
}
/** @test */
public function showsProperMessageWhenGeoLiteUpdateFails(): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_FAILURE);
$this->commandTester->execute([]);
$output = $this->commandTester->getDisplay();
self::assertStringContainsString('It is not possible to locate visits without a GeoLite2 db file.', $output);
$this->visitService->locateUnlocatedVisits(Argument::cetera())->shouldNotHaveBeenCalled();
}
/** @test */
public function providingAllFlagOnItsOwnDisplaysNotice(): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$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);
}
/**
* @test
* @dataProvider provideAbortInputs
*/
public function processingAllCancelsCommandIfUserDoesNotActivelyAgreeToConfirmation(array $inputs): void
{
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
$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
}