Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/Adapters/AdapterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
namespace Osteel\OpenApi\Testing\Adapters;

use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

interface AdapterInterface
{
/**
* Convert a HTTP message to a PSR-7 HTTP message.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to convert
* @param object $message the HTTP message to convert
*/
public function convert(Request|Response|ResponseInterface|ServerRequestInterface $message): MessageInterface;
public function convert(object $message): MessageInterface;
}
11 changes: 8 additions & 3 deletions src/Adapters/HttpFoundationAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Osteel\OpenApi\Testing\Adapters;

use InvalidArgumentException;
use Nyholm\Psr7\Factory\Psr17Factory;
use Psr\Http\Message\MessageInterface;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -17,9 +18,9 @@ final class HttpFoundationAdapter implements AdapterInterface
/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to convert
* @param object $message the HTTP message to convert
*/
public function convert(Request|Response|ResponseInterface|ServerRequestInterface $message): MessageInterface
public function convert(object $message): MessageInterface
{
if ($message instanceof ResponseInterface || $message instanceof ServerRequestInterface) {
return $message;
Expand All @@ -32,6 +33,10 @@ public function convert(Request|Response|ResponseInterface|ServerRequestInterfac
return $psrHttpFactory->createResponse($message);
}

return $psrHttpFactory->createRequest($message);
if ($message instanceof Request) {
return $psrHttpFactory->createRequest($message);
}

throw new InvalidArgumentException(sprintf('Unsupported %s object received', $message::class));
}
}
64 changes: 29 additions & 35 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
use Osteel\OpenApi\Testing\Adapters\AdapterInterface;
use Osteel\OpenApi\Testing\Exceptions\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

final class Validator implements ValidatorInterface
{
Expand All @@ -27,17 +24,14 @@ public function __construct(
/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param string $method the HTTP method
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param string $method the HTTP method
*
* @throws ValidationException
*/
public function validate(
Request|Response|ResponseInterface|ServerRequestInterface $message,
string $path,
string $method,
): bool {
public function validate(object $message, string $path, string $method): bool
{
$message = $this->adapter->convert($message);
$operation = $this->getOperationAddress($path, $method);
$validator = $message instanceof ResponseInterface ? $this->responseValidator : $this->requestValidator;
Expand Down Expand Up @@ -68,103 +62,103 @@ private function getOperationAddress(string $path, string $method): OperationAdd
/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function delete(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function delete(object $message, string $path): bool
{
return $this->validate($message, $path, 'delete');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function get(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function get(object $message, string $path): bool
{
return $this->validate($message, $path, 'get');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function head(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function head(object $message, string $path): bool
{
return $this->validate($message, $path, 'head');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function options(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function options(object $message, string $path): bool
{
return $this->validate($message, $path, 'options');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function patch(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function patch(object $message, string $path): bool
{
return $this->validate($message, $path, 'patch');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function post(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function post(object $message, string $path): bool
{
return $this->validate($message, $path, 'post');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function put(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function put(object $message, string $path): bool
{
return $this->validate($message, $path, 'put');
}

/**
* @inheritDoc
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function trace(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool
public function trace(object $message, string $path): bool
{
return $this->validate($message, $path, 'trace');
}
Expand Down
60 changes: 28 additions & 32 deletions src/ValidatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,101 +6,97 @@

use League\OpenAPIValidation\PSR7\Exception\ValidationFailed;
use Osteel\OpenApi\Testing\Exceptions\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

interface ValidatorInterface
{
/**
* Validate a HTTP message against the specified OpenAPI definition.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param string $method the HTTP method
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param string $method the HTTP method
*
* @throws ValidationException
*/
public function validate(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path, string $method): bool;
public function validate(object $message, string $path, string $method): bool;

/**
* Validate a HTTP message for a DELETE operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function delete(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function delete(object $message, string $path): bool;

/**
* Validate a HTTP message for a GET operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function get(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function get(object $message, string $path): bool;

/**
* Validate a HTTP message for a HEAD operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function head(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function head(object $message, string $path): bool;

/**
* Validate a HTTP message for a OPTIONS operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function options(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function options(object $message, string $path): bool;

/**
* Validate a HTTP message for a PATCH operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function patch(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function patch(object $message, string $path): bool;

/**
* Validate a HTTP message for a POST operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function post(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function post(object $message, string $path): bool;

/**
* Validate a HTTP message for a PUT operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function put(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function put(object $message, string $path): bool;

/**
* Validate a HTTP message for a TRACE operation on the provided OpenAPI definition path.
*
* @param Request|Response|ResponseInterface|ServerRequestInterface $message the HTTP message to validate
* @param string $path the OpenAPI path
* @param object $message the HTTP message to validate
* @param string $path the OpenAPI path
*
* @throws ValidationFailed
*/
public function trace(Request|Response|ResponseInterface|ServerRequestInterface $message, string $path): bool;
public function trace(object $message, string $path): bool;
}
9 changes: 9 additions & 0 deletions tests/Adapters/HttpFoundationAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Osteel\OpenApi\Testing\Tests\Adapters;

use InvalidArgumentException;
use Osteel\OpenApi\Testing\Adapters\HttpFoundationAdapter;
use Osteel\OpenApi\Testing\Tests\TestCase;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -20,6 +21,14 @@ protected function setUp(): void
$this->sut = new HttpFoundationAdapter();
}

public function test_it_does_not_convert_the_message_because_the_type_is_not_supported()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unsupported InvalidArgumentException object received');

$this->sut->convert(new InvalidArgumentException());
}

public function test_it_converts_the_http_foundation_request()
{
$result = $this->sut->convert(Request::create('/foo'));
Expand Down