mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-11 00:22:04 -06:00
Migrated LocateVisitsCommandTest to use PHPUnit mocks
This commit is contained in:
parent
e2986a7b4c
commit
4b3ed2b7ba
@ -6,8 +6,6 @@ namespace ShlinkioTest\Shlink\CLI\Command\Visit;
|
|||||||
|
|
||||||
use PHPUnit\Framework\MockObject\MockObject;
|
use PHPUnit\Framework\MockObject\MockObject;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
use Prophecy\Argument;
|
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
|
||||||
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
|
use Shlinkio\Shlink\CLI\Command\Visit\DownloadGeoLiteDbCommand;
|
||||||
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
|
use Shlinkio\Shlink\CLI\Command\Visit\LocateVisitsCommand;
|
||||||
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
use Shlinkio\Shlink\CLI\Util\ExitCodes;
|
||||||
@ -36,28 +34,21 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
use CliTestUtilsTrait;
|
use CliTestUtilsTrait;
|
||||||
|
|
||||||
private CommandTester $commandTester;
|
private CommandTester $commandTester;
|
||||||
private ObjectProphecy $visitService;
|
private MockObject $visitService;
|
||||||
private ObjectProphecy $visitToLocation;
|
private MockObject $visitToLocation;
|
||||||
private ObjectProphecy $lock;
|
private MockObject $lock;
|
||||||
private MockObject $downloadDbCommand;
|
private MockObject $downloadDbCommand;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->visitService = $this->prophesize(VisitLocator::class);
|
$this->visitService = $this->createMock(VisitLocator::class);
|
||||||
$this->visitToLocation = $this->prophesize(VisitToLocationHelperInterface::class);
|
$this->visitToLocation = $this->createMock(VisitToLocationHelperInterface::class);
|
||||||
|
|
||||||
$locker = $this->prophesize(Lock\LockFactory::class);
|
$locker = $this->createMock(Lock\LockFactory::class);
|
||||||
$this->lock = $this->prophesize(Lock\LockInterface::class);
|
$this->lock = $this->createMock(Lock\LockInterface::class);
|
||||||
$this->lock->acquire(false)->willReturn(true);
|
$locker->method('createLock')->with($this->isType('string'), 600.0, false)->willReturn($this->lock);
|
||||||
$this->lock->release()->will(function (): void {
|
|
||||||
});
|
|
||||||
$locker->createLock(Argument::type('string'), 600.0, false)->willReturn($this->lock->reveal());
|
|
||||||
|
|
||||||
$command = new LocateVisitsCommand(
|
$command = new LocateVisitsCommand($this->visitService, $this->visitToLocation, $locker);
|
||||||
$this->visitService->reveal(),
|
|
||||||
$this->visitToLocation->reveal(),
|
|
||||||
$locker->reveal(),
|
|
||||||
);
|
|
||||||
|
|
||||||
$this->downloadDbCommand = $this->createCommandMock(DownloadGeoLiteDbCommand::NAME);
|
$this->downloadDbCommand = $this->createCommandMock(DownloadGeoLiteDbCommand::NAME);
|
||||||
$this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand);
|
$this->commandTester = $this->testerForCommand($command, $this->downloadDbCommand);
|
||||||
@ -78,14 +69,22 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
||||||
$mockMethodBehavior = $this->invokeHelperMethods($visit, $location);
|
$mockMethodBehavior = $this->invokeHelperMethods($visit, $location);
|
||||||
|
|
||||||
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will($mockMethodBehavior);
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
|
||||||
$locateEmptyVisits = $this->visitService->locateVisitsWithEmptyLocation(Argument::cetera())->will(
|
$this->visitService->expects($this->exactly($expectedUnlocatedCalls))
|
||||||
$mockMethodBehavior,
|
->method('locateUnlocatedVisits')
|
||||||
);
|
->withAnyParameters()
|
||||||
$locateAllVisits = $this->visitService->locateAllVisits(Argument::cetera())->will($mockMethodBehavior);
|
->willReturnCallback($mockMethodBehavior);
|
||||||
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willReturn(
|
$this->visitService->expects($this->exactly($expectedEmptyCalls))
|
||||||
Location::emptyInstance(),
|
->method('locateVisitsWithEmptyLocation')
|
||||||
);
|
->withAnyParameters()
|
||||||
|
->willReturnCallback($mockMethodBehavior);
|
||||||
|
$this->visitService->expects($this->exactly($expectedAllCalls))
|
||||||
|
->method('locateAllVisits')
|
||||||
|
->withAnyParameters()
|
||||||
|
->willReturnCallback($mockMethodBehavior);
|
||||||
|
$this->visitToLocation->expects(
|
||||||
|
$this->exactly($expectedUnlocatedCalls + $expectedEmptyCalls + $expectedAllCalls),
|
||||||
|
)->method('resolveVisitLocation')->withAnyParameters()->willReturn(Location::emptyInstance());
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
||||||
|
|
||||||
$this->commandTester->setInputs(['y']);
|
$this->commandTester->setInputs(['y']);
|
||||||
@ -98,12 +97,6 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
} else {
|
} else {
|
||||||
self::assertStringNotContainsString('Continue at your own', $output);
|
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
|
public function provideArgs(): iterable
|
||||||
@ -122,10 +115,12 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), Visitor::emptyInstance());
|
||||||
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
||||||
|
|
||||||
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
|
||||||
$this->invokeHelperMethods($visit, $location),
|
$this->visitService->expects($this->once())
|
||||||
);
|
->method('locateUnlocatedVisits')
|
||||||
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow($e);
|
->withAnyParameters()
|
||||||
|
->willReturnCallback($this->invokeHelperMethods($visit, $location));
|
||||||
|
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException($e);
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
||||||
|
|
||||||
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||||
@ -133,8 +128,6 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
self::assertStringContainsString('Processing IP', $output);
|
self::assertStringContainsString('Processing IP', $output);
|
||||||
self::assertStringContainsString($message, $output);
|
self::assertStringContainsString($message, $output);
|
||||||
$locateVisits->shouldHaveBeenCalledOnce();
|
|
||||||
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideIgnoredAddresses(): iterable
|
public function provideIgnoredAddresses(): iterable
|
||||||
@ -149,10 +142,12 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
|
$visit = Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', ''));
|
||||||
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
$location = VisitLocation::fromGeolocation(Location::emptyInstance());
|
||||||
|
|
||||||
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
|
||||||
$this->invokeHelperMethods($visit, $location),
|
$this->visitService->expects($this->once())
|
||||||
);
|
->method('locateUnlocatedVisits')
|
||||||
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any())->willThrow(
|
->withAnyParameters()
|
||||||
|
->willReturnCallback($this->invokeHelperMethods($visit, $location));
|
||||||
|
$this->visitToLocation->expects($this->once())->method('resolveVisitLocation')->willThrowException(
|
||||||
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
|
IpCannotBeLocatedException::forError(WrongIpException::fromIpAddress('1.2.3.4')),
|
||||||
);
|
);
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
||||||
@ -162,16 +157,11 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
||||||
self::assertStringContainsString('An error occurred while locating IP. Skipped', $output);
|
self::assertStringContainsString('An error occurred while locating IP. Skipped', $output);
|
||||||
$locateVisits->shouldHaveBeenCalledOnce();
|
|
||||||
$resolveIpLocation->shouldHaveBeenCalledOnce();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function invokeHelperMethods(Visit $visit, VisitLocation $location): callable
|
private function invokeHelperMethods(Visit $visit, VisitLocation $location): callable
|
||||||
{
|
{
|
||||||
return function (array $args) use ($visit, $location): void {
|
return static function (VisitGeolocationHelperInterface $helper) use ($visit, $location): void {
|
||||||
/** @var VisitGeolocationHelperInterface $helper */
|
|
||||||
[$helper] = $args;
|
|
||||||
|
|
||||||
$helper->geolocateVisit($visit);
|
$helper->geolocateVisit($visit);
|
||||||
$helper->onVisitLocated($location, $visit);
|
$helper->onVisitLocated($location, $visit);
|
||||||
};
|
};
|
||||||
@ -180,11 +170,10 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
/** @test */
|
/** @test */
|
||||||
public function noActionIsPerformedIfLockIsAcquired(): void
|
public function noActionIsPerformedIfLockIsAcquired(): void
|
||||||
{
|
{
|
||||||
$this->lock->acquire(false)->willReturn(false);
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(false);
|
||||||
|
|
||||||
$locateVisits = $this->visitService->locateUnlocatedVisits(Argument::cetera())->will(function (): void {
|
$this->visitService->expects($this->never())->method('locateUnlocatedVisits');
|
||||||
});
|
$this->visitToLocation->expects($this->never())->method('resolveVisitLocation');
|
||||||
$resolveIpLocation = $this->visitToLocation->resolveVisitLocation(Argument::any());
|
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
||||||
|
|
||||||
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
$this->commandTester->execute([], ['verbosity' => OutputInterface::VERBOSITY_VERBOSE]);
|
||||||
@ -194,25 +183,25 @@ class LocateVisitsCommandTest extends TestCase
|
|||||||
sprintf('Command "%s" is already in progress. Skipping.', LocateVisitsCommand::NAME),
|
sprintf('Command "%s" is already in progress. Skipping.', LocateVisitsCommand::NAME),
|
||||||
$output,
|
$output,
|
||||||
);
|
);
|
||||||
$locateVisits->shouldNotHaveBeenCalled();
|
|
||||||
$resolveIpLocation->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function showsProperMessageWhenGeoLiteUpdateFails(): void
|
public function showsProperMessageWhenGeoLiteUpdateFails(): void
|
||||||
{
|
{
|
||||||
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_FAILURE);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_FAILURE);
|
||||||
|
$this->visitService->expects($this->never())->method('locateUnlocatedVisits');
|
||||||
|
|
||||||
$this->commandTester->execute([]);
|
$this->commandTester->execute([]);
|
||||||
$output = $this->commandTester->getDisplay();
|
$output = $this->commandTester->getDisplay();
|
||||||
|
|
||||||
self::assertStringContainsString('It is not possible to locate visits without a GeoLite2 db file.', $output);
|
self::assertStringContainsString('It is not possible to locate visits without a GeoLite2 db file.', $output);
|
||||||
$this->visitService->locateUnlocatedVisits(Argument::cetera())->shouldNotHaveBeenCalled();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
public function providingAllFlagOnItsOwnDisplaysNotice(): void
|
public function providingAllFlagOnItsOwnDisplaysNotice(): void
|
||||||
{
|
{
|
||||||
|
$this->lock->method('acquire')->with($this->isFalse())->willReturn(true);
|
||||||
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
$this->downloadDbCommand->method('run')->withAnyParameters()->willReturn(ExitCodes::EXIT_SUCCESS);
|
||||||
|
|
||||||
$this->commandTester->execute(['--all' => true]);
|
$this->commandTester->execute(['--all' => true]);
|
||||||
|
Loading…
Reference in New Issue
Block a user