From 195d2f9d1fdbdc3549c3633c2b4018b25c588376 Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Wed, 17 Jan 2024 07:54:57 -0600 Subject: [PATCH 1/2] Delete .styleci.yml --- .styleci.yml | 17 ----------------- 1 file changed, 17 deletions(-) delete mode 100644 .styleci.yml diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index 11dd67b..0000000 --- a/.styleci.yml +++ /dev/null @@ -1,17 +0,0 @@ -php: - preset: laravel - finder: - not-name: - - index.php - - server.php - enabled: - - fully_qualified_strict_types -js: - finder: - not-name: - - webpack.mix.js - exclude: - - public - not-path: - - dist -css: true \ No newline at end of file From 6b531a9cab2d3a90e52587bcc56484889e7c115d Mon Sep 17 00:00:00 2001 From: Adam Campbell Date: Wed, 17 Jan 2024 08:55:20 -0500 Subject: [PATCH 2/2] Apply fixes from StyleCI (#162) --- _ide_helpers.php | 30 +++---- config/spectator.php | 12 +-- src/Assertions.php | 2 +- src/Exceptions/SchemaValidationException.php | 55 ++++++------ src/Middleware.php | 41 +++++---- src/RequestFactory.php | 47 ++++++----- src/Spectator.php | 28 +++---- src/Validation/AbstractValidator.php | 19 +++-- src/Validation/RequestValidator.php | 33 ++++---- src/Validation/ResponseValidator.php | 14 ++-- tests/AssertionsTest.php | 14 ++-- tests/MiddlewareTest.php | 4 +- tests/RequestValidatorTest.php | 88 ++++++++++---------- tests/ResponseValidatorTest.php | 84 +++++++++---------- 14 files changed, 249 insertions(+), 222 deletions(-) diff --git a/_ide_helpers.php b/_ide_helpers.php index 7748f20..787d16e 100644 --- a/_ide_helpers.php +++ b/_ide_helpers.php @@ -8,14 +8,14 @@ /** * @see \Spectator\Assertions * - * @method static assertValidRequest() - * @method static assertInvalidRequest() - * @method static assertValidResponse($status = null) - * @method static assertInvalidResponse($status = null) - * @method static assertValidationMessage($expected) - * @method static assertErrorsContain($errors) - * @method static dumpSpecErrors() - * @method static void skipRequestValidation() + * @method static assertValidRequest() + * @method static assertInvalidRequest() + * @method static assertValidResponse($status = null) + * @method static assertInvalidResponse($status = null) + * @method static assertValidationMessage($expected) + * @method static assertErrorsContain($errors) + * @method static dumpSpecErrors() + * @method static void skipRequestValidation() */ class TestResponse { @@ -27,13 +27,13 @@ class TestResponse /** * @see \Spectator\Assertions * - * @method static assertValidRequest() - * @method static assertInvalidRequest() - * @method static assertValidResponse($status = null) - * @method static assertInvalidResponse($status = null) - * @method static assertValidationMessage($expected) - * @method static assertErrorsContain($errors) - * @method static void skipRequestValidation() + * @method static assertValidRequest() + * @method static assertInvalidRequest() + * @method static assertValidResponse($status = null) + * @method static assertInvalidResponse($status = null) + * @method static assertValidationMessage($expected) + * @method static assertErrorsContain($errors) + * @method static void skipRequestValidation() */ class TestResponse { diff --git a/config/spectator.php b/config/spectator.php index 4c11b7a..5f5b436 100644 --- a/config/spectator.php +++ b/config/spectator.php @@ -26,21 +26,21 @@ 'sources' => [ 'local' => [ - 'source' => 'local', + 'source' => 'local', 'base_path' => env('SPEC_PATH'), ], 'remote' => [ - 'source' => 'remote', + 'source' => 'remote', 'base_path' => env('SPEC_PATH'), - 'params' => env('SPEC_URL_PARAMS', ''), + 'params' => env('SPEC_URL_PARAMS', ''), ], 'github' => [ - 'source' => 'github', + 'source' => 'github', 'base_path' => env('SPEC_GITHUB_PATH'), - 'repo' => env('SPEC_GITHUB_REPO'), - 'token' => env('SPEC_GITHUB_TOKEN'), + 'repo' => env('SPEC_GITHUB_REPO'), + 'token' => env('SPEC_GITHUB_TOKEN'), ], ], diff --git a/src/Assertions.php b/src/Assertions.php index 8693c1d..45950e0 100644 --- a/src/Assertions.php +++ b/src/Assertions.php @@ -123,7 +123,7 @@ public function assertErrorsContain() return fn ($errors) => $this->runAssertion(function () use ($errors) { $matches = 0; - if (! is_array($errors)) { + if (!is_array($errors)) { $errors = [$errors]; } diff --git a/src/Exceptions/SchemaValidationException.php b/src/Exceptions/SchemaValidationException.php index dc8af2d..0bb79e3 100644 --- a/src/Exceptions/SchemaValidationException.php +++ b/src/Exceptions/SchemaValidationException.php @@ -15,8 +15,9 @@ abstract class SchemaValidationException extends \Exception implements Exception protected array $errors = []; /** - * @param string $message - * @param ValidationError $error + * @param string $message + * @param ValidationError $error + * * @return static */ public static function withError(string $message, ValidationError $error): self @@ -31,7 +32,7 @@ public static function withError(string $message, ValidationError $error): self } /** - * @param ValidationError $error + * @param ValidationError $error */ protected function setErrors(ValidationError $error) { @@ -64,8 +65,9 @@ public function hasErrors(): bool * Given a schema and ValidationError, returns a helpful error message where the validation * errors are mapped over a structured schema representation. * - * @param array|object $schema A JSON schema. - * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. + * @param array|object $schema A JSON schema. + * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. + * * @return string */ public static function validationErrorMessage($schema, ValidationError $validation_error): string @@ -102,7 +104,7 @@ public static function validationErrorMessage($schema, ValidationError $validati // error alongside the item. $strings = []; - if (! is_null($schema_formatted)) { + if (!is_null($schema_formatted)) { foreach ($schema_formatted as $key => $schema_item) { if (isset($error_location_map[$key])) { $schema_item = self::colorize($schema_item, Format::TEXT_LIGHT_GREY); @@ -125,8 +127,9 @@ public static function validationErrorMessage($schema, ValidationError $validati /** * Returns a formatted validation error. * - * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. - * @param bool $flat Should the formatted error be flat (a simple array) or structured? + * @param ValidationError $validation_error A validation error generated by Opis\JsonSchema. + * @param bool $flat Should the formatted error be flat (a simple array) or structured? + * * @return array|string */ public static function formatValidationError(ValidationError $validation_error, bool $flat = false) @@ -164,11 +167,12 @@ public static function formatValidationError(ValidationError $validation_error, * status*: string * message*: string <== The data (integer) must match the type: string * - * @param array $schema JSON schema represented as an array. - * @param string $location_current The current location within the JSON schema structure. - * @param string $key_current The key at the current location, if one is present. - * @param array $keys_required The keys required at the current location, if provided. - * @param int $indent_level Represents how much newly added values should be indented. + * @param array $schema JSON schema represented as an array. + * @param string $location_current The current location within the JSON schema structure. + * @param string $key_current The key at the current location, if one is present. + * @param array $keys_required The keys required at the current location, if provided. + * @param int $indent_level Represents how much newly added values should be indented. + * * @return array */ public static function formatSchema(array $schema, string $location_current, string $key_current, array $keys_required, int $indent_level): array @@ -182,7 +186,7 @@ public static function formatSchema(array $schema, string $location_current, str }, ARRAY_FILTER_USE_KEY); $polymorphic_keys = array_values(array_flip($polymorphic_keys)); - if (! empty($polymorphic_keys)) { // first, check for a polymorphic schema... + if (!empty($polymorphic_keys)) { // first, check for a polymorphic schema... $polymorphic_key = $polymorphic_keys[0]; // create entry for polymorphic schema @@ -199,7 +203,7 @@ public static function formatSchema(array $schema, string $location_current, str } elseif (isset($schema['type'])) { // otherwise, check for explicit schema type... // convert "type" to an array (to support single/multiple types) $types = []; - if (! is_array($schema['type'])) { + if (!is_array($schema['type'])) { $types = [$schema['type']]; } else { $types = $schema['type']; @@ -296,10 +300,11 @@ public static function formatSchema(array $schema, string $location_current, str * Applies modifiers to schema's key name and value, if provided. Then returns display string * for the schema. * - * @param string $type The schema's type (ex: allOf, object, string). - * @param string $type_modifier A modifier for the type (ex: "++" in "object++"). - * @param string $key The schema's key (ex: "name" in "name: string"). - * @param string $key_modifier A modifier for the key (ex: "?" in "string?"). + * @param string $type The schema's type (ex: allOf, object, string). + * @param string $type_modifier A modifier for the type (ex: "++" in "object++"). + * @param string $key The schema's key (ex: "name" in "name: string"). + * @param string $key_modifier A modifier for the key (ex: "?" in "string?"). + * * @return string */ protected static function schemaItemDisplayString(string $type, string $type_modifier = '', string $key = '', string $key_modifier = ''): string @@ -313,8 +318,9 @@ protected static function schemaItemDisplayString(string $type, string $type_mod /** * Applies indentation to a display string and returns it. * - * @param string $display_string A string to display. - * @param int $indent_level The level of indentation to apply to the display string. + * @param string $display_string A string to display. + * @param int $indent_level The level of indentation to apply to the display string. + * * @return string */ protected static function indentedDisplayString(string $display_string, int $indent_level = 0): string @@ -325,9 +331,10 @@ protected static function indentedDisplayString(string $display_string, int $ind /** * Colorize text. * - * @param string $text - * @param string $text_color - * @param string $style + * @param string $text + * @param string $text_color + * @param string $style + * * @return string */ protected static function colorize(string $text, string $text_color, string $style = ''): string diff --git a/src/Middleware.php b/src/Middleware.php index 4a07f7f..aa42006 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -40,8 +40,8 @@ class Middleware /** * Middleware constructor. * - * @param RequestFactory $spectator - * @param ExceptionHandler $exceptionHandler + * @param RequestFactory $spectator + * @param ExceptionHandler $exceptionHandler */ public function __construct(RequestFactory $spectator, ExceptionHandler $exceptionHandler) { @@ -50,18 +50,19 @@ public function __construct(RequestFactory $spectator, ExceptionHandler $excepti } /** - * @param Request $request - * @param Closure $next - * @return JsonResponse|Request + * @param Request $request + * @param Closure $next * * @throws InvalidPathException * @throws MissingSpecException * @throws RequestValidationException * @throws \Throwable + * + * @return JsonResponse|Request */ public function handle(Request $request, Closure $next) { - if (! $this->spectator->getSpec()) { + if (!$this->spectator->getSpec()) { return $next($request); } @@ -86,8 +87,9 @@ public function handle(Request $request, Closure $next) } /** - * @param $exception - * @param $code + * @param $exception + * @param $code + * * @return JsonResponse */ protected function formatResponse($exception, $code): JsonResponse @@ -98,15 +100,16 @@ protected function formatResponse($exception, $code): JsonResponse return Response::json(array_merge([ 'exception' => get_class($exception), - 'message' => $exception->getMessage(), + 'message' => $exception->getMessage(), ], $errors), $code); } /** - * @param Request $request - * @param Closure $next - * @param string $requestPath - * @param PathItem $pathItem + * @param Request $request + * @param Closure $next + * @param string $requestPath + * @param PathItem $pathItem + * * @return mixed */ protected function validate(Request $request, Closure $next, string $requestPath, PathItem $pathItem) @@ -138,9 +141,8 @@ protected function validate(Request $request, Closure $next, string $requestPath } /** - * @param $requestPath - * @param $requestMethod - * @return PathItem + * @param $requestPath + * @param $requestMethod * * @throws InvalidPathException * @throws MalformedSpecException @@ -149,10 +151,12 @@ protected function validate(Request $request, Closure $next, string $requestPath * @throws UnresolvableReferenceException * @throws IOException * @throws InvalidJsonPointerSyntaxException + * + * @return PathItem */ protected function pathItem($requestPath, $requestMethod): PathItem { - if (! Str::startsWith($requestPath, '/')) { + if (!Str::startsWith($requestPath, '/')) { $requestPath = '/'.$requestPath; } @@ -177,7 +181,8 @@ protected function pathItem($requestPath, $requestMethod): PathItem } /** - * @param string $path + * @param string $path + * * @return string */ protected function resolvePath(string $path): string diff --git a/src/RequestFactory.php b/src/RequestFactory.php index ceeb6ab..bb27c89 100644 --- a/src/RequestFactory.php +++ b/src/RequestFactory.php @@ -44,7 +44,7 @@ class RequestFactory /** * Set the file name of the spec. * - * @param $name + * @param $name */ public function using($name) { @@ -64,8 +64,8 @@ public function getSpec(): ?string /** * Set the prefix for the API paths. * - * @param $pathPrefix - * return RequestFactory + * @param $pathPrefix + * return RequestFactory */ public function setPathPrefix($pathPrefix): self { @@ -99,14 +99,14 @@ public function reset(): void /** * Resolve and parse the spec. * - * @return OpenApi - * * @throws \cebe\openapi\exceptions\IOException * @throws \cebe\openapi\exceptions\TypeErrorException * @throws \cebe\openapi\exceptions\UnresolvableReferenceException * @throws \cebe\openapi\json\InvalidJsonPointerSyntaxException * @throws MalformedSpecException * @throws MissingSpecException + * + * @return OpenApi */ public function resolve(): OpenApi { @@ -137,7 +137,8 @@ public function resolve(): OpenApi } /** - * @param Throwable $throwable + * @param Throwable $throwable + * * @return void */ public function captureRequestValidation(Throwable $throwable) @@ -146,7 +147,8 @@ public function captureRequestValidation(Throwable $throwable) } /** - * @param Throwable $throwable + * @param Throwable $throwable + * * @return void */ public function captureResponseValidation(Throwable $throwable) @@ -157,13 +159,13 @@ public function captureResponseValidation(Throwable $throwable) /** * Retrieve the spec file. * - * @return mixed - * * @throws MissingSpecException + * + * @return mixed */ protected function getFile() { - if (! $source = Arr::get(config('spectator.sources', []), config('spectator.default'))) { + if (!$source = Arr::get(config('spectator.sources', []), config('spectator.default'))) { throw new MissingSpecException('Cannot resolve schema with missing or invalid spec.'); } @@ -181,11 +183,12 @@ protected function getFile() /** * Retrieve a local spec file. * - * @param array $source - * @param $file - * @return string + * @param array $source + * @param $file * * @throws MissingSpecException + * + * @return string */ protected function getLocalPath(array $source, $file): string { @@ -203,8 +206,9 @@ protected function getLocalPath(array $source, $file): string /** * Retrieve a remote spec file. * - * @param array $source - * @param $file + * @param array $source + * @param $file + * * @return string */ protected function getRemotePath(array $source, $file): string @@ -221,8 +225,9 @@ protected function getRemotePath(array $source, $file): string /** * Build a Github path. * - * @param array $source - * @param $file + * @param array $source + * @param $file + * * @return string */ protected function getGithubPath(array $source, $file): string @@ -235,7 +240,8 @@ protected function getGithubPath(array $source, $file): string /** * Standardize a file name. * - * @param $file + * @param $file + * * @return string */ protected function standardizeFileName($file): string @@ -250,12 +256,13 @@ protected function standardizeFileName($file): string /** * Standardize a path. * - * @param $path + * @param $path + * * @return string */ protected function standardizePath($path): string { - if (! Str::endsWith($path, '/')) { + if (!Str::endsWith($path, '/')) { $path = $path.'/'; } diff --git a/src/Spectator.php b/src/Spectator.php index 63661be..0205501 100644 --- a/src/Spectator.php +++ b/src/Spectator.php @@ -5,26 +5,26 @@ use Illuminate\Support\Facades\Facade; /** - * @method static void using($name) Set the spec file to use. - * @method static void reset() Reset the name of the spec. - * @method static string getSpec() Get the spec being used. - * @method static string getPathPrefix() Get the path prefix being used. - * @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used. - * @method static object resolve() Resolve the spec into an object. - * @method bool shouldValidateRequest() Indicate if request should be validated. + * @method static void using($name) Set the spec file to use. + * @method static void reset() Reset the name of the spec. + * @method static string getSpec() Get the spec being used. + * @method static string getPathPrefix() Get the path prefix being used. + * @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used. + * @method static object resolve() Resolve the spec into an object. + * @method bool shouldValidateRequest() Indicate if request should be validated. * * @see \Spectator\RequestFactory */ class Spectator extends Facade { /** - * @method static void using($name) Set the spec file to use. - * @method static void reset() Reset the name of the spec. - * @method static string getSpec() Get the spec being used. - * @method static string getPathPrefix() Get the path prefix being used. - * @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used. - * @method static object resolve() Resolve the spec into an object. - * @method bool shouldValidateRequest() Indicate if request should be validated. + * @method static void using($name) Set the spec file to use. + * @method static void reset() Reset the name of the spec. + * @method static string getSpec() Get the spec being used. + * @method static string getPathPrefix() Get the path prefix being used. + * @method static RequestFactory setPathPrefix($prefix) Set the path prefix being used. + * @method static object resolve() Resolve the spec into an object. + * @method bool shouldValidateRequest() Indicate if request should be validated. * * @see \Spectator\RequestFactory */ diff --git a/src/Validation/AbstractValidator.php b/src/Validation/AbstractValidator.php index b9ec688..bc51109 100644 --- a/src/Validation/AbstractValidator.php +++ b/src/Validation/AbstractValidator.php @@ -13,15 +13,16 @@ abstract class AbstractValidator /** * Check if properties exist, and if so, prepare them based on version. * - * @param Schema $schema - * @param string|null $mode Access mode 'read' or 'write' + * @param Schema $schema + * @param string|null $mode Access mode 'read' or 'write' + * * @return mixed */ protected function prepareData(Schema $schema, string $mode = null) { $data = $schema->getSerializableData(); - if (! isset($data->properties)) { + if (!isset($data->properties)) { return $data; } @@ -41,8 +42,9 @@ protected function prepareData(Schema $schema, string $mode = null) /** * Filters out readonly|writeonly properties. * - * @param $data - * @param string|null $type Access mode 'read' or 'write' + * @param $data + * @param string|null $type Access mode 'read' or 'write' + * * @return mixed */ protected function filterProperties(object $data, string $mode = null): object @@ -89,7 +91,7 @@ function ($property) use ($filter_by) { $data->required = array_filter( $data->required, function ($property) use ($filter_properties) { - return ! in_array($property, $filter_properties); + return !in_array($property, $filter_properties); }, ); } @@ -146,7 +148,8 @@ function ($item) use ($mode) { * ... * ] * - * @param $properties + * @param $properties + * * @return mixed */ protected function wrapAttributesToArray($properties) @@ -177,7 +180,7 @@ protected function wrapAttributesToArray($properties) } // Before we check recursive cases, make sure this object defines a "type". - if (! isset($attributes->type)) { + if (!isset($attributes->type)) { break; } diff --git a/src/Validation/RequestValidator.php b/src/Validation/RequestValidator.php index b136bdb..179c5b9 100644 --- a/src/Validation/RequestValidator.php +++ b/src/Validation/RequestValidator.php @@ -37,10 +37,10 @@ class RequestValidator extends AbstractValidator /** * RequestValidator constructor. * - * @param Request $request - * @param PathItem $pathItem - * @param string $method - * @param string $version + * @param Request $request + * @param PathItem $pathItem + * @param string $method + * @param string $version */ public function __construct(Request $request, PathItem $pathItem, string $method, string $version = '3.0') { @@ -51,9 +51,9 @@ public function __construct(Request $request, PathItem $pathItem, string $method } /** - * @param Request $request - * @param PathItem $pathItem - * @param string $method + * @param Request $request + * @param PathItem $pathItem + * @param string $method * * @throws RequestValidationException|SchemaValidationException */ @@ -92,13 +92,13 @@ protected function validateParameters() foreach ($required_parameters as $parameter) { // Verify presence, if required. - if ($parameter->in === 'path' && ! $route->hasParameter($parameter->name)) { + if ($parameter->in === 'path' && !$route->hasParameter($parameter->name)) { throw new RequestValidationException("Missing required parameter {$parameter->name} in URL path."); - } elseif ($parameter->in === 'query' && ! $this->hasQueryParam($parameter->name)) { + } elseif ($parameter->in === 'query' && !$this->hasQueryParam($parameter->name)) { throw new RequestValidationException("Missing required query parameter [?{$parameter->name}=]."); - } elseif ($parameter->in === 'header' && ! $this->request->headers->has($parameter->name)) { + } elseif ($parameter->in === 'header' && !$this->request->headers->has($parameter->name)) { throw new RequestValidationException("Missing required header [{$parameter->name}]."); - } elseif ($parameter->in === 'cookie' && ! $this->request->cookies->has($parameter->name)) { + } elseif ($parameter->in === 'cookie' && !$this->request->cookies->has($parameter->name)) { throw new RequestValidationException("Missing required cookie [{$parameter->name}]."); } } @@ -145,6 +145,7 @@ protected function validateParameters() // If the result is not valid, then display failure reason. if ($result->isValid() === false) { $message = RequestValidationException::validationErrorMessage($expected_parameter_schema, $result->error()); + throw RequestValidationException::withError($message, $result->error()); } } @@ -153,8 +154,9 @@ protected function validateParameters() } /** - * @param mixed $parameter - * @param string|null $type + * @param mixed $parameter + * @param string|null $type + * * @return mixed */ private function castParameter($parameter, ?string $type) @@ -191,7 +193,7 @@ protected function validateBody(): void // Content types should match. $content_type = $this->request->header('Content-Type'); - if (! array_key_exists($content_type, $expected_body->content)) { + if (!array_key_exists($content_type, $expected_body->content)) { throw new RequestValidationException('Request did not match any specified media type for request body.'); } @@ -214,6 +216,7 @@ protected function validateBody(): void // If the result is not valid, then display failure reason. if ($result->isValid() === false) { $message = RequestValidationException::validationErrorMessage($expected_body_schema, $result->error()); + throw RequestValidationException::withError($message, $result->error()); } } @@ -241,7 +244,7 @@ protected function parseBodySchema(): object private function toObject($data) { - if (! is_array($data)) { + if (!is_array($data)) { return $data; } elseif (Arr::isAssoc($data)) { return (object) array_map([$this, 'toObject'], $data); diff --git a/src/Validation/ResponseValidator.php b/src/Validation/ResponseValidator.php index c6b6061..4ecf2c3 100644 --- a/src/Validation/ResponseValidator.php +++ b/src/Validation/ResponseValidator.php @@ -63,7 +63,7 @@ protected function parseResponse(Response $response) )); // Does the response match any of the specified media types? - if (! in_array($contentType, $specTypes)) { + if (!in_array($contentType, $specTypes)) { $message = 'Response did not match any specified content type.'; $message .= PHP_EOL.PHP_EOL.' Expected: '.$specTypes[0]; $message .= PHP_EOL.' Actual: '.$contentType; @@ -81,8 +81,8 @@ protected function parseResponse(Response $response) } /** - * @param Schema $schema - * @param $body + * @param Schema $schema + * @param $body * * @throws ResponseValidationException * @throws SchemaValidationException @@ -96,6 +96,7 @@ protected function validateResponse(Schema $schema, $body) if ($result->isValid() === false) { $message = ResponseValidationException::validationErrorMessage($expected_schema, $result->error()); + throw ResponseValidationException::withError($message, $result->error()); } } @@ -151,11 +152,12 @@ protected function schemaType(Schema $schema) } /** - * @param $contentType - * @param $schemaType - * @return mixed + * @param $contentType + * @param $schemaType * * @throws ResponseValidationException + * + * @return mixed */ protected function body($contentType, $schemaType) { diff --git a/tests/AssertionsTest.php b/tests/AssertionsTest.php index a1a6b17..1e3b14f 100644 --- a/tests/AssertionsTest.php +++ b/tests/AssertionsTest.php @@ -26,8 +26,8 @@ public function test_asserts_invalid_path() Route::get('/invalid', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -46,8 +46,8 @@ public function test_fails_asserts_invalid_path() Route::get('/invalid', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -65,8 +65,8 @@ public function test_fails_asserts_invalid_path_without_exception_handling() Route::get('/invalid', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -106,7 +106,7 @@ public function test_request_assertion_does_not_format_laravel_validation_respon })->middleware(Middleware::class); $response = $this->postJson('/users', [ - 'name' => 'Jane Doe', + 'name' => 'Jane Doe', 'email' => 'jane.doe@example.com', ]); diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index 074a6fa..8cc0c3b 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -27,8 +27,8 @@ public function test_only_processes_request_once(): void return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; diff --git a/tests/RequestValidatorTest.php b/tests/RequestValidatorTest.php index 84e750a..d5cc4b6 100644 --- a/tests/RequestValidatorTest.php +++ b/tests/RequestValidatorTest.php @@ -31,8 +31,8 @@ public function test_cannot_resolve_prefixed_path(): void Route::get('/v1/users', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -51,8 +51,8 @@ public function test_resolves_prefixed_path_from_config(): void Route::get('/v1/users', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -69,8 +69,8 @@ public function test_uses_global_path_via_inline_setting(): void Route::get('/v1/users', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -90,7 +90,7 @@ public function test_uses_global_path_through_config(): void Route::get('/v1/orgs/{orgUuid}', function () use ($uuid) { return [ - 'id' => 1, + 'id' => 1, 'uuid' => $uuid, 'name' => 'My Org', ]; @@ -112,7 +112,7 @@ public function test_uses_global_path_with_route_prefix(): void Route::prefix('v1')->group(function () use ($uuid) { Route::get('/orgs/{orgUuid}', function () use ($uuid) { return [ - 'id' => 1, + 'id' => 1, 'uuid' => $uuid, 'name' => 'My Org', ]; @@ -130,8 +130,8 @@ public function test_resolve_route_model_binding(): void Route::get('/users/{user}', function () { return [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ]; })->middleware(Middleware::class); @@ -148,7 +148,7 @@ public function test_resolve_route_model_explicit_binding(): void Route::get('/posts/{postUuid}', function () { return [ - 'id' => 1, + 'id' => 1, 'title' => 'My Post', ]; })->middleware(Middleware::class); @@ -165,7 +165,7 @@ public function test_cannot_resolve_route_model_explicit_binding_with_invalid_fo Route::get('/posts/{postUuid}', function () { return [ - 'id' => 1, + 'id' => 1, 'title' => 'My Post', ]; })->middleware(Middleware::class); @@ -182,7 +182,7 @@ public function test_resolve_route_model_binding_with_multiple_parameters(): voi Route::get('/posts/{postUuid}/comments/{comment}', function () { return [ - 'id' => 1, + 'id' => 1, 'message' => 'My Comment', ]; })->middleware(Middleware::class); @@ -204,7 +204,7 @@ public function test_handle_nullables( Route::post('/users')->middleware(Middleware::class); $payload = [ - 'name' => 'Adam Campbell', + 'name' => 'Adam Campbell', 'email' => 'adam@hotmeteor.com', ]; @@ -323,7 +323,7 @@ public function oneOfSchemaProvider(): array return [ 'valid request, first type' => [ [ - 'bark' => true, + 'bark' => true, 'breed' => 'Dingo', ], $valid, @@ -331,23 +331,23 @@ public function oneOfSchemaProvider(): array 'valid request, second type' => [ [ 'hunts' => true, - 'age' => 2, + 'age' => 2, ], $valid, ], 'invalid request' => [ [ - 'bark' => true, + 'bark' => true, 'hunts' => false, ], $invalid, ], 'invalid request, mixed' => [ [ - 'bark' => true, + 'bark' => true, 'hunts' => false, 'breed' => 'Husky', - 'age' => 3, + 'age' => 3, ], $invalid, ], @@ -390,7 +390,7 @@ public function anyOfSchemaProvider(): array 'valid, other required' => [ [ 'pet_type' => 'Cat', - 'hunts' => true, + 'hunts' => true, ], $valid, ], @@ -398,14 +398,14 @@ public function anyOfSchemaProvider(): array [ 'nickname' => 'Fido', 'pet_type' => 'Dog', - 'age' => 4, + 'age' => 4, ], $valid, ], 'invalid request, missing required' => [ [ 'nickname' => 'Mr. Paws', - 'hunts' => false, + 'hunts' => false, ], $invalid, ], @@ -442,23 +442,23 @@ public function allOfSchemaProvider(): array 'valid, Cat' => [ [ 'pet_type' => 'Cat', - 'age' => 3, - 'hunts' => true, + 'age' => 3, + 'hunts' => true, ], $valid, ], 'valid, Dog' => [ [ 'pet_type' => 'Dog', - 'bark' => true, + 'bark' => true, ], $valid, ], 'valid, Dog 2' => [ [ 'pet_type' => 'Dog', - 'bark' => true, - 'breed' => 'Dingo', + 'bark' => true, + 'breed' => 'Dingo', ], $valid, ], @@ -470,7 +470,7 @@ public function allOfSchemaProvider(): array ], 'invalid request, invalid attribute' => [ [ - 'age' => 3, + 'age' => 3, 'bark' => true, ], $invalid, @@ -547,7 +547,7 @@ public function test_handles_form_data(): void $this->post( '/users', - ['name' => 'Adam Campbell', 'picture' => UploadedFile::fake()->image('test.jpg')], + ['name' => 'Adam Campbell', 'picture' => UploadedFile::fake()->image('test.jpg')], ['Content-Type' => 'multipart/form-data'] ) ->assertInvalidRequest() @@ -557,7 +557,7 @@ public function test_handles_form_data(): void $this->post( '/users', - ['name' => 'Adam Campbell', 'email' => 'test@test.com'], + ['name' => 'Adam Campbell', 'email' => 'test@test.com'], ['Content-Type' => 'multipart/form-data'] ) ->assertInvalidRequest() @@ -567,7 +567,7 @@ public function test_handles_form_data(): void $this->post( '/users', - ['name' => 'Adam Campbell', 'email' => 'test@test.com', 'picture' => UploadedFile::fake()->image('test.jpg')], + ['name' => 'Adam Campbell', 'email' => 'test@test.com', 'picture' => UploadedFile::fake()->image('test.jpg')], ['Content-Type' => 'multipart/form-data'] ) ->assertValidRequest(); @@ -585,7 +585,7 @@ public function test_handles_form_data_with_multiple_files(): void '/users/multiple-files', [ 'picture' => UploadedFile::fake()->image('test.jpg'), - 'files' => [ + 'files' => [ ['name' => 'test.jpg', 'file' => UploadedFile::fake()->image('test.jpg')], ['name' => 'test.jpg', 'file' => UploadedFile::fake()->image('test.jpg')], ], @@ -602,8 +602,8 @@ public function test_handles_form_data_with_multiple_files(): void '/users/multiple-files', [ 'picture' => UploadedFile::fake()->image('test.jpg'), - 'files' => [], - 'resume' => [ + 'files' => [], + 'resume' => [ 'name' => 'test.pdf', 'file' => UploadedFile::fake()->create('test.pdf'), ], @@ -642,8 +642,8 @@ public function test_numeric_values() Route::get('/users', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -663,8 +663,8 @@ public function test_comma_separated_values() Route::get('/users', function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -696,7 +696,7 @@ public function test_ignores_request_validation_if_not_asserted(): void Route::get('/posts/{postUuid}', function () { return [ - 'id' => 1, + 'id' => 1, 'title' => 'My Post', ]; })->middleware(Middleware::class); @@ -744,8 +744,8 @@ public function requiredReadOnlySchemaProvider(): array return [ 'valid, Readonly not passed' => [ [ - 'name' => 'Adam Campbell', - 'email' => 'adam@hotmeteor.com', + 'name' => 'Adam Campbell', + 'email' => 'adam@hotmeteor.com', 'arrayProperty' => [ [ 'name' => 'The Hobbit', @@ -765,15 +765,15 @@ public function requiredReadOnlySchemaProvider(): array ], 'Invalid, Books not passed' => [ [ - 'name' => 'Adam Campbell', + 'name' => 'Adam Campbell', 'email' => 'adam@hotmeteor.com', ], $invalid, ], 'invalid, Readonly passed' => [ [ - 'id' => 1, - 'name' => 'Adam Campbell', + 'id' => 1, + 'name' => 'Adam Campbell', 'email' => 'adam@hotmeteor.com', ], $invalid, diff --git a/tests/ResponseValidatorTest.php b/tests/ResponseValidatorTest.php index aebb804..60b3813 100644 --- a/tests/ResponseValidatorTest.php +++ b/tests/ResponseValidatorTest.php @@ -26,8 +26,8 @@ public function test_validates_valid_json_response(): void Route::get('/users', static function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -56,7 +56,7 @@ public function test_validates_invalid_json_response(): void Route::get('/users', static function () { return [ [ - 'id' => 1, + 'id' => 1, 'email' => 'invalid', ], ]; @@ -73,8 +73,8 @@ public function test_validates_valid_problem_json_response() Route::get('/users', function () { return response()->json([ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ], 422, ['Content-Type' => 'application/problem+json']); @@ -103,7 +103,7 @@ public function test_validates_invalid_problem_json_response() Route::get('/users', function () { return response()->json([ [ - 'id' => 1, + 'id' => 1, 'email' => 'invalid', ], ], 422, ['Content-Type' => 'application/problem+json']); @@ -160,8 +160,8 @@ public function test_cannot_locate_path_without_path_prefix(): void Route::get('/api/v2/users', static function () { return [ [ - 'id' => 1, - 'name' => 'Jim', + 'id' => 1, + 'name' => 'Jim', 'email' => 'test@test.test', ], ]; @@ -208,8 +208,8 @@ public function test_handle_nullables( Route::get('/users/{user}', static function () use ($state) { $return = [ 'first_name' => 'Joe', - 'last_name' => 'Bloggs', - 'posts' => [ + 'last_name' => 'Bloggs', + 'posts' => [ ['title' => 'My first post!'], ], ]; @@ -339,7 +339,7 @@ public function test_handles_oneOf($response, $valid): void })->middleware(Middleware::class); $request = [ - 'bark' => true, + 'bark' => true, 'breed' => 'Dingo', ]; @@ -360,7 +360,7 @@ public function oneOfSchemaProvider(): array return [ 'valid response, first type' => [ [ - 'bark' => true, + 'bark' => true, 'breed' => 'Dingo', ], $valid, @@ -368,23 +368,23 @@ public function oneOfSchemaProvider(): array 'valid response, second type' => [ [ 'hunts' => true, - 'age' => 2, + 'age' => 2, ], $valid, ], 'invalid response' => [ [ - 'bark' => true, + 'bark' => true, 'hunts' => false, ], $invalid, ], 'invalid response, mixed' => [ [ - 'bark' => true, + 'bark' => true, 'hunts' => false, 'breed' => 'Husky', - 'age' => 3, + 'age' => 3, ], $invalid, ], @@ -431,7 +431,7 @@ public function anyOfSchemaProvider(): array 'valid, other required' => [ [ 'pet_type' => 'Cat', - 'hunts' => true, + 'hunts' => true, ], $valid, ], @@ -439,14 +439,14 @@ public function anyOfSchemaProvider(): array [ 'nickname' => 'Fido', 'pet_type' => 'Dog', - 'age' => 4, + 'age' => 4, ], $valid, ], 'invalid request, missing required' => [ [ 'nickname' => 'Mr. Paws', - 'hunts' => false, + 'hunts' => false, ], $invalid, ], @@ -467,8 +467,8 @@ public function test_handles_allOf($response, $isValid): void $request = [ 'pet_type' => 'Cat', - 'age' => 3, - 'hunts' => true, + 'age' => 3, + 'hunts' => true, ]; $handled_request = $this->patchJson('/pets', $request); @@ -489,23 +489,23 @@ public function allOfSchemaProvider(): array 'valid, Cat' => [ [ 'pet_type' => 'Cat', - 'age' => 3, - 'hunts' => true, + 'age' => 3, + 'hunts' => true, ], $valid, ], 'valid, Dog' => [ [ 'pet_type' => 'Dog', - 'bark' => true, + 'bark' => true, ], $valid, ], 'valid, Dog 2' => [ [ 'pet_type' => 'Dog', - 'bark' => true, - 'breed' => 'Dingo', + 'bark' => true, + 'breed' => 'Dingo', ], $valid, ], @@ -517,7 +517,7 @@ public function allOfSchemaProvider(): array ], 'invalid request, invalid attribute' => [ [ - 'age' => 3, + 'age' => 3, 'bark' => true, ], $invalid, @@ -554,8 +554,8 @@ public function test_handles_inheritance(): void Route::get('/item', static function () { return [ - 'name' => 'Table', - 'type' => 1234, + 'name' => 'Table', + 'type' => 1234, 'description' => 'Furniture', ]; })->middleware(Middleware::class); @@ -573,14 +573,14 @@ public function test_response_example(): void Route::get('/tags', static function () { return [ 'status' => 'success', - 'data' => [ + 'data' => [ [ - 'id' => '3fafec77-402b-35f9-b26a-bd6430da3a29', + 'id' => '3fafec77-402b-35f9-b26a-bd6430da3a29', 'name' => 'Photography', 'slug' => 'photography', ], [ - 'id' => '3fafec77-402b-35f9-b26a-bd6430da3a29', + 'id' => '3fafec77-402b-35f9-b26a-bd6430da3a29', 'name' => 'Marketing', 'slug' => null, ], @@ -623,8 +623,8 @@ public function test_response_succeeds_with_empty_array(): void Route::get('/orgs/{orgUuid}', static function () use ($uuid) { return [ - 'id' => $uuid, - 'name' => 'My Org', + 'id' => $uuid, + 'name' => 'My Org', 'orders' => [], ]; })->middleware(Middleware::class); @@ -642,8 +642,8 @@ public function test_response_fails_with_invalid_array(): void Route::get('/orgs/{orgUuid}', static function () use ($uuid) { return [ - 'id' => $uuid, - 'name' => 'My Org', + 'id' => $uuid, + 'name' => 'My Org', 'orders' => [[]], ]; })->middleware(Middleware::class); @@ -705,8 +705,8 @@ public function requiredWriteOnlySchemaProvider(): array return [ 'valid, Writeonly not passed' => [ [ - 'id' => 1, - 'email' => 'adam@hotmeteor.com', + 'id' => 1, + 'email' => 'adam@hotmeteor.com', 'arrayProperty' => [ [ 'id' => 2, @@ -726,22 +726,22 @@ public function requiredWriteOnlySchemaProvider(): array ], 'Invalid, Books not passed' => [ [ - 'id' => 1, + 'id' => 1, 'email' => 'adam@hotmeteor.com', ], $invalid, ], 'invalid, Writeonly passed' => [ [ - 'id' => 1, - 'name' => 'Adam Campbell', + 'id' => 1, + 'name' => 'Adam Campbell', 'email' => 'adam@hotmeteor.com', ], $invalid, ], 'invalid, required not passed' => [ [ - 'name' => 'Adam Campbell', + 'name' => 'Adam Campbell', 'email' => 'adam@hotmeteor.com', ], $invalid,