Skip to content

Commit

Permalink
Replace HTTP status codes with StatusCodeInterface constants
Browse files Browse the repository at this point in the history
This is a more meaningful, easier to maintain approach.
  • Loading branch information
settermjd committed Jan 11, 2025
1 parent aabc83b commit 9959f4d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
4 changes: 3 additions & 1 deletion config/sample.redirecttonewdomainmiddleware.global.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Fig\Http\Message\StatusCodeInterface;

return [
"redirect-to-new-domain-middleware" => [
/**
Expand All @@ -18,6 +20,6 @@
* This is the type of redirect, which can be set to either 301 (the
* default) or 302.
*/
"status" => 301,
"status" => StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
],
];
11 changes: 7 additions & 4 deletions src/Middleware/RedirectToNewDomainMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Settermjd\Middleware;

use Fig\Http\Message\StatusCodeInterface;
use Laminas\Diactoros\Response\RedirectResponse;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -23,7 +24,7 @@
*/
final readonly class RedirectToNewDomainMiddleware implements MiddlewareInterface
{
public const int DEFAULT_STATUS = 301;
public const int DEFAULT_STATUS = StatusCodeInterface::STATUS_MOVED_PERMANENTLY;

public function __construct(
/**
Expand Down Expand Up @@ -65,9 +66,11 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
?->debug(sprintf("redirecting to: %s", $newRequestUri));
return new RedirectResponse(
$newRequestUri,
! in_array($this->redirectStatus, [301, 302])
? self::DEFAULT_STATUS
: $this->redirectStatus
! in_array($this->redirectStatus, [
StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
StatusCodeInterface::STATUS_FOUND,
]) ? self::DEFAULT_STATUS
: $this->redirectStatus
);
}

Expand Down
3 changes: 2 additions & 1 deletion test/Middleware/RedirectToNewDomainMiddlewareFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Settermjd\MiddlewareTest;

use Fig\Http\Message\StatusCodeInterface;
use PHPUnit\Framework\Attributes\TestWith;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
Expand Down Expand Up @@ -31,7 +32,7 @@ public function testCanInstantiateMiddlewareWithConfigDataWhenSet(): void
"redirect-to-new-domain-middleware" => [
"old" => "deploywithdockercompose.com",
"new" => "https://deploywithdockercompose.webdevwithmatt.com",
"status" => 301,
"status" => StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
],
],
$this->createMock(LoggerInterface::class),
Expand Down
18 changes: 14 additions & 4 deletions test/Middleware/RedirectToNewDomainMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Settermjd\MiddlewareTest;

use Fig\Http\Message\StatusCodeInterface;
use Laminas\Diactoros\Response\EmptyResponse;
use Laminas\Diactoros\Response\RedirectResponse;
use Laminas\Diactoros\Uri;
Expand Down Expand Up @@ -89,7 +90,7 @@ function ($parameters) use ($invokedCount, $originalRequest, $newRequest) {

self::assertInstanceOf(RedirectResponse::class, $response);
self::assertSame($newRequest, $response->getHeaderLine("location"));
self::assertSame(301, $response->getStatusCode());
self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
}

#[TestWith(['https://deploywithdockercompose.webdevwithmatt.com'])]
Expand Down Expand Up @@ -157,9 +158,18 @@ public function testLogsRequestedDomainIfLoggerIsPresent(string $originalRequest
$middleware->process($request, $handler);
}

#[TestWith([301, 301])]
#[TestWith([302, 302])]
#[TestWith([303, 301])]
#[TestWith([
StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
])]
#[TestWith([
StatusCodeInterface::STATUS_FOUND,
StatusCodeInterface::STATUS_FOUND,
])]
#[TestWith([
StatusCodeInterface::STATUS_SEE_OTHER,
StatusCodeInterface::STATUS_MOVED_PERMANENTLY,
])]
public function testCanSetRedirectStatusCodeTo301Or302(int $desiredStatus, int $actualStatus): void
{
$middleware = new RedirectToNewDomainMiddleware(
Expand Down

0 comments on commit 9959f4d

Please sign in to comment.