2021-02-08 15:16:15 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ShlinkioTest\Shlink\Core\ErrorHandler;
|
|
|
|
|
2024-11-24 07:05:33 -06:00
|
|
|
use Laminas\Diactoros\Response;
|
2021-02-08 15:16:15 -06:00
|
|
|
use Laminas\Diactoros\ServerRequestFactory;
|
2024-11-24 07:05:33 -06:00
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
2023-02-09 13:42:18 -06:00
|
|
|
use PHPUnit\Framework\Attributes\Test;
|
2022-10-21 12:29:02 -05:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2021-02-08 15:16:15 -06:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Psr\Http\Server\RequestHandlerInterface;
|
|
|
|
use Shlinkio\Shlink\Core\ErrorHandler\Model\NotFoundType;
|
|
|
|
use Shlinkio\Shlink\Core\ErrorHandler\NotFoundTrackerMiddleware;
|
2021-07-15 10:23:09 -05:00
|
|
|
use Shlinkio\Shlink\Core\Visit\RequestTrackerInterface;
|
2021-02-08 15:16:15 -06:00
|
|
|
|
2024-11-24 07:05:33 -06:00
|
|
|
use const Shlinkio\Shlink\REDIRECT_URL_REQUEST_ATTRIBUTE;
|
|
|
|
|
2021-02-08 15:16:15 -06:00
|
|
|
class NotFoundTrackerMiddlewareTest extends TestCase
|
|
|
|
{
|
|
|
|
private NotFoundTrackerMiddleware $middleware;
|
|
|
|
private ServerRequestInterface $request;
|
2022-10-24 12:53:13 -05:00
|
|
|
private MockObject & RequestHandlerInterface $handler;
|
|
|
|
private MockObject & RequestTrackerInterface $requestTracker;
|
2021-02-08 15:16:15 -06:00
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
{
|
2022-10-21 12:29:02 -05:00
|
|
|
$this->handler = $this->createMock(RequestHandlerInterface::class);
|
|
|
|
$this->requestTracker = $this->createMock(RequestTrackerInterface::class);
|
|
|
|
$this->middleware = new NotFoundTrackerMiddleware($this->requestTracker);
|
2021-02-08 15:16:15 -06:00
|
|
|
|
|
|
|
$this->request = ServerRequestFactory::fromGlobals()->withAttribute(
|
|
|
|
NotFoundType::class,
|
2022-10-21 12:29:02 -05:00
|
|
|
$this->createMock(NotFoundType::class),
|
2021-02-08 15:16:15 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-24 07:05:33 -06:00
|
|
|
#[Test, DataProvider('provideResponses')]
|
|
|
|
public function delegatesIntoRequestTracker(Response $resp, string|null $expectedRedirectUrl): void
|
2021-02-08 15:16:15 -06:00
|
|
|
{
|
2024-11-24 07:05:33 -06:00
|
|
|
$this->handler->expects($this->once())->method('handle')->with($this->request)->willReturn($resp);
|
|
|
|
$this->requestTracker->expects($this->once())->method('trackNotFoundIfApplicable')->with(
|
|
|
|
$this->request->withAttribute(REDIRECT_URL_REQUEST_ATTRIBUTE, $expectedRedirectUrl),
|
|
|
|
);
|
|
|
|
|
|
|
|
$result = $this->middleware->process($this->request, $this->handler);
|
2021-02-08 15:16:15 -06:00
|
|
|
|
2024-11-24 07:05:33 -06:00
|
|
|
self::assertSame($resp, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function provideResponses(): iterable
|
|
|
|
{
|
|
|
|
yield 'no location response' => [new Response(), null];
|
|
|
|
yield 'location response' => [new Response\RedirectResponse('the_location'), 'the_location'];
|
2021-02-08 15:16:15 -06:00
|
|
|
}
|
|
|
|
}
|