refactor: remove status_code from response classes and update related tests
Some checks failed
🧪✨ Tests Workflow / 🛡️ 🔒 Library Audit (push) Successful in 3m5s
🧪✨ Tests Workflow / 📝 ✨ Code Lint (push) Failing after 2m56s
🧪✨ Tests Workflow / 🛡️ 🔒 License Check (push) Successful in 3m12s
🧪✨ Tests Workflow / 🧪 ✨ Database Migrations (push) Successful in 3m32s
🧪✨ Tests Workflow / 🐙 🔍 Code Sniffer (push) Successful in 3m17s
🧪✨ Tests Workflow / 🧪 ✅ Unit Tests (push) Successful in 1m42s

This commit is contained in:
2025-12-10 08:45:51 -05:00
parent f59dcb2dcc
commit 84c3b392ba
9 changed files with 27 additions and 22 deletions

View File

@@ -82,6 +82,11 @@ composer-install: ## Install PHP dependencies in the composer runtime container
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
$(COMPOSER) "composer install --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs"
composer-install-no-dev: ## Install PHP dependencies without dev packages in the composer runtime container
@printf "$(COMPOSE) $(GREEN)Installing PHP dependencies (no-dev) in $(COMPOSER_RUNTIME)$(RESET)\n"
@$(DOCKER) up $(COMPOSER_RUNTIME) -d --no-recreate
$(COMPOSER) "composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader --ignore-platform-reqs"
composer-require: ## Require a PHP package in the composer runtime container (usage: make composer-require package=vendor/package)
ifndef package
$(error package variable is required: make composer-require package=vendor/package)

View File

@@ -9,7 +9,9 @@ use Nyholm\Psr7\ServerRequest;
use Psr\Http\Message\ResponseInterface;
use Siteworxpro\App\Http\JsonResponseFactory;
use Siteworxpro\App\Http\Responses\GenericResponse;
use Siteworxpro\App\Http\Responses\ServerErrorResponse;
use Siteworxpro\App\Models\Model;
use Siteworxpro\App\Services\Facades\Logger;
use Siteworxpro\App\Services\Facades\Redis;
use Siteworxpro\HttpStatus\CodesEnum;
use OpenApi\Attributes as OA;
@@ -43,18 +45,19 @@ class HealthcheckController extends Controller
throw new \Exception('Redis ping failed');
}
} catch (\Exception $e) {
Logger::emergency(
'Healthcheck failed: ' . $e->getMessage(),
['exception' => $e]
);
return JsonResponseFactory::createJsonResponse(
[
'status_code' => CodesEnum::SERVICE_UNAVAILABLE->value,
'message' => 'Healthcheck Failed',
'error' => $e->getMessage(),
],
new ServerErrorResponse($e),
CodesEnum::SERVICE_UNAVAILABLE
);
}
return JsonResponseFactory::createJsonResponse(
new GenericResponse('Healthcheck OK', CodesEnum::OK->value)
new GenericResponse('Healthcheck OK')
);
}
}

View File

@@ -16,7 +16,6 @@ class UnauthorizedResponse extends OA\Response
mediaType: 'application/json',
schema: new OA\Schema(
properties: [
new OA\Property(property: 'status_code', type: 'integer', example: 401),
new OA\Property(property: 'message', type: 'string', example: 'Unauthorized'),
]
)

View File

@@ -11,14 +11,12 @@ use OpenApi\Attributes as OA;
schema: 'GenericResponse',
properties: [
new OA\Property(property: 'message', type: 'string', example: 'Operation completed successfully.'),
new OA\Property(property: 'status_code', type: 'integer', example: 200),
]
)]
readonly class GenericResponse implements Arrayable
{
public function __construct(
private string $message = '',
private int $statusCode = 200
) {
}
@@ -26,7 +24,6 @@ readonly class GenericResponse implements Arrayable
{
return [
'message' => $this->message,
'status_code' => $this->statusCode,
];
}
}

View File

