Skip to content

Commit

Permalink
Replace status-code dependency
Browse files Browse the repository at this point in the history
This allows http-message 2 upgrade.
  • Loading branch information
usox committed Nov 12, 2023
1 parent ec06ba8 commit 1d572a9
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 103 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"psr/http-server-handler": "^1.0",
"psr/log": "^1.1||^3",
"ramsey/uuid": "^4.1",
"shrikeh/teapot": "^2.3"
"teapot/status-code": "^1.1"
},
"autoload": {
"psr-4": {
Expand Down
75 changes: 9 additions & 66 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/Dispatch/MethodDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use stdClass;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Contract\ApiMethodInterface;
use Usox\JsonSchemaApi\Contract\MethodProviderInterface;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
Expand Down Expand Up @@ -65,7 +65,7 @@ public function dispatch(
if (!$handler instanceof ApiMethodInterface) {
throw new MethodNotFoundException(
'Method not found',
StatusCode::BAD_REQUEST
Http::BAD_REQUEST
);
}

Expand Down
6 changes: 3 additions & 3 deletions src/Dispatch/MethodValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Opis\JsonSchema\Helper;
use Opis\JsonSchema\Validator;
use stdClass;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Exception\RequestMalformedException;
use Usox\JsonSchemaApi\Exception\ResponseMalformedException;

Expand Down Expand Up @@ -40,7 +40,7 @@ public function validateInput(
if (!$validationResult->isValid()) {
throw new RequestMalformedException(
'Bad Request',
StatusCode::BAD_REQUEST
Http::BAD_REQUEST
);
}
}
Expand Down Expand Up @@ -78,7 +78,7 @@ public function validateOutput(
if ($error !== null) {
throw new ResponseMalformedException(
'Internal Server Error',
StatusCode::INTERNAL_SERVER_ERROR,
Http::INTERNAL_SERVER_ERROR,
null,
$this->errorFormatter->format($error)
);
Expand Down
6 changes: 3 additions & 3 deletions src/Dispatch/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Usox\JsonSchemaApi\Dispatch;

use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
use Opis\JsonSchema\Validator;
use Psr\Http\Message\ServerRequestInterface;
use stdClass;
use Teapot\StatusCode;
use Usox\JsonSchemaApi\Dispatch\Exception\JsonInvalidException;
use Usox\JsonSchemaApi\Exception\RequestMalformedException;

Expand Down Expand Up @@ -40,7 +40,7 @@ public function validate(ServerRequestInterface $request): stdClass
if (json_last_error() !== JSON_ERROR_NONE) {
throw new JsonInvalidException(
sprintf('Input is no valid json (%s)', json_last_error_msg()),
StatusCode::BAD_REQUEST
Http::BAD_REQUEST
);
}

Expand All @@ -57,7 +57,7 @@ public function validate(ServerRequestInterface $request): stdClass
if (!$validationResult->isValid()) {
throw new RequestMalformedException(
'Request is invalid',
StatusCode::BAD_REQUEST
Http::BAD_REQUEST
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/Dispatch/SchemaLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Usox\JsonSchemaApi\Dispatch;

use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException;
use stdClass;
use Teapot\StatusCode;
use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException;

/**
Expand All @@ -28,7 +28,7 @@ public function load(
if (!file_exists($schemaFilePath)) {
throw new SchemaNotFoundException(
sprintf('Schema file `%s` not found', $schemaFilePath),
StatusCode::INTERNAL_SERVER_ERROR
Http::INTERNAL_SERVER_ERROR
);
}

Expand All @@ -37,7 +37,7 @@ public function load(
if ($fileContent === false) {
throw new SchemaNotLoadableException(
sprintf('Schema file `%s` not loadable', $schemaFilePath),
StatusCode::INTERNAL_SERVER_ERROR
Http::INTERNAL_SERVER_ERROR
);
}

Expand All @@ -47,7 +47,7 @@ public function load(
if (json_last_error() !== JSON_ERROR_NONE) {
throw new SchemaInvalidException(
sprintf('Schema does not contain valid json (%s)', json_last_error_msg()),
StatusCode::INTERNAL_SERVER_ERROR
Http::INTERNAL_SERVER_ERROR
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidFactoryInterface;
use Ramsey\Uuid\UuidInterface;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Throwable;
use Usox\JsonSchemaApi\Contract\MethodProviderInterface;
use Usox\JsonSchemaApi\Dispatch\MethodDispatcher;
Expand Down Expand Up @@ -52,7 +52,7 @@ public function __construct(
public function serve(
ServerRequestInterface $request,
): ResponseInterface {
$statusCode = StatusCode::OK;
$statusCode = Http::OK;
$responseData = null;

try {
Expand All @@ -71,22 +71,22 @@ public function serve(
// Build an error response
$responseData = $this->responseBuilder->buildErrorResponse($e, $uuid);

$statusCode = StatusCode::BAD_REQUEST;
$statusCode = Http::BAD_REQUEST;
} catch (InternalException $e) {
$this->logError(
$e,
$this->uuidFactory->uuid4(),
$e->getContext()
);

$statusCode = StatusCode::INTERNAL_SERVER_ERROR;
$statusCode = Http::INTERNAL_SERVER_ERROR;
} catch (Throwable $e) {
$this->logError(
$e,
$this->uuidFactory->uuid4()
);

$statusCode = StatusCode::INTERNAL_SERVER_ERROR;
$statusCode = Http::INTERNAL_SERVER_ERROR;
}

$response = $this->responseFactory->createResponse($statusCode);
Expand Down
4 changes: 2 additions & 2 deletions tests/Dispatch/MethodDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Mockery\MockInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Log\LoggerInterface;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Contract\ApiMethodInterface;
use Usox\JsonSchemaApi\Contract\MethodProviderInterface;
use Usox\JsonSchemaApi\Exception\MethodNotFoundException;
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testDispatchThrowsExceptionIfMethodDoesNotExist(): void

$this->expectException(MethodNotFoundException::class);
$this->expectExceptionMessage('Method not found');
$this->expectExceptionCode(StatusCode::BAD_REQUEST);
$this->expectExceptionCode(Http::BAD_REQUEST);

$method = 'some-method';
$parameter = (object) ['some-parameter'];
Expand Down
6 changes: 3 additions & 3 deletions tests/Dispatch/MethodValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Opis\JsonSchema\Errors\ValidationError;
use Opis\JsonSchema\ValidationResult;
use Opis\JsonSchema\Validator;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Exception\RequestMalformedException;
use Usox\JsonSchemaApi\Exception\ResponseMalformedException;

Expand Down Expand Up @@ -46,7 +46,7 @@ public function testValidateInputThrowsExceptionIfInputDoesNotValidate(): void

$this->expectException(RequestMalformedException::class);
$this->expectExceptionMessage('Bad Request');
$this->expectExceptionCode(StatusCode::BAD_REQUEST);
$this->expectExceptionCode(Http::BAD_REQUEST);

$this->schemaValidator->shouldReceive('validate')
->with(
Expand Down Expand Up @@ -111,7 +111,7 @@ public function testValidateOutputThrowsExceptionIfOutputDoesNotValidate(): void

$this->expectException(ResponseMalformedException::class);
$this->expectExceptionMessage('Internal Server Error');
$this->expectExceptionCode(StatusCode::INTERNAL_SERVER_ERROR);
$this->expectExceptionCode(Http::INTERNAL_SERVER_ERROR);

$this->schemaValidator->shouldReceive('validate')
->with(
Expand Down
6 changes: 3 additions & 3 deletions tests/Dispatch/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Opis\JsonSchema\Validator;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamInterface;
use Teapot\StatusCode;
use Teapot\StatusCode\Http;
use Usox\JsonSchemaApi\Dispatch\Exception\JsonInvalidException;
use Usox\JsonSchemaApi\Exception\RequestMalformedException;

Expand Down Expand Up @@ -40,7 +40,7 @@ public function testValidateThrowsExceptionIfInputIsInvalid(): void
{
$this->expectException(JsonInvalidException::class);
$this->expectExceptionMessage('Input is no valid json (Syntax error)');
$this->expectExceptionCode(StatusCode::BAD_REQUEST);
$this->expectExceptionCode(Http::BAD_REQUEST);

$stream = Mockery::mock(StreamInterface::class);
$request = Mockery::mock(ServerRequestInterface::class);
Expand All @@ -64,7 +64,7 @@ public function testValidateThrowsExceptionIfInputDoesNotValidate(): void
{
$this->expectException(RequestMalformedException::class);
$this->expectExceptionMessage('Request is invalid');
$this->expectExceptionCode(StatusCode::BAD_REQUEST);
$this->expectExceptionCode(Http::BAD_REQUEST);

$stream = Mockery::mock(StreamInterface::class);
$request = Mockery::mock(ServerRequestInterface::class);
Expand Down
Loading

0 comments on commit 1d572a9

Please sign in to comment.