Fixed event handler not being properly registered as a service

This commit is contained in:
Alejandro Celaya 2019-07-13 15:43:54 +02:00
parent 91698034e7
commit 4380b62715
4 changed files with 40 additions and 4 deletions

View file

@ -15,8 +15,10 @@ return [
],
'dependencies' => [
'factories' => [
EventDispatcher\LocateShortUrlVisit::class => ConfigAbstractFactory::class,
],
],
ConfigAbstractFactory::class => [
EventDispatcher\LocateShortUrlVisit::class => [IpLocationResolverInterface::class, 'em', 'Logger_Shlink'],

View file

@ -5,6 +5,7 @@ namespace Shlinkio\Shlink\Core\EventDispatcher;
use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Core\Entity\Visit;
@ -42,9 +43,17 @@ class LocateShortUrlVisit
return;
}
try {
$location = $visit->isLocatable()
? $this->ipLocationResolver->resolveIpLocation($visit->getRemoteAddr())
: Location::emptyInstance();
} catch (WrongIpException $e) {
$this->logger->warning(
sprintf('Tried to locate visit with id "%s", but its address seems to be wrong. {e}', $visitId),
['e' => $e]
);
return;
}
$visit->locate(new VisitLocation($location));
$this->em->flush($visit);

View file

@ -8,6 +8,7 @@ use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Log\LoggerInterface;
use Shlinkio\Shlink\Common\Exception\WrongIpException;
use Shlinkio\Shlink\Common\IpGeolocation\IpLocationResolverInterface;
use Shlinkio\Shlink\Common\IpGeolocation\Model\Location;
use Shlinkio\Shlink\Common\Util\IpAddress;
@ -57,6 +58,29 @@ class LocateShortUrlVisitTest extends TestCase
$logWarning->shouldHaveBeenCalled();
}
/** @test */
public function invalidAddressLogsWarning(): void
{
$event = new ShortUrlVisited('123');
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
new Visit(new ShortUrl(''), new Visitor('', '', '1.2.3.4'))
);
$resolveLocation = $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow(
WrongIpException::class
);
$logWarning = $this->logger->warning(
Argument::containingString('Tried to locate visit with id "123", but its address seems to be wrong.'),
Argument::type('array')
);
($this->locateVisit)($event);
$findVisit->shouldHaveBeenCalledOnce();
$resolveLocation->shouldHaveBeenCalledOnce();
$logWarning->shouldHaveBeenCalled();
$this->em->flush(Argument::cetera())->shouldNotHaveBeenCalled();
}
/**
* @test
* @dataProvider provideNonLocatableVisits

View file

@ -2,3 +2,4 @@ parameters:
ignoreErrors:
- '#League\\Plates\\callback#'
- '#is not subtype of Throwable#'
- '#ObjectManager::flush()#'