@@ -14,7 +14,6 @@ use OpenApi\Attributes as OA;
type: 'string',
example: 'The requested resource /api/resource was not found.'
),
new OA\Property(property: 'status_code', type: 'integer', example: 404),
new OA\Property(
property: 'context',
description: 'Additional context about the not found error.',
@@ -32,7 +31,6 @@ readonly class NotFoundResponse implements Arrayable
public function toArray(): array
{
return [
'status_code' => CodesEnum::NOT_FOUND->value,
'message' => 'The requested resource ' . $this->uri . ' was not found.',
'context' => $this->context,
];

View File

@@ -11,7 +11,7 @@ use OpenApi\Attributes as OA;
schema: 'ServerErrorResponse',
properties: array(
new OA\Property(property: 'message', type: 'string', example: 'An internal server error occurred.'),
new OA\Property(property: 'status_code', type: 'integer', example: 500),
new OA\Property(property: 'code', type: 'integer', example: 500),
new OA\Property(
property: 'file',
type: 'string',
@@ -35,7 +35,7 @@ readonly class ServerErrorResponse implements Arrayable
{
if (Config::get('app.dev_mode')) {
return [
'status_code' => $this->e->getCode() != 0 ?
'code' => $this->e->getCode() != 0 ?
$this->e->getCode() :
CodesEnum::INTERNAL_SERVER_ERROR->value,
'message' => $this->e->getMessage(),
@@ -47,7 +47,7 @@ readonly class ServerErrorResponse implements Arrayable
}
return [
'status_code' => $this->e->getCode() != 0 ?
'code' => $this->e->getCode() != 0 ?
$this->e->getCode() :
CodesEnum::INTERNAL_SERVER_ERROR->value,
'message' => 'An internal server error occurred.',

View File

@@ -20,7 +20,7 @@ class IndexControllerTest extends AbstractController
$response = $controller->get($this->getMockRequest());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('{"message":"Server is running","status_code":200}', (string)$response->getBody());
$this->assertEquals('{"message":"Server is running"}', (string)$response->getBody());
}
/**
@@ -35,6 +35,6 @@ class IndexControllerTest extends AbstractController
$response = $controller->post($this->getMockRequest());
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('{"message":"POST request received","status_code":200}', (string)$response->getBody());
$this->assertEquals('{"message":"POST request received"}', (string)$response->getBody());
}
}

View File

@@ -12,7 +12,6 @@ class NotFoundResponseTest extends Unit
$response = new NotFoundResponse('/api/resource', ['key' => 'value']);
$expected = [
'status_code' => 404,
'message' => 'The requested resource /api/resource was not found.',
'context' => ['key' => 'value'],
];

View File

@@ -19,7 +19,7 @@ class ServerErrorResponseTest extends Unit
$response = new ServerErrorResponse($e, ['operation' => 'data_processing']);
$expected = [
'status_code' => 500,
'code' => 500,
'message' => 'A Test Error occurred.',
'context' => [
'operation' => 'data_processing'
@@ -35,13 +35,15 @@ class ServerErrorResponseTest extends Unit
public function testToArrayNotInDevMode(): void
{
Config::set('app.dev_mode', false);
try {
throw new \Exception('A Test Error occurred.');
} catch (\Exception $exception) {
$response = new ServerErrorResponse($exception);
$expected = [
'status_code' => 500,
'code' => 500,
'message' => 'An internal server error occurred.',
];
@@ -51,13 +53,15 @@ class ServerErrorResponseTest extends Unit
public function testToArrayIfCodeIsSet(): void
{
Config::set('app.dev_mode', false);
try {
throw new \Exception('A Test Error occurred.', 1234);
} catch (\Exception $exception) {
$response = new ServerErrorResponse($exception);
$expected = [
'status_code' => 1234,
'code' => 1234,
'message' => 'An internal server error occurred.',
];
@@ -75,7 +79,7 @@ class ServerErrorResponseTest extends Unit
$response = new ServerErrorResponse($exception);
$expected = [
'status_code' => 1234,
'code' => 1234,
'message' => 'A Test Error occurred.',
'file' => $exception->getFile(),
'line' => $exception->getLine(),