shlink/module/Core/test/ErrorHandler/NotFoundTrackerMiddlewareTest.php

59 lines
2.2 KiB
PHP
Raw Normal View History

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;
use PHPUnit\Framework\Attributes\Test;
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;
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
{
$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,
$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
}
}