Ensured IP address is resolved when tracking orphan visits

This commit is contained in:
Alejandro Celaya
2021-02-09 17:28:06 +01:00
committed by Alejandro Celaya
parent 5278d7668c
commit b01487ac91
3 changed files with 32 additions and 13 deletions

View File

@@ -7,27 +7,29 @@ namespace ShlinkioTest\Shlink\Core\ErrorHandler;
use Closure;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Uri;
use Mezzio\Router\Route;
use Mezzio\Router\RouteResult;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Shlinkio\Shlink\Core\Action\RedirectAction;
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTemplateHandler;
class NotFoundTemplateHandlerTest extends TestCase
{
private NotFoundTemplateHandler $handler;
private Closure $readFile;
private bool $readFileCalled;
public function setUp(): void
{
$this->readFileCalled = false;
$this->readFile = function (string $fileName): string {
$readFile = function (string $fileName): string {
$this->readFileCalled = true;
return $fileName;
};
$this->handler = new NotFoundTemplateHandler($this->readFile);
$this->handler = new NotFoundTemplateHandler($readFile);
}
/**
@@ -45,15 +47,29 @@ class NotFoundTemplateHandlerTest extends TestCase
public function provideTemplates(): iterable
{
$request = ServerRequestFactory::fromGlobals();
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo'));
yield [$request, NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
yield [
$request->withAttribute(
yield 'base url' => [$this->withNotFoundType($request, '/foo'), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
yield 'regular not found' => [$this->withNotFoundType($request), NotFoundTemplateHandler::NOT_FOUND_TEMPLATE];
yield 'invalid short code' => [
$this->withNotFoundType($request->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('', $this->prophesize(MiddlewareInterface::class)->reveal())),
),
RouteResult::fromRoute(
new Route(
'',
$this->prophesize(MiddlewareInterface::class)->reveal(),
['GET'],
RedirectAction::class,
),
),
)),
NotFoundTemplateHandler::INVALID_SHORT_CODE_TEMPLATE,
];
}
private function withNotFoundType(ServerRequestInterface $req, string $baseUrl = ''): ServerRequestInterface
{
$type = NotFoundType::fromRequest($req, $baseUrl);
return $req->withAttribute(NotFoundType::class, $type);
}
}