Extended error handling on LocateVisit handler

This commit is contained in:
Alejandro Celaya
2021-04-07 12:53:53 +02:00
parent 5de706e0fe
commit c4718e7523
4 changed files with 62 additions and 41 deletions

View File

@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace ShlinkioTest\Shlink\Core\EventDispatcher;
use Doctrine\ORM\EntityManagerInterface;
use OutOfRangeException;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;
@@ -110,7 +111,7 @@ class LocateVisitTest extends TestCase
WrongIpException::class,
);
$logWarning = $this->logger->warning(
Argument::containingString('Tried to locate visit with id "{visitId}", but its address seems to be wrong.'),
'Tried to locate visit with id "{visitId}", but its address seems to be wrong. {e}',
Argument::type('array'),
);
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
@@ -125,6 +126,32 @@ class LocateVisitTest extends TestCase
$dispatch->shouldHaveBeenCalledOnce();
}
/** @test */
public function unhandledExceptionLogsError(): void
{
$event = new UrlVisited('123');
$findVisit = $this->em->find(Visit::class, '123')->willReturn(
Visit::forValidShortUrl(ShortUrl::createEmpty(), new Visitor('', '', '1.2.3.4', '')),
);
$resolveLocation = $this->ipLocationResolver->resolveIpLocation(Argument::cetera())->willThrow(
OutOfRangeException::class,
);
$logError = $this->logger->error(
'An unexpected error occurred while trying to locate visit with id "{visitId}". {e}',
Argument::type('array'),
);
$dispatch = $this->eventDispatcher->dispatch(new VisitLocated('123'))->will(function (): void {
});
($this->locateVisit)($event);
$findVisit->shouldHaveBeenCalledOnce();
$resolveLocation->shouldHaveBeenCalledOnce();
$logError->shouldHaveBeenCalled();
$this->em->flush()->shouldNotHaveBeenCalled();
$dispatch->shouldHaveBeenCalledOnce();
}
/**
* @test
* @dataProvider provideNonLocatableVisits