NotFoundRedirectHandlerTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. declare(strict_types=1);
  3. namespace ShlinkioTest\Shlink\Core\ErrorHandler;
  4. use Laminas\Diactoros\Response;
  5. use Laminas\Diactoros\ServerRequestFactory;
  6. use Laminas\Diactoros\Uri;
  7. use Mezzio\Router\Route;
  8. use Mezzio\Router\RouteResult;
  9. use PHPUnit\Framework\TestCase;
  10. use Psr\Http\Message\ServerRequestInterface;
  11. use Psr\Http\Server\MiddlewareInterface;
  12. use Psr\Http\Server\RequestHandlerInterface;
  13. use Shlinkio\Shlink\Core\Action\RedirectAction;
  14. use Shlinkio\Shlink\Core\ErrorHandler\NotFoundRedirectHandler;
  15. use Shlinkio\Shlink\Core\Options\NotFoundRedirectOptions;
  16. class NotFoundRedirectHandlerTest extends TestCase
  17. {
  18. private NotFoundRedirectHandler $middleware;
  19. private NotFoundRedirectOptions $redirectOptions;
  20. public function setUp(): void
  21. {
  22. $this->redirectOptions = new NotFoundRedirectOptions();
  23. $this->middleware = new NotFoundRedirectHandler($this->redirectOptions, '');
  24. }
  25. /**
  26. * @test
  27. * @dataProvider provideRedirects
  28. */
  29. public function expectedRedirectionIsReturnedDependingOnTheCase(
  30. ServerRequestInterface $request,
  31. string $expectedRedirectTo
  32. ): void {
  33. $this->redirectOptions->invalidShortUrl = 'invalidShortUrl';
  34. $this->redirectOptions->regular404 = 'regular404';
  35. $this->redirectOptions->baseUrl = 'baseUrl';
  36. $next = $this->prophesize(RequestHandlerInterface::class);
  37. $handle = $next->handle($request)->willReturn(new Response());
  38. $resp = $this->middleware->process($request, $next->reveal());
  39. $this->assertInstanceOf(Response\RedirectResponse::class, $resp);
  40. $this->assertEquals($expectedRedirectTo, $resp->getHeaderLine('Location'));
  41. $handle->shouldNotHaveBeenCalled();
  42. }
  43. public function provideRedirects(): iterable
  44. {
  45. yield 'base URL with trailing slash' => [
  46. ServerRequestFactory::fromGlobals()->withUri(new Uri('/')),
  47. 'baseUrl',
  48. ];
  49. yield 'base URL without trailing slash' => [
  50. ServerRequestFactory::fromGlobals()->withUri(new Uri('')),
  51. 'baseUrl',
  52. ];
  53. yield 'regular 404' => [
  54. ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo/bar')),
  55. 'regular404',
  56. ];
  57. yield 'invalid short URL' => [
  58. ServerRequestFactory::fromGlobals()
  59. ->withAttribute(
  60. RouteResult::class,
  61. RouteResult::fromRoute(
  62. new Route(
  63. '',
  64. $this->prophesize(MiddlewareInterface::class)->reveal(),
  65. ['GET'],
  66. RedirectAction::class,
  67. ),
  68. ),
  69. )
  70. ->withUri(new Uri('/abc123')),
  71. 'invalidShortUrl',
  72. ];
  73. }
  74. /** @test */
  75. public function nextMiddlewareIsInvokedWhenNotRedirectNeedsToOccur(): void
  76. {
  77. $req = ServerRequestFactory::fromGlobals();
  78. $resp = new Response();
  79. $next = $this->prophesize(RequestHandlerInterface::class);
  80. $handle = $next->handle($req)->willReturn($resp);
  81. $result = $this->middleware->process($req, $next->reveal());
  82. $this->assertSame($resp, $result);
  83. $handle->shouldHaveBeenCalledOnce();
  84. }
  85. }