From e27770ab4fde2f3f3a1d7376b7ffbd664eab3b78 Mon Sep 17 00:00:00 2001 From: Bedram Tamang Date: Tue, 18 Oct 2022 16:52:18 +0545 Subject: [PATCH 1/3] Pint --- .php_cs | 40 --------- composer.json | 108 ++++++++++++------------- config/api-generator.php | 27 ++++--- pint.json | 11 +++ src/APIGeneratorFactory.php | 1 - src/APIGeneratorServiceProvider.php | 4 +- src/Contract/Description.php | 2 - src/Parser/HasPropertyType.php | 23 +++--- src/Parser/RequestBodyComponent.php | 34 ++++---- src/Rules/ArrayRule.php | 7 +- src/Rules/FileRule.php | 7 +- src/Rules/IntegerRule.php | 5 +- src/Rules/RequiredRule.php | 8 +- src/Rules/RuleContract.php | 2 - src/Security/Bearer.php | 8 +- src/Security/HasParameter.php | 38 ++++----- src/Security/HasResponse.php | 52 ++++++------ src/Security/HasSecurity.php | 10 +-- src/Security/HasServer.php | 7 +- src/Services/Generator.php | 43 +++++----- src/Services/ProcessRequestTrait.php | 9 ++- src/Traits/HasDocsGenerator.php | 48 +++++------ src/helper.php | 3 +- tests/ArrayRuleTest.php | 7 +- tests/ConfigTest.php | 14 ++-- tests/FileRuleTest.php | 8 +- tests/FileSetupTest.php | 19 +++-- tests/FormRequestRuleTest.php | 33 ++++---- tests/FormRequestTest.php | 54 ++++++------- tests/IntegerRuleTest.php | 3 +- tests/PathTest.php | 38 ++++----- tests/RequireRuleTest.php | 9 +-- tests/ResponseSchemeTest.php | 44 +++++----- tests/RouteParameterTest.php | 23 +++--- tests/SecuritySchemaTest.php | 10 +-- tests/Stubs/ExampleController.php | 6 +- tests/Stubs/ExampleFormRequest.php | 1 - tests/Stubs/FormRequestImageArray.php | 18 ++--- tests/Stubs/FormRequestInArray.php | 7 +- tests/Stubs/RuleExampleFormRequest.php | 8 +- tests/Stubs/UsersController.php | 26 +++--- tests/TagsTest.php | 12 +-- tests/TestCase.php | 15 ++-- tests/helper.php | 13 ++- 44 files changed, 384 insertions(+), 481 deletions(-) delete mode 100644 .php_cs create mode 100644 pint.json diff --git a/.php_cs b/.php_cs deleted file mode 100644 index ac127a7..0000000 --- a/.php_cs +++ /dev/null @@ -1,40 +0,0 @@ -in([ - __DIR__ . '/src', - __DIR__ . '/tests', - ]) - ->name('*.php') - ->notName('*.blade.php') - ->ignoreDotFiles(true) - ->ignoreVCS(true); - -return PhpCsFixer\Config::create() - ->setRules([ - '@PSR2' => true, - 'array_syntax' => ['syntax' => 'short'], - 'ordered_imports' => ['sortAlgorithm' => 'alpha'], - 'no_unused_imports' => true, - 'not_operator_with_successor_space' => true, - 'trailing_comma_in_multiline_array' => true, - 'phpdoc_scalar' => true, - 'unary_operator_spaces' => true, - 'binary_operator_spaces' => true, - 'blank_line_before_statement' => [ - 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], - ], - 'phpdoc_single_line_var_spacing' => true, - 'phpdoc_var_without_name' => true, - 'class_attributes_separation' => [ - 'elements' => [ - 'method', - ], - ], - 'method_argument_space' => [ - 'on_multiline' => 'ensure_fully_multiline', - 'keep_multiple_spaces_after_comma' => true, - ], - 'single_trait_insert_per_statement' => true, - ]) - ->setFinder($finder); diff --git a/composer.json b/composer.json index 980a1db..d6106d1 100644 --- a/composer.json +++ b/composer.json @@ -1,59 +1,59 @@ { - "name": "jobins/api-generator", - "description": "Generate api docs while writing test case (Laravel).", - "keywords": [ - "JoBins", - "documentation", - "api", - "swagger", - "openapi", - "phpunit" - ], - "homepage": "https://github.com/JoBinsJP/api-genereator", - "license": "MIT", - "authors": [ - { - "name": "Bedram Tamang", - "email": "tmgbedu@gmail.com" - } - ], - "require": { - "php": "^7.4|^8.0|^8.1", - "ext-json": "*" - }, - "autoload": { - "psr-4": { - "JoBins\\APIGenerator\\": "src" + "name": "jobins/api-generator", + "description": "Generate api docs while writing test case (Laravel).", + "keywords": [ + "JoBins", + "documentation", + "api", + "swagger", + "openapi", + "phpunit" + ], + "homepage": "https://github.com/JoBinsJP/api-genereator", + "license": "MIT", + "authors": [ + { + "name": "Bedram Tamang", + "email": "tmgbedu@gmail.com" + } + ], + "require": { + "php": "^7.4|^8.0|^8.1", + "ext-json": "*" + }, + "autoload": { + "psr-4": { + "JoBins\\APIGenerator\\": "src" + }, + "files": [ + "src/helper.php" + ] + }, + "scripts": { + "test": "vendor/bin/phpunit", + "psalm": "vendor/bin/psalm", + "test-coverage": "vendor/bin/phpunit --coverage-html coverage", + "format": "vendor/bin/pint" + }, + "autoload-dev": { + "psr-4": { + "JoBins\\APIGenerator\\Tests\\": "tests" + }, + "files": [ + "tests/helper.php" + ] }, - "files": [ - "src/helper.php" - ] - }, - "scripts": { - "test": "vendor/bin/phpunit", - "psalm": "vendor/bin/psalm", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage", - "format": "vendor/bin/php-cs-fixer fix --allow-risky=yes" - }, - "autoload-dev": { - "psr-4": { - "JoBins\\APIGenerator\\Tests\\": "tests" + "require-dev": { + "orchestra/testbench": "^6.15", + "phpunit/phpunit": "^9.0", + "vimeo/psalm": "^4.7", + "laravel/pint": "^1.2" }, - "files": [ - "tests/helper.php" - ] - }, - "require-dev": { - "orchestra/testbench": "^6.15", - "phpunit/phpunit": "^9.0", - "friendsofphp/php-cs-fixer": "^2.18", - "vimeo/psalm": "^4.7" - }, - "extra": { - "laravel": { - "providers": [ - "JoBins\\APIGenerator\\APIGeneratorServiceProvider" - ] + "extra": { + "laravel": { + "providers": [ + "JoBins\\APIGenerator\\APIGeneratorServiceProvider" + ] + } } - } } diff --git a/config/api-generator.php b/config/api-generator.php index 9dd82b0..9f0d9bd 100644 --- a/config/api-generator.php +++ b/config/api-generator.php @@ -1,22 +1,23 @@ [ - "url" => "https://{environment}.localhost.test", - "variables" => [ - "environment" => [ - "default" => "api", - "enum" => [ - "api", - "api.dev", - "api.staging", + 'servers' => [ + 'url' => 'https://{environment}.localhost.test', + 'variables' => [ + 'environment' => [ + 'default' => 'api', + 'enum' => [ + 'api', + 'api.dev', + 'api.staging', ], ], ], ], - "openapi" => "3.0.0", - "title" => "Reaching APP API", - "version" => "1.0.0", + 'openapi' => '3.0.0', + 'title' => 'Reaching APP API', + 'version' => '1.0.0', - "file-path" => storage_path("api-docs/api-docs.json"), + 'file-path' => storage_path('api-docs/api-docs.json'), ]; diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..cf5f303 --- /dev/null +++ b/pint.json @@ -0,0 +1,11 @@ +{ + "preset": "laravel", + "rules": { + "simplified_null_return": true, + "braces": false, + "new_with_braces": { + "anonymous_class": false, + "named_class": false + } + } +} diff --git a/src/APIGeneratorFactory.php b/src/APIGeneratorFactory.php index 3ca5711..c887436 100644 --- a/src/APIGeneratorFactory.php +++ b/src/APIGeneratorFactory.php @@ -4,7 +4,6 @@ /** * Class APIGeneratorFactory - * @package JoBins\APIGenerator */ class APIGeneratorFactory { diff --git a/src/APIGeneratorServiceProvider.php b/src/APIGeneratorServiceProvider.php index 59f82ad..0cc8a58 100644 --- a/src/APIGeneratorServiceProvider.php +++ b/src/APIGeneratorServiceProvider.php @@ -34,7 +34,7 @@ public function boot() __DIR__.'/../config/api-generator.php' => config_path('api-generator.php'), ]); - TestResponse::macro("generate", function ($testCase, bool $generate = true) { + TestResponse::macro('generate', function ($testCase, bool $generate = true) { if (! $generate) { return; } @@ -42,7 +42,7 @@ public function boot() /** @var JsonResponse $response */ $response = $this; - (new Generator())->setRequest($testCase->getParams())->setResponse($response)->generate(); + (new Generator)->setRequest($testCase->getParams())->setResponse($response)->generate(); }); } } diff --git a/src/Contract/Description.php b/src/Contract/Description.php index 8782ba3..8967a3b 100644 --- a/src/Contract/Description.php +++ b/src/Contract/Description.php @@ -4,8 +4,6 @@ /** * Interface Description - * - * @package JoBins\APIGenerator\Contract */ interface Description { diff --git a/src/Parser/HasPropertyType.php b/src/Parser/HasPropertyType.php index a6bd0b4..87218f0 100644 --- a/src/Parser/HasPropertyType.php +++ b/src/Parser/HasPropertyType.php @@ -9,8 +9,6 @@ /** * Trait HasPropertyType - * - * @package JoBins\APIGenerator\Security */ trait HasPropertyType { @@ -22,9 +20,8 @@ trait HasPropertyType protected array $rulesArray = []; /** - * @param array $rules - * @param string $name - * + * @param array $rules + * @param string $name * @return array|string[] */ public function getPropertyType(array $rules, string $name): array @@ -33,8 +30,8 @@ public function getPropertyType(array $rules, string $name): array $rules = Arr::get($this->rulesArray, "{$name}.*"); return [ - "type" => "array", - "items" => $this->getPropertyItemType($rules), + 'type' => 'array', + 'items' => $this->getPropertyItemType($rules), ]; } @@ -42,7 +39,7 @@ public function getPropertyType(array $rules, string $name): array } /** - * @param array $rulesArray + * @param array $rulesArray */ public function setRulesArray(array $rulesArray): void { @@ -52,22 +49,22 @@ public function setRulesArray(array $rulesArray): void private function getPropertyItemType(array $rules): array { if (FileRule::check($rules)) { - $this->contentType = "multipart/form-data"; + $this->contentType = 'multipart/form-data'; return [ - "type" => "string", - "format" => "binary", + 'type' => 'string', + 'format' => 'binary', ]; } if (IntegerRule::check($rules)) { return [ - "type" => "integer", + 'type' => 'integer', ]; } return [ - "type" => "string", + 'type' => 'string', ]; } } diff --git a/src/Parser/RequestBodyComponent.php b/src/Parser/RequestBodyComponent.php index d09f0f2..6b231da 100644 --- a/src/Parser/RequestBodyComponent.php +++ b/src/Parser/RequestBodyComponent.php @@ -10,8 +10,6 @@ /** * Class HasRequestBodies - * - * @package JoBins\APIGenerator\Security */ class RequestBodyComponent { @@ -25,12 +23,12 @@ class RequestBodyComponent /** * @var string */ - protected string $contentType = "application/json"; + protected string $contentType = 'application/json'; /** * ParseRequestBody constructor. * - * @param array $request + * @param array $request */ public function __construct(array $request) { @@ -45,7 +43,7 @@ public function getRules(object $class): array return collect($class->rules())->map(function ($item) { if (! is_array($item)) { - return explode("|", $item); + return explode('|', $item); } return $item; @@ -64,14 +62,14 @@ public function getDescriptions(object $class): array public function getParseSchema(array $request): array { $data = []; - $data["schema"] = [ - "type" => "object", - "required" => $this->getRequired($request), - "properties" => $this->getProperties($request), + $data['schema'] = [ + 'type' => 'object', + 'required' => $this->getRequired($request), + 'properties' => $this->getProperties($request), ]; - if (! $this->request["ignoreData"]) { - $data["example"] = $this->request["data"]; + if (! $this->request['ignoreData']) { + $data['example'] = $this->request['data']; } return $data; @@ -79,7 +77,7 @@ public function getParseSchema(array $request): array public function parseRequestBodies(): array { - if (($className = Arr::get($this->request, "rule")) == null) { + if (($className = Arr::get($this->request, 'rule')) == null) { return []; } @@ -94,7 +92,7 @@ public function parseRequestBodies(): array return [ getClassIdentifier($className) => [ - "content" => [ + 'content' => [ $this->contentType => $this->getParseSchema($data), ], ], @@ -114,12 +112,12 @@ private function processRequests(array $rules, array $descriptions): array $data[$name] = $this->getPropertyType($item, $name); - $data[$name]["required"] = RequiredRule::check($item); + $data[$name]['required'] = RequiredRule::check($item); if ($descriptionText = Arr::get($descriptions, $name)) { - $data[$name]["description"] = $descriptionText; + $data[$name]['description'] = $descriptionText; } - }; + } return $data; } @@ -127,14 +125,14 @@ private function processRequests(array $rules, array $descriptions): array private function getRequired(array $rules): Collection { return collect($rules)->filter(function (array $item) { - return $item["required"] == true; + return $item['required'] == true; })->keys(); } private function getProperties(array $data): array { return collect($data)->map(function (array $value) { - return Arr::except($value, "required"); + return Arr::except($value, 'required'); })->toArray(); } } diff --git a/src/Rules/ArrayRule.php b/src/Rules/ArrayRule.php index 8db4c87..602a6e0 100644 --- a/src/Rules/ArrayRule.php +++ b/src/Rules/ArrayRule.php @@ -4,19 +4,16 @@ /** * Class ArrayRule - * - * @package JoBins\APIGenerator\Rules */ class ArrayRule implements RuleContract { /** * Rules that is valid for required. */ - const CONTAIN = ["array"]; + const CONTAIN = ['array']; /** - * @param array $rules - * + * @param array $rules * @return bool */ public static function check(array $rules): bool diff --git a/src/Rules/FileRule.php b/src/Rules/FileRule.php index 620cd46..5875f3e 100644 --- a/src/Rules/FileRule.php +++ b/src/Rules/FileRule.php @@ -9,16 +9,15 @@ class FileRule implements RuleContract /** * Rules that is required to be File. */ - const CONTAIN = ["image"]; + const CONTAIN = ['image']; /** * string that should be included to be a File. */ - const STR_INCLUDES = ["mimes", "mimetypes", "dimensions"]; + const STR_INCLUDES = ['mimes', 'mimetypes', 'dimensions']; /** - * @param array $rules - * + * @param array $rules * @return bool */ public static function check(array $rules): bool diff --git a/src/Rules/IntegerRule.php b/src/Rules/IntegerRule.php index 09a2fbd..419fc8f 100644 --- a/src/Rules/IntegerRule.php +++ b/src/Rules/IntegerRule.php @@ -7,11 +7,10 @@ class IntegerRule implements RuleContract /** * Rules that is valid for required. */ - const CONTAIN = ["integer"]; + const CONTAIN = ['integer']; /** - * @param array $rules - * + * @param array $rules * @return bool */ public static function check(array $rules): bool diff --git a/src/Rules/RequiredRule.php b/src/Rules/RequiredRule.php index 01d242c..3c2b864 100644 --- a/src/Rules/RequiredRule.php +++ b/src/Rules/RequiredRule.php @@ -4,23 +4,21 @@ /** * Class RequiredRule - * @package JoBins\APIGenerator\Rules */ class RequiredRule { /** * Rules that is valid for required. */ - const CONTAIN = ["required"]; + const CONTAIN = ['required']; /** * Rules that shouldn't present to be required. */ - const NOT_CONTAIN = ["sometimes", "nullable"]; + const NOT_CONTAIN = ['sometimes', 'nullable']; /** - * @param array $rules - * + * @param array $rules * @return bool */ public static function check(array $rules): bool diff --git a/src/Rules/RuleContract.php b/src/Rules/RuleContract.php index 4f77e9d..92b2db4 100644 --- a/src/Rules/RuleContract.php +++ b/src/Rules/RuleContract.php @@ -4,8 +4,6 @@ /** * Interface RuleContract - * - * @package JoBins\APIGenerator\Rules */ interface RuleContract { diff --git a/src/Security/Bearer.php b/src/Security/Bearer.php index 73cbf87..e483c49 100644 --- a/src/Security/Bearer.php +++ b/src/Security/Bearer.php @@ -4,17 +4,15 @@ /** * Class Bearer - * - * @package JoBins\APIGenerator\Security */ class Bearer { public function getSchema() { return [ - "name" => "barerToken", - "type" => "http", - "scheme" => "bearer", + 'name' => 'barerToken', + 'type' => 'http', + 'scheme' => 'bearer', ]; } } diff --git a/src/Security/HasParameter.php b/src/Security/HasParameter.php index 3c5293e..134cb97 100644 --- a/src/Security/HasParameter.php +++ b/src/Security/HasParameter.php @@ -8,8 +8,6 @@ /** * Trait HasParameter - * - * @package JoBins\APIGenerator\Security */ trait HasParameter { @@ -18,8 +16,8 @@ trait HasParameter */ public function preparePathWithParam(): array { - $url = Arr::get($this->request, "url"); - $method = Arr::get($this->request, "method"); + $url = Arr::get($this->request, 'url'); + $method = Arr::get($this->request, 'method'); $route = Route::getRoutes()->match(Request::create($url, $method)); @@ -28,48 +26,46 @@ public function preparePathWithParam(): array $parameters = $this->processQuery($queries) + $this->processParameters($route->parameters()); - return ["/".$route->uri(), array_values($parameters)]; + return ['/'.$route->uri(), array_values($parameters)]; } /** - * @param array $queries - * + * @param array $queries * @return array */ public function processQuery(array $queries): array { - $definitions = Arr::get($this->request, "definitions"); + $definitions = Arr::get($this->request, 'definitions'); return collect($queries)->map(function ($value, $param) use ($definitions) { return [ - "in" => "query", - "name" => $param, - "schema" => [ - "type" => is_numeric($value) ? "integer" : "string", + 'in' => 'query', + 'name' => $param, + 'schema' => [ + 'type' => is_numeric($value) ? 'integer' : 'string', ], - "description" => Arr::get($definitions, $param) ?? " ", + 'description' => Arr::get($definitions, $param) ?? ' ', ]; })->toArray(); } /** * @param $parameters - * * @return array */ public function processParameters($parameters): array { - $definitions = Arr::get($this->request, "definitions"); + $definitions = Arr::get($this->request, 'definitions'); return collect($parameters)->map(function ($value, $param) use ($definitions) { return [ - "in" => "path", - "name" => $param, - "schema" => [ - "type" => is_numeric($value) ? "integer" : "string", + 'in' => 'path', + 'name' => $param, + 'schema' => [ + 'type' => is_numeric($value) ? 'integer' : 'string', ], - "required" => true, - "description" => Arr::get($definitions, $param) ?? " ", + 'required' => true, + 'description' => Arr::get($definitions, $param) ?? ' ', ]; })->toArray(); } diff --git a/src/Security/HasResponse.php b/src/Security/HasResponse.php index 0845409..68ec0c0 100644 --- a/src/Security/HasResponse.php +++ b/src/Security/HasResponse.php @@ -9,27 +9,27 @@ trait HasResponse { public function processResponse() { - $schemas = Arr::get($this->request, "responseSchema"); + $schemas = Arr::get($this->request, 'responseSchema'); - $properties = Arr::get($schemas, "define", []); + $properties = Arr::get($schemas, 'define', []); return collect($properties)->map(function ($properties, $attribute) { - $type = "object"; + $type = 'object'; - $refSchemaName = Arr::get($properties, "refSchema"); + $refSchemaName = Arr::get($properties, 'refSchema'); - if (Str::contains($attribute, "*") && $refSchemaName) { + if (Str::contains($attribute, '*') && $refSchemaName) { return [ - "type" => "array", - "items" => [ - "\$ref" => "#/components/schemas/{$refSchemaName}", + 'type' => 'array', + 'items' => [ + '$ref' => "#/components/schemas/{$refSchemaName}", ], ]; } if ($refSchemaName) { return [ - "\$ref" => "#/components/schemas/{$refSchemaName}", + '$ref' => "#/components/schemas/{$refSchemaName}", ]; } if ($this->getSchemaName() && is_array($properties)) { @@ -37,13 +37,13 @@ public function processResponse() $this->defineSchema($schemaName, $type, $properties); return [ - "\$ref" => "#/components/schemas/{$schemaName}", + '$ref' => "#/components/schemas/{$schemaName}", ]; } return [ - "type" => "string", - "description" => $properties, + 'type' => 'string', + 'description' => $properties, ]; })->toArray(); } @@ -55,20 +55,20 @@ public function parseResponse($originalResponseData) $schema = []; $properties = $this->processResponse(); if (! empty($properties)) { - $schema["properties"] = $this->processResponse(); + $schema['properties'] = $this->processResponse(); } $example = json_decode($this->response->getContent(), true) ?? []; if (! empty($example)) { - $schema["example"] = $example; + $schema['example'] = $example; } $responseData = [ $code => [ - "description" => "{$code} status response", - "content" => [ - "application/json" => [ - "schema" => $schema, + 'description' => "{$code} status response", + 'content' => [ + 'application/json' => [ + 'schema' => $schema, ], ], ], @@ -80,11 +80,11 @@ public function parseResponse($originalResponseData) private function defineSchema(string $name, string $type, array $properties) { $schemaData = [ - "type" => $type, - "items" => [ - "type" => "object", + 'type' => $type, + 'items' => [ + 'type' => 'object', ], - "properties" => $this->getSchemaProperties($properties), + 'properties' => $this->getSchemaProperties($properties), ]; data_set($this->data, "components.schemas.{$name}", $schemaData); @@ -94,8 +94,8 @@ private function getSchemaProperties($properties) { return collect($properties)->map(function ($definition) { return [ - "type" => "string", - "description" => $definition, + 'type' => 'string', + 'description' => $definition, ]; })->toArray(); } @@ -112,8 +112,8 @@ private function getResponseSchemas() */ private function getSchemaName() { - $schemas = Arr::get($this->request, "responseSchema"); + $schemas = Arr::get($this->request, 'responseSchema'); - return Arr::get($schemas, "schema"); + return Arr::get($schemas, 'schema'); } } diff --git a/src/Security/HasSecurity.php b/src/Security/HasSecurity.php index 528243b..03bdd3a 100644 --- a/src/Security/HasSecurity.php +++ b/src/Security/HasSecurity.php @@ -8,7 +8,7 @@ trait HasSecurity { public function processSecurity($request): ?array { - $securities = Arr::get($this->request, "security"); + $securities = Arr::get($this->request, 'security'); if (empty($securities)) { return null; @@ -20,7 +20,7 @@ public function processSecurity($request): ?array $this->ensureSecuritySchemaExists($schema); return [ - $schema["name"] => [], + $schema['name'] => [], ]; })->toArray(); } @@ -31,11 +31,11 @@ public function ensureSecuritySchemaExists(array $schema): void if (data_get($this->data, $key)) { return; - }; + } data_set($this->data, $key, [ - "type" => "http", - "scheme" => "bearer", + 'type' => 'http', + 'scheme' => 'bearer', ]); } } diff --git a/src/Security/HasServer.php b/src/Security/HasServer.php index 72be526..8c76bce 100644 --- a/src/Security/HasServer.php +++ b/src/Security/HasServer.php @@ -6,20 +6,17 @@ /** * Trait HasServer - * - * @package JoBins\APIGenerator\Security */ trait HasServer { /** - * @param array $config - * + * @param array $config * @return array */ public function processServer(array $config): array { return [ - Arr::get($config, "servers"), + Arr::get($config, 'servers'), ]; } } diff --git a/src/Services/Generator.php b/src/Services/Generator.php index 81ad31e..9a2a208 100644 --- a/src/Services/Generator.php +++ b/src/Services/Generator.php @@ -12,7 +12,6 @@ /** * Class Generator - * @package JoBins\APIGenerator */ class Generator { @@ -27,20 +26,20 @@ class Generator /** @var */ protected $response = []; - protected string $filePath = ""; + protected string $filePath = ''; - protected string $url = ""; + protected string $url = ''; public function __construct() { $this->initializeFile(); $this->data = array_merge($this->data, [ - "servers" => $this->processServer(config()->get("api-generator")), - "openapi" => config()->get("api-generator.openapi"), - "info" => [ - "title" => config()->get("api-generator.title"), - "version" => config()->get("api-generator.version"), + 'servers' => $this->processServer(config()->get('api-generator')), + 'openapi' => config()->get('api-generator.openapi'), + 'info' => [ + 'title' => config()->get('api-generator.title'), + 'version' => config()->get('api-generator.version'), ], ]); } @@ -50,9 +49,9 @@ public function __construct() */ public function initializeFile(): void { - $this->filePath = config()->get("api-generator.file-path"); + $this->filePath = config()->get('api-generator.file-path'); - $path = pathinfo($this->filePath)["dirname"]; + $path = pathinfo($this->filePath)['dirname']; if (! file_exists($path)) { mkdir($path, 0777, true); @@ -81,11 +80,11 @@ public function generate(): void public function getBasicPathInfo(array $pathData, $parameters): array { - $responseData = Arr::get($pathData, "responses", []); + $responseData = Arr::get($pathData, 'responses', []); $return = $this->getBasicPathInfoData($responseData); - data_set($return, "parameters", $parameters); + data_set($return, 'parameters', $parameters); return $return; } @@ -95,22 +94,22 @@ public function getBasicPathInfoData($responseData) $data = []; if ($security = $this->processSecurity($this->request)) { - $data["security"] = $security; + $data['security'] = $security; } - $data["summary"] = $this->request["summary"]; + $data['summary'] = $this->request['summary']; - if ($tags = Arr::get($this->request, "tags")) { - $data["tags"] = $tags; + if ($tags = Arr::get($this->request, 'tags')) { + $data['tags'] = $tags; } - $data["operationId"] = $this->request["operationID"]; + $data['operationId'] = $this->request['operationID']; if ($requestBody = $this->parseRequestBody()) { - $data["requestBody"] = $requestBody; + $data['requestBody'] = $requestBody; } - $data["responses"] = $this->parseResponse($responseData); + $data['responses'] = $this->parseResponse($responseData); return $data; } @@ -118,7 +117,7 @@ public function getBasicPathInfoData($responseData) private function parseParam() { [$url, $parameters] = $this->preparePathWithParam(); - $method = $this->request["method"]; + $method = $this->request['method']; $pathKey = "paths.{$url}.{$method}"; $pathData = Arr::get($this->data, $pathKey, []); @@ -126,12 +125,12 @@ private function parseParam() data_set($this->data, $pathKey, $pathData); - $requestBodies = Arr::get($this->data, "components.requestBodies", []); + $requestBodies = Arr::get($this->data, 'components.requestBodies', []); $requestBodies = $requestBodies + (new RequestBodyComponent($this->request))->parseRequestBodies(); if (! empty($requestBodies)) { - data_set($this->data, "components.requestBodies", $requestBodies); + data_set($this->data, 'components.requestBodies', $requestBodies); } } } diff --git a/src/Services/ProcessRequestTrait.php b/src/Services/ProcessRequestTrait.php index f81c6cc..d5ea6f7 100644 --- a/src/Services/ProcessRequestTrait.php +++ b/src/Services/ProcessRequestTrait.php @@ -12,8 +12,7 @@ trait ProcessRequestTrait protected array $request; /** - * @param array $request - * + * @param array $request * @return $this */ public function setRequest(array $request): self @@ -25,12 +24,14 @@ public function setRequest(array $request): self private function parseRequestBody(): ?array { - if (! ($className = Arr::get($this->request, "rule"))) { + $requestClass = app($this->request); + + if (! ($className = Arr::get($this->request, 'rule'))) { return null; } return [ - "\$ref" => "#/components/requestBodies/".getClassIdentifier($className), + '$ref' => '#/components/requestBodies/'.getClassIdentifier($className), ]; } } diff --git a/src/Traits/HasDocsGenerator.php b/src/Traits/HasDocsGenerator.php index eb6fafa..2ef992a 100644 --- a/src/Traits/HasDocsGenerator.php +++ b/src/Traits/HasDocsGenerator.php @@ -6,8 +6,6 @@ /** * Class HasDocsGenerator - * - * @package JoBins\APIGenerator */ trait HasDocsGenerator { @@ -74,8 +72,7 @@ trait HasDocsGenerator /** * Operation Id of swagger api's endpoint. * - * @param string $operationId - * + * @param string $operationId * @return $this */ public function setId(string $operationId) @@ -95,8 +92,7 @@ public function setSummary($summary) /** * Set tags for this endpoint. * - * @param array $tags - * + * @param array $tags * @return self */ public function setTags(array $tags) @@ -121,8 +117,7 @@ public function ignoreRequestDataAsExample() } /** - * @param array $security - * + * @param array $security * @return $this */ public function setSecurity(array $security): self @@ -133,8 +128,7 @@ public function setSecurity(array $security): self } /** - * @param array $parameters - * + * @param array $parameters * @return self */ public function defineParameters(array $parameters): self @@ -146,7 +140,6 @@ public function defineParameters(array $parameters): self /** * @param $schema - * * @return $this */ public function defineResponseSchema($schema): self @@ -157,11 +150,10 @@ public function defineResponseSchema($schema): self } /** - * @param string $method - * @param string $uri - * @param array $data - * @param array $headers - * + * @param string $method + * @param string $uri + * @param array $data + * @param array $headers * @return TestResponse */ public function jsond(string $method, string $uri, array $data = [], array $headers = []): TestResponse @@ -183,18 +175,18 @@ public function jsond(string $method, string $uri, array $data = [], array $head public function getParams(): array { return [ - "security" => $this->d_security, - "operationID" => $this->d_operationId, - "summary" => $this->d_summary, - "tags" => $this->d_tags, - "rule" => $this->d_request, - "data" => $this->d_data, - "header" => $this->d_header, - "definitions" => $this->d_definitions, - "url" => $this->d_url, - "method" => $this->d_method, - "ignoreData" => $this->d_ignore_request_data, - "responseSchema" => $this->d_response_schema, + 'security' => $this->d_security, + 'operationID' => $this->d_operationId, + 'summary' => $this->d_summary, + 'tags' => $this->d_tags, + 'rule' => $this->d_request, + 'data' => $this->d_data, + 'header' => $this->d_header, + 'definitions' => $this->d_definitions, + 'url' => $this->d_url, + 'method' => $this->d_method, + 'ignoreData' => $this->d_ignore_request_data, + 'responseSchema' => $this->d_response_schema, ]; } } diff --git a/src/helper.php b/src/helper.php index 605ee59..ede7464 100644 --- a/src/helper.php +++ b/src/helper.php @@ -1,8 +1,7 @@ assertEquals($expected, (new ArrayRule())->check($rules)); + $this->assertEquals($expected, (new ArrayRule)->check($rules)); } public function arrayRuleDataProvider(): array diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 74b4e5d..a34d8c6 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php @@ -14,17 +14,17 @@ public function it_sets_correct_server_config() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") - ->jsond("post", route("posts.store"), []) + $this->setSummary('This is a example route') + ->setId('ExampleRoute') + ->jsond('post', route('posts.store'), []) ->generate($this, true); $json = getJsonFromDocs(); - $this->assertArrayHasKey("url", Arr::get($json, "servers.0")); - $this->assertArrayHasKey("variables", Arr::get($json, "servers.0")); + $this->assertArrayHasKey('url', Arr::get($json, 'servers.0')); + $this->assertArrayHasKey('variables', Arr::get($json, 'servers.0')); - $this->assertEquals(config()->get("api-generator.servers.url"), Arr::get($json, "servers.0.url")); - $this->assertEquals(config()->get("api-generator.servers.variables"), Arr::get($json, "servers.0.variables")); + $this->assertEquals(config()->get('api-generator.servers.url'), Arr::get($json, 'servers.0.url')); + $this->assertEquals(config()->get('api-generator.servers.variables'), Arr::get($json, 'servers.0.variables')); } } diff --git a/tests/FileRuleTest.php b/tests/FileRuleTest.php index 0dfb6d9..389f4d0 100644 --- a/tests/FileRuleTest.php +++ b/tests/FileRuleTest.php @@ -7,8 +7,6 @@ /** * Class FileRuleTest - * - * @package JoBins\APIGenerator\Tests */ class FileRuleTest extends TestCase { @@ -19,7 +17,7 @@ class FileRuleTest extends TestCase */ public function it_validates_file($rules, $expected) { - $this->assertEquals($expected, (new FileRule())->check($rules)); + $this->assertEquals($expected, (new FileRule)->check($rules)); } public function fileRuleDataProvider(): array @@ -27,8 +25,8 @@ public function fileRuleDataProvider(): array return [ [['mimetypes:video/avi,video/mpeg,video/quicktime'], true], [['mimes:jpg,bmp,png'], true], - [["image"], true], - [["dimensions:ratio=3/2"], true], + [['image'], true], + [['dimensions:ratio=3/2'], true], [[Rule::dimensions()->maxWidth(1000)->maxHeight(500)->ratio(3 / 2)], true], ]; } diff --git a/tests/FileSetupTest.php b/tests/FileSetupTest.php index eb56a95..f2117b3 100644 --- a/tests/FileSetupTest.php +++ b/tests/FileSetupTest.php @@ -8,7 +8,6 @@ /** * Class GeneratorTest - * @package JoBins\APIGenerator\Tests */ class FileSetupTest extends TestCase { @@ -17,14 +16,14 @@ class FileSetupTest extends TestCase /** @test */ public function it_generates_api_docs_if_file_does_not_exists() { - $path = config()->get("api-generator.file-path"); + $path = config()->get('api-generator.file-path'); File::delete($path); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(ExampleFormRequest::class) - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); $this->assertFileExists($path); @@ -33,16 +32,16 @@ public function it_generates_api_docs_if_file_does_not_exists() /** @test */ public function it_generates_api_docs_if_directory_does_not_exists() { - $path = config()->get("api-generator.file-path"); + $path = config()->get('api-generator.file-path'); - $directory = pathinfo($path)["dirname"]; + $directory = pathinfo($path)['dirname']; File::deleteDirectory($directory); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(ExampleFormRequest::class) - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); $this->assertFileExists($path); diff --git a/tests/FormRequestRuleTest.php b/tests/FormRequestRuleTest.php index 93f3dde..56bca76 100644 --- a/tests/FormRequestRuleTest.php +++ b/tests/FormRequestRuleTest.php @@ -8,7 +8,6 @@ /** * Class FormRequestRuleTest - * @package JoBins\APIGenerator\Tests */ class FormRequestRuleTest extends TestCase { @@ -19,16 +18,16 @@ public function it_does_not_has_request_bodies_if_form_is_not_present() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") - ->jsond("post", route("posts.store"), []) + $this->setSummary('This is a example route') + ->setId('ExampleRoute') + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); $json = getJsonFromDocs(); - $this->assertNull(Arr::get($json, "paths./api/posts.post.requestBody")); + $this->assertNull(Arr::get($json, 'paths./api/posts.post.requestBody')); } /** @@ -40,17 +39,17 @@ public function all_the__required_params_will_list_in_request_bodies($class, $re { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(RuleExampleFormRequest::class) - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); $schema = getRequestBodyScheme(RuleExampleFormRequest::class); - $this->assertEquals($required, Arr::get($schema, "schema.required")); + $this->assertEquals($required, Arr::get($schema, 'schema.required')); } /** @test */ @@ -58,18 +57,18 @@ public function the_form_request_descriptions_associated_in_body() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(RuleExampleFormRequest::class) - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); $schema = getRequestBodyScheme(RuleExampleFormRequest::class); - $properties = Arr::get($schema, "schema.properties"); + $properties = Arr::get($schema, 'schema.properties'); foreach ($properties as $key => $property) { - $expected = Arr::get((new RuleExampleFormRequest())->descriptions(), $key); + $expected = Arr::get((new RuleExampleFormRequest)->descriptions(), $key); $this->assertEquals($expected, $property['description'] ?? null); } @@ -78,7 +77,7 @@ public function the_form_request_descriptions_associated_in_body() public function requiredRuleDataProvider() { return [ - [RuleExampleFormRequest::class, ["name"]], + [RuleExampleFormRequest::class, ['name']], ]; } } diff --git a/tests/FormRequestTest.php b/tests/FormRequestTest.php index 672d3a2..b603bd5 100644 --- a/tests/FormRequestTest.php +++ b/tests/FormRequestTest.php @@ -10,8 +10,6 @@ /** * Class FormRequestTest - * - * @package JoBins\APIGenerator\Tests */ class FormRequestTest extends TestCase { @@ -22,12 +20,12 @@ public function it_generates_api_docs_even_if_form_request_does_not_exist() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") - ->jsond("post", route("posts.store"), []) + $this->setSummary('This is a example route') + ->setId('ExampleRoute') + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); } /** @test */ @@ -35,13 +33,13 @@ public function it_generates_api_docs_form_request_does_not_have_description_met { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(NoDescriptionFormRequest::class) - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); } /** @test */ @@ -49,14 +47,14 @@ public function it_ignores_examples_if_it_set_to_ignore() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") + $this->setSummary('This is a example route') + ->setId('ExampleRoute') ->setRulesFromFormRequest(NoDescriptionFormRequest::class) ->ignoreRequestDataAsExample() - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); } /** @test */ @@ -64,14 +62,14 @@ public function it_supports_form_request_in_array_and_class() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("FormRequestInArray") + $this->setSummary('This is a example route') + ->setId('FormRequestInArray') ->setRulesFromFormRequest(FormRequestInArray::class) ->ignoreRequestDataAsExample() - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); } /** @test */ @@ -79,23 +77,23 @@ public function it_supports_array_type_data_in_rules() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("FormRequestImageArray") + $this->setSummary('This is a example route') + ->setId('FormRequestImageArray') ->setRulesFromFormRequest(FormRequestImageArray::class) ->ignoreRequestDataAsExample() - ->jsond("post", route("posts.store"), []) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $this->assertFileExists(config()->get("api-generator.file-path")); + $this->assertFileExists(config()->get('api-generator.file-path')); - $schema = getRequestBodyScheme(FormRequestImageArray::class, "multipart/form-data"); + $schema = getRequestBodyScheme(FormRequestImageArray::class, 'multipart/form-data'); - $properties = Arr::get($schema, "schema.properties"); + $properties = Arr::get($schema, 'schema.properties'); - $this->assertEquals("array", Arr::get($properties, "images.type")); - $this->assertEquals(["type" => "string", "format" => "binary"], Arr::get($properties, "images.items")); + $this->assertEquals('array', Arr::get($properties, 'images.type')); + $this->assertEquals(['type' => 'string', 'format' => 'binary'], Arr::get($properties, 'images.items')); - $this->assertEquals("array", Arr::get($properties, "keys.type")); - $this->assertEquals(["type" => "integer"], Arr::get($properties, "keys.items")); + $this->assertEquals('array', Arr::get($properties, 'keys.type')); + $this->assertEquals(['type' => 'integer'], Arr::get($properties, 'keys.items')); } } diff --git a/tests/IntegerRuleTest.php b/tests/IntegerRuleTest.php index 911f584..32dd3ca 100644 --- a/tests/IntegerRuleTest.php +++ b/tests/IntegerRuleTest.php @@ -1,6 +1,5 @@ assertEquals($expected, (new IntegerRule())->check($rules)); + $this->assertEquals($expected, (new IntegerRule)->check($rules)); } public function integerRuleDataProvider(): array diff --git a/tests/PathTest.php b/tests/PathTest.php index 2e7c502..80e0f34 100644 --- a/tests/PathTest.php +++ b/tests/PathTest.php @@ -14,24 +14,24 @@ public function it_does_not_override_the_same_path_for_diff_method() { deleteDocs(); - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->defineParameters([ - "id" => "Numeric ID of user to get details", + 'id' => 'Numeric ID of user to get details', ]) - ->jsond("get", route("users.show", 1)) + ->jsond('get', route('users.show', 1)) ->generate($this, true); - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->defineParameters([ - "id" => "Numeric ID of the user to delete", + 'id' => 'Numeric ID of the user to delete', ]) - ->jsond("delete", route("users.show", 1), []) + ->jsond('delete', route('users.show', 1), []) ->generate($this, true); - $this->assertNotNull(getJsonForEndpoint(route("users.show", 1), "get")); - $this->assertNotNull(getJsonForEndpoint(route("users.destroy", 1), "delete")); + $this->assertNotNull(getJsonForEndpoint(route('users.show', 1), 'get')); + $this->assertNotNull(getJsonForEndpoint(route('users.destroy', 1), 'delete')); } /** @test */ @@ -39,23 +39,23 @@ public function it_merges_the_params_same_path_for_same_path() { deleteDocs(); - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->defineParameters([ - "id" => "Numeric ID of user to get details", + 'id' => 'Numeric ID of user to get details', ]) - ->jsond("get", route("users.show", 1)) + ->jsond('get', route('users.show', 1)) ->generate($this, true); // Second operation should not override the first operation. - $this->jsond("get", route("users.show", 1)) + $this->jsond('get', route('users.show', 1)) ->generate($this, true); - $json = getJsonForEndpoint(route("users.show", 1)); + $json = getJsonForEndpoint(route('users.show', 1)); - $this->assertEquals("User list API.", Arr::get($json, "summary")); - $this->assertEquals("Register", Arr::get($json, "operationId")); + $this->assertEquals('User list API.', Arr::get($json, 'summary')); + $this->assertEquals('Register', Arr::get($json, 'operationId')); - $this->assertEquals("Numeric ID of user to get details", Arr::get($json, "parameters.0.description")); + $this->assertEquals('Numeric ID of user to get details', Arr::get($json, 'parameters.0.description')); } } diff --git a/tests/RequireRuleTest.php b/tests/RequireRuleTest.php index 59193b8..892fe38 100644 --- a/tests/RequireRuleTest.php +++ b/tests/RequireRuleTest.php @@ -6,7 +6,6 @@ /** * Class RequireRuleTest - * @package JoBins\APIGenerator\Tests */ class RequireRuleTest extends TestCase { @@ -17,15 +16,15 @@ class RequireRuleTest extends TestCase */ public function test_required_rule($rules, $expected) { - $this->assertEquals($expected, (new RequiredRule())->check($rules)); + $this->assertEquals($expected, (new RequiredRule)->check($rules)); } public function requiredRuleDataProvider() { return [ - [["required"], true], - [["sometimes", "required"], false], - [["nullable", "required"], false], + [['required'], true], + [['sometimes', 'required'], false], + [['nullable', 'required'], false], ]; } } diff --git a/tests/ResponseSchemeTest.php b/tests/ResponseSchemeTest.php index f74463e..1b68706 100644 --- a/tests/ResponseSchemeTest.php +++ b/tests/ResponseSchemeTest.php @@ -7,8 +7,6 @@ /** * Class ResponseSchemeTest - * - * @package JoBins\APIGenerator\Tests\Stubs */ class ResponseSchemeTest extends TestCase { @@ -20,23 +18,23 @@ public function it_generates_scheme_for_list_responses() deleteDocs(); $responseSchema = [ - "description" => "A User Object", - "define" => [ - "data.*" => ["refSchema" => "UserSchema"], - "message" => "Message for user", + 'description' => 'A User Object', + 'define' => [ + 'data.*' => ['refSchema' => 'UserSchema'], + 'message' => 'Message for user', ], ]; - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->defineResponseSchema($responseSchema) - ->jsond("get", route("users.index")) + ->jsond('get', route('users.index')) ->generate($this, true); - $json = getJsonForEndpoint(route("users.index"), "get"); - $json = Arr::get($json, "responses.200.content.application/json.schema"); + $json = getJsonForEndpoint(route('users.index'), 'get'); + $json = Arr::get($json, 'responses.200.content.application/json.schema'); - $this->assertCount(2, Arr::get($json, "properties")); + $this->assertCount(2, Arr::get($json, 'properties')); } /** @test */ @@ -45,23 +43,23 @@ public function it_generates_scheme_for_detail_responses() deleteDocs(); $responseSchema = [ - "schema" => "UserSchema", - "description" => "A User Object", - "define" => [ - "data" => [ - "name" => "Full name of an User", - "email" => "Email of an User.", + 'schema' => 'UserSchema', + 'description' => 'A User Object', + 'define' => [ + 'data' => [ + 'name' => 'Full name of an User', + 'email' => 'Email of an User.', ], ], ]; - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->defineResponseSchema($responseSchema) - ->jsond("get", route("users.show", 1)) + ->jsond('get', route('users.show', 1)) ->generate($this, true); - $json = getSchema("UserSchema"); - $this->assertCount(2, Arr::get($json, "properties")); + $json = getSchema('UserSchema'); + $this->assertCount(2, Arr::get($json, 'properties')); } } diff --git a/tests/RouteParameterTest.php b/tests/RouteParameterTest.php index b081cbe..595d11e 100644 --- a/tests/RouteParameterTest.php +++ b/tests/RouteParameterTest.php @@ -7,8 +7,6 @@ /** * Class RouteParameterTest - * - * @package JoBins\APIGenerator\Tests */ class RouteParameterTest extends TestCase { @@ -24,33 +22,32 @@ public function setUp(): void /** @test */ public function it_generates_route_parameters() { - $this->setSummary("Register a new user.") - ->setId("UserDetail") + $this->setSummary('Register a new user.') + ->setId('UserDetail') ->defineParameters([ - "id" => "Numeric ID of the user to get", + 'id' => 'Numeric ID of the user to get', ]) - ->jsond("get", route("users.show", ["id" => 1, "from" => "2020-12-12", "to" => "2020-01-12"])) + ->jsond('get', route('users.show', ['id' => 1, 'from' => '2020-12-12', 'to' => '2020-01-12'])) ->assertStatus(200) ->generate($this, true); - $json = getJsonFromDocs(); - $this->assertCount(3, Arr::get($json, "paths./api/users/{id}.get.parameters") ?? []); + $this->assertCount(3, Arr::get($json, 'paths./api/users/{id}.get.parameters') ?? []); } /** @test */ public function it_generates_route_parameters_with_query() { - $this->setSummary("Get a list of Users.") - ->setId("UserList") + $this->setSummary('Get a list of Users.') + ->setId('UserList') ->defineParameters([ - "id" => "Numeric ID of the user to get", + 'id' => 'Numeric ID of the user to get', ]) - ->jsond("get", route("users.index", ["from" => "2020-12-12", "to" => "2020-01-12"])) + ->jsond('get', route('users.index', ['from' => '2020-12-12', 'to' => '2020-01-12'])) ->assertStatus(200) ->generate($this, true); $json = getJsonFromDocs(); - $this->assertCount(2, Arr::get($json, "paths./api/users.get.parameters")); + $this->assertCount(2, Arr::get($json, 'paths./api/users.get.parameters')); } } diff --git a/tests/SecuritySchemaTest.php b/tests/SecuritySchemaTest.php index 12d64a0..7cb6197 100644 --- a/tests/SecuritySchemaTest.php +++ b/tests/SecuritySchemaTest.php @@ -17,15 +17,15 @@ public function it_generates_security_schemas() { deleteDocs(); - $this->setSummary("User list API.") - ->setId("Register") + $this->setSummary('User list API.') + ->setId('Register') ->setSecurity([Bearer::class]) - ->jsond("get", route("users.index")) + ->jsond('get', route('users.index')) ->assertStatus(200) ->generate($this, true); - $json = getJsonForEndpoint(route("users.index")); + $json = getJsonForEndpoint(route('users.index')); - $this->assertCount(1, Arr::get($json, "security")); + $this->assertCount(1, Arr::get($json, 'security')); } } diff --git a/tests/Stubs/ExampleController.php b/tests/Stubs/ExampleController.php index f482b22..2e3a1bd 100644 --- a/tests/Stubs/ExampleController.php +++ b/tests/Stubs/ExampleController.php @@ -6,17 +6,15 @@ /** * Class ExampleController - * @package JoBins\APIGenerator\Tests\Stubs */ class ExampleController { /** - * @param ExampleFormRequest $request - * + * @param ExampleFormRequest $request * @return JsonResponse */ public function index(ExampleFormRequest $request) { - return response()->json(["message" => "This is json response", "data" => []]); + return response()->json(['message' => 'This is json response', 'data' => []]); } } diff --git a/tests/Stubs/ExampleFormRequest.php b/tests/Stubs/ExampleFormRequest.php index 3f983b9..11de52a 100644 --- a/tests/Stubs/ExampleFormRequest.php +++ b/tests/Stubs/ExampleFormRequest.php @@ -6,7 +6,6 @@ /** * Class ExampleFormRequest - * @package JoBins\APIGenerator\Tests\Stubs */ class ExampleFormRequest extends FormRequest { diff --git a/tests/Stubs/FormRequestImageArray.php b/tests/Stubs/FormRequestImageArray.php index a26cf0e..4334c81 100644 --- a/tests/Stubs/FormRequestImageArray.php +++ b/tests/Stubs/FormRequestImageArray.php @@ -7,28 +7,26 @@ /** * Class FormRequestImageArray - * - * @package JoBins\APIGenerator\Tests\Stubs */ class FormRequestImageArray extends FormRequest implements Description { public function rules() { return [ - "profile" => "required|image", - "images" => "nullable|sometimes|array|min:1|max:10", - "images.*" => "image", - "keys" => "array", - "keys.*" => "integer", + 'profile' => 'required|image', + 'images' => 'nullable|sometimes|array|min:1|max:10', + 'images.*' => 'image', + 'keys' => 'array', + 'keys.*' => 'integer', ]; } public function descriptions(): array { return [ - "profiles" => "Profiles", - "images" => "Images only", - "keys.*" => "Asterik", + 'profiles' => 'Profiles', + 'images' => 'Images only', + 'keys.*' => 'Asterik', ]; } } diff --git a/tests/Stubs/FormRequestInArray.php b/tests/Stubs/FormRequestInArray.php index 0baa0c0..10e2263 100644 --- a/tests/Stubs/FormRequestInArray.php +++ b/tests/Stubs/FormRequestInArray.php @@ -7,24 +7,19 @@ /** * Class FormRequestInArray - * - * @package JoBins\APIGenerator\Tests\Stubs */ class FormRequestInArray extends FormRequest { public function rules() { return [ - "email" => ["required", new EmailRule], + 'email' => ['required', new EmailRule], ]; } } - /** * Class EmailRule - * - * @package JoBins\APIGenerator\Tests\Stubs */ class EmailRule implements Rule { diff --git a/tests/Stubs/RuleExampleFormRequest.php b/tests/Stubs/RuleExampleFormRequest.php index 4eefb27..0c54ef3 100644 --- a/tests/Stubs/RuleExampleFormRequest.php +++ b/tests/Stubs/RuleExampleFormRequest.php @@ -9,16 +9,16 @@ class RuleExampleFormRequest extends FormRequest public function rules() { return [ - "name" => "required", - "full_name" => "sometimes|required", - "last_name" => "nullable|required", + 'name' => 'required', + 'full_name' => 'sometimes|required', + 'last_name' => 'nullable|required', ]; } public function descriptions() { return [ - "name" => "Full name of a user", + 'name' => 'Full name of a user', ]; } } diff --git a/tests/Stubs/UsersController.php b/tests/Stubs/UsersController.php index 5e8967b..cd555b4 100644 --- a/tests/Stubs/UsersController.php +++ b/tests/Stubs/UsersController.php @@ -6,8 +6,6 @@ /** * Class UsersController - * - * @package JoBins\APIGenerator\Tests\Stubs */ class UsersController { @@ -18,10 +16,10 @@ public function index(): JsonResponse { return response()->json( [ - "message" => "List of Users", - "data" => [ - ["id" => 1, "name" => "Vedas Bomjon", "email" => "bedram@jobins.jp"], - ["id" => 2, "name" => "Puncoz", "email" => "puncoz@jobins.jp"], + 'message' => 'List of Users', + 'data' => [ + ['id' => 1, 'name' => 'Vedas Bomjon', 'email' => 'bedram@jobins.jp'], + ['id' => 2, 'name' => 'Puncoz', 'email' => 'puncoz@jobins.jp'], ], ] ); @@ -31,14 +29,14 @@ public function show(): JsonResponse { return response()->json( [ - "message" => "Detail of a User.", - "data" => [ - "id" => 1, - "name" => "Vedas Bomjon", - "email" => "bedram@jobins.jp", - "company" => [ - "name" => "JoBins Inc.", - "country" => "Japan", + 'message' => 'Detail of a User.', + 'data' => [ + 'id' => 1, + 'name' => 'Vedas Bomjon', + 'email' => 'bedram@jobins.jp', + 'company' => [ + 'name' => 'JoBins Inc.', + 'country' => 'Japan', ], ], ] diff --git a/tests/TagsTest.php b/tests/TagsTest.php index 7c6fc04..3adb5f9 100644 --- a/tests/TagsTest.php +++ b/tests/TagsTest.php @@ -14,14 +14,14 @@ public function it_assigns_tags_to_endpoints() { deleteDocs(); - $this->setSummary("This is a example route") - ->setId("ExampleRoute") - ->setTags(["Posts"]) - ->jsond("post", route("posts.store"), []) + $this->setSummary('This is a example route') + ->setId('ExampleRoute') + ->setTags(['Posts']) + ->jsond('post', route('posts.store'), []) ->generate($this, true); - $json = getJsonForEndpoint(route("posts.store"), "post"); + $json = getJsonForEndpoint(route('posts.store'), 'post'); - $this->assertEquals(["Posts"], Arr::get($json, "tags")); + $this->assertEquals(['Posts'], Arr::get($json, 'tags')); } } diff --git a/tests/TestCase.php b/tests/TestCase.php index 2c30a73..ca248ac 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -11,15 +11,13 @@ /** * Class TestCase - * @package JoBins\APIGenerator */ class TestCase extends BaseTestCase { /** * Get package providers. * - * @param Application $app - * + * @param Application $app * @return array */ protected function getPackageProviders($app) @@ -34,15 +32,14 @@ protected function getPackageProviders($app) /** * Define routes setup. * - * @param Router $router - * + * @param Router $router * @return void */ protected function defineRoutes($router) { - $router->get("/api/users", [UsersController::class, "index"])->name("users.index"); - $router->get("/api/users/{id}", [UsersController::class, "show"])->name("users.show"); - $router->delete("/api/users/{id}", [UsersController::class, "destroy"])->name("users.destroy"); - $router->post("/api/posts", [ExampleController::class, "index"])->name("posts.store"); + $router->get('/api/users', [UsersController::class, 'index'])->name('users.index'); + $router->get('/api/users/{id}', [UsersController::class, 'show'])->name('users.show'); + $router->delete('/api/users/{id}', [UsersController::class, 'destroy'])->name('users.destroy'); + $router->post('/api/posts', [ExampleController::class, 'index'])->name('posts.store'); } } diff --git a/tests/helper.php b/tests/helper.php index 6047f28..53e5070 100644 --- a/tests/helper.php +++ b/tests/helper.php @@ -7,25 +7,24 @@ function deleteDocs() { - $path = config()->get("api-generator.file-path"); + $path = config()->get('api-generator.file-path'); File::delete($path); } function getJsonFromDocs() { - $path = config()->get("api-generator.file-path"); + $path = config()->get('api-generator.file-path'); return json_decode(file_get_contents($path), true); } /** - * @param $requestClass - * @param string $contentType - * + * @param $requestClass + * @param string $contentType * @return array|ArrayAccess|mixed */ -function getRequestBodyScheme($requestClass, string $contentType = "application/json") +function getRequestBodyScheme($requestClass, string $contentType = 'application/json') { $json = getJsonFromDocs(); @@ -41,7 +40,7 @@ function getSchema(string $name) return Arr::get($json, "components.schemas.{$name}"); } -function getJsonForEndpoint(string $endpoint, $method = "get") +function getJsonForEndpoint(string $endpoint, $method = 'get') { $json = getJsonFromDocs(); From d7a24f2ced0858436f5de55f4ab43f220cd16278 Mon Sep 17 00:00:00 2001 From: Bedram Tamang Date: Tue, 18 Oct 2022 16:58:42 +0545 Subject: [PATCH 2/3] Fix pint github action --- .github/workflows/{php-cs-fixer.yml => php-pint.yml} | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) rename .github/workflows/{php-cs-fixer.yml => php-pint.yml} (79%) diff --git a/.github/workflows/php-cs-fixer.yml b/.github/workflows/php-pint.yml similarity index 79% rename from .github/workflows/php-cs-fixer.yml rename to .github/workflows/php-pint.yml index c9d9cc0..5443957 100644 --- a/.github/workflows/php-cs-fixer.yml +++ b/.github/workflows/php-pint.yml @@ -1,18 +1,20 @@ name: Check & fix styling -on: [push] +on: [ push ] jobs: style: runs-on: ubuntu-latest - + steps: - name: Checkout code uses: actions/checkout@v1 - name: Fix style - uses: docker://oskarstark/php-cs-fixer-ga + uses: aglipanci/laravel-pint-action@1.0.0 with: - args: --config=.php_cs --allow-risky=yes + preset: laravel + verboseMode: true + testMode: true - name: Extract branch name shell: bash run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" From 7c44e6c23de5dd52b0878927b2f1d182d43bd549 Mon Sep 17 00:00:00 2001 From: Bedram Tamang Date: Wed, 19 Oct 2022 09:15:22 +0545 Subject: [PATCH 3/3] Enum support --- .github/workflows/php-pint.yml | 28 ---------------------- composer.json | 7 +++--- config/api-generator.php | 7 ++++-- pint.json | 11 --------- src/Parser/HasPropertyType.php | 20 ++++++++++++---- src/Parser/RequestBodyComponent.php | 18 ++++++++------- src/Rules/EnumRule.php | 32 ++++++++++++++++++++++++++ src/Rules/FileRule.php | 3 --- src/Rules/IntegerRule.php | 4 ---- src/Rules/RequiredRule.php | 4 ---- src/Security/Bearer.php | 4 ++-- src/Security/HasParameter.php | 12 +++++----- src/Security/HasResponse.php | 12 +++++----- src/Security/HasSecurity.php | 2 +- src/Services/Generator.php | 8 +++---- src/Services/ProcessRequestTrait.php | 4 ++-- src/Traits/HasDocsGenerator.php | 22 +++++++++--------- tests/ResponseSchemeTest.php | 10 ++++---- tests/{ => Rules}/ArrayRuleTest.php | 3 ++- tests/Rules/EnumTest.php | 19 +++++++++++++++ tests/{ => Rules}/FileRuleTest.php | 3 ++- tests/{ => Rules}/IntegerRuleTest.php | 2 ++ tests/{ => Rules}/RequireRuleTest.php | 3 ++- tests/Stubs/ExampleFormRequest.php | 4 +++- tests/Stubs/FormRequestImageArray.php | 12 +++++----- tests/Stubs/RuleExampleFormRequest.php | 2 +- tests/Stubs/UsersController.php | 12 +++++----- 27 files changed, 145 insertions(+), 123 deletions(-) delete mode 100644 .github/workflows/php-pint.yml delete mode 100644 pint.json create mode 100644 src/Rules/EnumRule.php rename tests/{ => Rules}/ArrayRuleTest.php (83%) create mode 100644 tests/Rules/EnumTest.php rename tests/{ => Rules}/FileRuleTest.php (89%) rename tests/{ => Rules}/IntegerRuleTest.php (92%) rename tests/{ => Rules}/RequireRuleTest.php (87%) diff --git a/.github/workflows/php-pint.yml b/.github/workflows/php-pint.yml deleted file mode 100644 index 5443957..0000000 --- a/.github/workflows/php-pint.yml +++ /dev/null @@ -1,28 +0,0 @@ -name: Check & fix styling - -on: [ push ] - -jobs: - style: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v1 - - name: Fix style - uses: aglipanci/laravel-pint-action@1.0.0 - with: - preset: laravel - verboseMode: true - testMode: true - - name: Extract branch name - shell: bash - run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" - id: extract_branch - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v2.3.0 - with: - commit_message: Fix styling - branch: ${{ steps.extract_branch.outputs.branch }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/composer.json b/composer.json index d6106d1..5a79e80 100644 --- a/composer.json +++ b/composer.json @@ -32,8 +32,8 @@ "scripts": { "test": "vendor/bin/phpunit", "psalm": "vendor/bin/psalm", - "test-coverage": "vendor/bin/phpunit --coverage-html coverage", - "format": "vendor/bin/pint" + "pint": "vendor/bin/pint", + "test-coverage": "vendor/bin/phpunit --coverage-html coverage" }, "autoload-dev": { "psr-4": { @@ -46,8 +46,7 @@ "require-dev": { "orchestra/testbench": "^6.15", "phpunit/phpunit": "^9.0", - "vimeo/psalm": "^4.7", - "laravel/pint": "^1.2" + "vimeo/psalm": "^4.7" }, "extra": { "laravel": { diff --git a/config/api-generator.php b/config/api-generator.php index 9f0d9bd..0ba7665 100644 --- a/config/api-generator.php +++ b/config/api-generator.php @@ -3,9 +3,12 @@ return [ 'servers' => [ 'url' => 'https://{environment}.localhost.test', + 'variables' => [ 'environment' => [ + 'default' => 'api', + 'enum' => [ 'api', 'api.dev', @@ -16,8 +19,8 @@ ], 'openapi' => '3.0.0', - 'title' => 'Reaching APP API', + 'title' => 'Reaching APP API', 'version' => '1.0.0', - 'file-path' => storage_path('api-docs/api-docs.json'), + 'file-path' => public_path('api-docs/api-docs.json'), ]; diff --git a/pint.json b/pint.json deleted file mode 100644 index cf5f303..0000000 --- a/pint.json +++ /dev/null @@ -1,11 +0,0 @@ -{ - "preset": "laravel", - "rules": { - "simplified_null_return": true, - "braces": false, - "new_with_braces": { - "anonymous_class": false, - "named_class": false - } - } -} diff --git a/src/Parser/HasPropertyType.php b/src/Parser/HasPropertyType.php index 87218f0..55858d5 100644 --- a/src/Parser/HasPropertyType.php +++ b/src/Parser/HasPropertyType.php @@ -4,6 +4,7 @@ use Illuminate\Support\Arr; use JoBins\APIGenerator\Rules\ArrayRule; +use JoBins\APIGenerator\Rules\EnumRule; use JoBins\APIGenerator\Rules\FileRule; use JoBins\APIGenerator\Rules\IntegerRule; @@ -22,15 +23,16 @@ trait HasPropertyType /** * @param array $rules * @param string $name + * * @return array|string[] */ public function getPropertyType(array $rules, string $name): array { - if (ArrayRule::check($rules)) { + if ( ArrayRule::check($rules) ) { $rules = Arr::get($this->rulesArray, "{$name}.*"); return [ - 'type' => 'array', + 'type' => 'array', 'items' => $this->getPropertyItemType($rules), ]; } @@ -48,21 +50,29 @@ public function setRulesArray(array $rulesArray): void private function getPropertyItemType(array $rules): array { - if (FileRule::check($rules)) { + if ( FileRule::check($rules) ) { $this->contentType = 'multipart/form-data'; return [ - 'type' => 'string', + 'type' => 'string', 'format' => 'binary', ]; } - if (IntegerRule::check($rules)) { + if ( IntegerRule::check($rules) ) { return [ 'type' => 'integer', ]; } + $enums = EnumRule::data($rules); + if ( count($enums) > 0 ) { + return [ + 'type' => 'string', + 'enum' => $enums, + ]; + } + return [ 'type' => 'string', ]; diff --git a/src/Parser/RequestBodyComponent.php b/src/Parser/RequestBodyComponent.php index 6b231da..d0fa19f 100644 --- a/src/Parser/RequestBodyComponent.php +++ b/src/Parser/RequestBodyComponent.php @@ -41,12 +41,14 @@ public function getRules(object $class): array return []; } - return collect($class->rules())->map(function ($item) { - if (! is_array($item)) { - return explode('|', $item); + return collect($class->rules())->map(function ($rules) { + if (! is_array($rules)) { + return explode('|', $rules); } - return $item; + return collect($rules)->filter(function ($item) { + return is_string($item); + }); })->toArray(); } @@ -61,10 +63,10 @@ public function getDescriptions(object $class): array public function getParseSchema(array $request): array { - $data = []; + $data = []; $data['schema'] = [ - 'type' => 'object', - 'required' => $this->getRequired($request), + 'type' => 'object', + 'required' => $this->getRequired($request), 'properties' => $this->getProperties($request), ]; @@ -82,7 +84,7 @@ public function parseRequestBodies(): array } /** @var FormRequest $class */ - $class = (new $className); + $class = new $className; // Set rules of a class for global access. $rules = $this->getRules($class); diff --git a/src/Rules/EnumRule.php b/src/Rules/EnumRule.php new file mode 100644 index 0000000..7bc6880 --- /dev/null +++ b/src/Rules/EnumRule.php @@ -0,0 +1,32 @@ +filter(function ($item) { - return is_string($item); - })->toArray(); - if (count(array_intersect(self::CONTAIN, $rules)) > 0) { return true; } diff --git a/src/Rules/RequiredRule.php b/src/Rules/RequiredRule.php index 3c2b864..8a1626e 100644 --- a/src/Rules/RequiredRule.php +++ b/src/Rules/RequiredRule.php @@ -23,10 +23,6 @@ class RequiredRule */ public static function check(array $rules): bool { - $rules = collect($rules)->filter(function ($item) { - return is_string($item); - })->toArray(); - if (count(array_intersect(self::CONTAIN, $rules)) == 0) { return false; } diff --git a/src/Security/Bearer.php b/src/Security/Bearer.php index e483c49..bd058c9 100644 --- a/src/Security/Bearer.php +++ b/src/Security/Bearer.php @@ -10,8 +10,8 @@ class Bearer public function getSchema() { return [ - 'name' => 'barerToken', - 'type' => 'http', + 'name' => 'barerToken', + 'type' => 'http', 'scheme' => 'bearer', ]; } diff --git a/src/Security/HasParameter.php b/src/Security/HasParameter.php index 134cb97..0bfe155 100644 --- a/src/Security/HasParameter.php +++ b/src/Security/HasParameter.php @@ -16,7 +16,7 @@ trait HasParameter */ public function preparePathWithParam(): array { - $url = Arr::get($this->request, 'url'); + $url = Arr::get($this->request, 'url'); $method = Arr::get($this->request, 'method'); $route = Route::getRoutes()->match(Request::create($url, $method)); @@ -39,8 +39,8 @@ public function processQuery(array $queries): array return collect($queries)->map(function ($value, $param) use ($definitions) { return [ - 'in' => 'query', - 'name' => $param, + 'in' => 'query', + 'name' => $param, 'schema' => [ 'type' => is_numeric($value) ? 'integer' : 'string', ], @@ -59,12 +59,12 @@ public function processParameters($parameters): array return collect($parameters)->map(function ($value, $param) use ($definitions) { return [ - 'in' => 'path', - 'name' => $param, + 'in' => 'path', + 'name' => $param, 'schema' => [ 'type' => is_numeric($value) ? 'integer' : 'string', ], - 'required' => true, + 'required' => true, 'description' => Arr::get($definitions, $param) ?? ' ', ]; })->toArray(); diff --git a/src/Security/HasResponse.php b/src/Security/HasResponse.php index 68ec0c0..2379bac 100644 --- a/src/Security/HasResponse.php +++ b/src/Security/HasResponse.php @@ -20,7 +20,7 @@ public function processResponse() if (Str::contains($attribute, '*') && $refSchemaName) { return [ - 'type' => 'array', + 'type' => 'array', 'items' => [ '$ref' => "#/components/schemas/{$refSchemaName}", ], @@ -42,7 +42,7 @@ public function processResponse() } return [ - 'type' => 'string', + 'type' => 'string', 'description' => $properties, ]; })->toArray(); @@ -52,7 +52,7 @@ public function parseResponse($originalResponseData) { $code = $this->response->getStatusCode(); - $schema = []; + $schema = []; $properties = $this->processResponse(); if (! empty($properties)) { $schema['properties'] = $this->processResponse(); @@ -66,7 +66,7 @@ public function parseResponse($originalResponseData) $responseData = [ $code => [ 'description' => "{$code} status response", - 'content' => [ + 'content' => [ 'application/json' => [ 'schema' => $schema, ], @@ -80,7 +80,7 @@ public function parseResponse($originalResponseData) private function defineSchema(string $name, string $type, array $properties) { $schemaData = [ - 'type' => $type, + 'type' => $type, 'items' => [ 'type' => 'object', ], @@ -94,7 +94,7 @@ private function getSchemaProperties($properties) { return collect($properties)->map(function ($definition) { return [ - 'type' => 'string', + 'type' => 'string', 'description' => $definition, ]; })->toArray(); diff --git a/src/Security/HasSecurity.php b/src/Security/HasSecurity.php index 03bdd3a..86870c1 100644 --- a/src/Security/HasSecurity.php +++ b/src/Security/HasSecurity.php @@ -34,7 +34,7 @@ public function ensureSecuritySchemaExists(array $schema): void } data_set($this->data, $key, [ - 'type' => 'http', + 'type' => 'http', 'scheme' => 'bearer', ]); } diff --git a/src/Services/Generator.php b/src/Services/Generator.php index 9a2a208..7bd86b0 100644 --- a/src/Services/Generator.php +++ b/src/Services/Generator.php @@ -37,8 +37,8 @@ public function __construct() $this->data = array_merge($this->data, [ 'servers' => $this->processServer(config()->get('api-generator')), 'openapi' => config()->get('api-generator.openapi'), - 'info' => [ - 'title' => config()->get('api-generator.title'), + 'info' => [ + 'title' => config()->get('api-generator.title'), 'version' => config()->get('api-generator.version'), ], ]); @@ -117,8 +117,8 @@ public function getBasicPathInfoData($responseData) private function parseParam() { [$url, $parameters] = $this->preparePathWithParam(); - $method = $this->request['method']; - $pathKey = "paths.{$url}.{$method}"; + $method = $this->request['method']; + $pathKey = "paths.{$url}.{$method}"; $pathData = Arr::get($this->data, $pathKey, []); $pathData = $pathData + $this->getBasicPathInfo($pathData, $parameters); diff --git a/src/Services/ProcessRequestTrait.php b/src/Services/ProcessRequestTrait.php index d5ea6f7..061eae6 100644 --- a/src/Services/ProcessRequestTrait.php +++ b/src/Services/ProcessRequestTrait.php @@ -24,9 +24,9 @@ public function setRequest(array $request): self private function parseRequestBody(): ?array { - $requestClass = app($this->request); + $className = Arr::get($this->request, 'rule'); - if (! ($className = Arr::get($this->request, 'rule'))) { + if (empty($className)) { return null; } diff --git a/src/Traits/HasDocsGenerator.php b/src/Traits/HasDocsGenerator.php index 2ef992a..d944eaa 100644 --- a/src/Traits/HasDocsGenerator.php +++ b/src/Traits/HasDocsGenerator.php @@ -175,17 +175,17 @@ public function jsond(string $method, string $uri, array $data = [], array $head public function getParams(): array { return [ - 'security' => $this->d_security, - 'operationID' => $this->d_operationId, - 'summary' => $this->d_summary, - 'tags' => $this->d_tags, - 'rule' => $this->d_request, - 'data' => $this->d_data, - 'header' => $this->d_header, - 'definitions' => $this->d_definitions, - 'url' => $this->d_url, - 'method' => $this->d_method, - 'ignoreData' => $this->d_ignore_request_data, + 'security' => $this->d_security, + 'operationID' => $this->d_operationId, + 'summary' => $this->d_summary, + 'tags' => $this->d_tags, + 'rule' => $this->d_request, + 'data' => $this->d_data, + 'header' => $this->d_header, + 'definitions' => $this->d_definitions, + 'url' => $this->d_url, + 'method' => $this->d_method, + 'ignoreData' => $this->d_ignore_request_data, 'responseSchema' => $this->d_response_schema, ]; } diff --git a/tests/ResponseSchemeTest.php b/tests/ResponseSchemeTest.php index 1b68706..7f0e45e 100644 --- a/tests/ResponseSchemeTest.php +++ b/tests/ResponseSchemeTest.php @@ -19,8 +19,8 @@ public function it_generates_scheme_for_list_responses() $responseSchema = [ 'description' => 'A User Object', - 'define' => [ - 'data.*' => ['refSchema' => 'UserSchema'], + 'define' => [ + 'data.*' => ['refSchema' => 'UserSchema'], 'message' => 'Message for user', ], ]; @@ -43,11 +43,11 @@ public function it_generates_scheme_for_detail_responses() deleteDocs(); $responseSchema = [ - 'schema' => 'UserSchema', + 'schema' => 'UserSchema', 'description' => 'A User Object', - 'define' => [ + 'define' => [ 'data' => [ - 'name' => 'Full name of an User', + 'name' => 'Full name of an User', 'email' => 'Email of an User.', ], ], diff --git a/tests/ArrayRuleTest.php b/tests/Rules/ArrayRuleTest.php similarity index 83% rename from tests/ArrayRuleTest.php rename to tests/Rules/ArrayRuleTest.php index 416c61b..2b1f828 100644 --- a/tests/ArrayRuleTest.php +++ b/tests/Rules/ArrayRuleTest.php @@ -1,8 +1,9 @@ assertTrue(EnumRule::check(['in:football,volleyball'])); + $this->assertEquals(['football', 'volleyball'], EnumRule::data(['in:football,volleyball'])); + } +} diff --git a/tests/FileRuleTest.php b/tests/Rules/FileRuleTest.php similarity index 89% rename from tests/FileRuleTest.php rename to tests/Rules/FileRuleTest.php index 389f4d0..c18ca25 100644 --- a/tests/FileRuleTest.php +++ b/tests/Rules/FileRuleTest.php @@ -1,9 +1,10 @@ 'required|in:football,cricket', + ]; } public function descriptions() diff --git a/tests/Stubs/FormRequestImageArray.php b/tests/Stubs/FormRequestImageArray.php index 4334c81..3f20c27 100644 --- a/tests/Stubs/FormRequestImageArray.php +++ b/tests/Stubs/FormRequestImageArray.php @@ -13,11 +13,11 @@ class FormRequestImageArray extends FormRequest implements Description public function rules() { return [ - 'profile' => 'required|image', - 'images' => 'nullable|sometimes|array|min:1|max:10', + 'profile' => 'required|image', + 'images' => 'nullable|sometimes|array|min:1|max:10', 'images.*' => 'image', - 'keys' => 'array', - 'keys.*' => 'integer', + 'keys' => 'array', + 'keys.*' => 'integer', ]; } @@ -25,8 +25,8 @@ public function descriptions(): array { return [ 'profiles' => 'Profiles', - 'images' => 'Images only', - 'keys.*' => 'Asterik', + 'images' => 'Images only', + 'keys.*' => 'Asterik', ]; } } diff --git a/tests/Stubs/RuleExampleFormRequest.php b/tests/Stubs/RuleExampleFormRequest.php index 0c54ef3..8efabaf 100644 --- a/tests/Stubs/RuleExampleFormRequest.php +++ b/tests/Stubs/RuleExampleFormRequest.php @@ -9,7 +9,7 @@ class RuleExampleFormRequest extends FormRequest public function rules() { return [ - 'name' => 'required', + 'name' => 'required', 'full_name' => 'sometimes|required', 'last_name' => 'nullable|required', ]; diff --git a/tests/Stubs/UsersController.php b/tests/Stubs/UsersController.php index cd555b4..6ae0908 100644 --- a/tests/Stubs/UsersController.php +++ b/tests/Stubs/UsersController.php @@ -17,7 +17,7 @@ public function index(): JsonResponse return response()->json( [ 'message' => 'List of Users', - 'data' => [ + 'data' => [ ['id' => 1, 'name' => 'Vedas Bomjon', 'email' => 'bedram@jobins.jp'], ['id' => 2, 'name' => 'Puncoz', 'email' => 'puncoz@jobins.jp'], ], @@ -30,12 +30,12 @@ public function show(): JsonResponse return response()->json( [ 'message' => 'Detail of a User.', - 'data' => [ - 'id' => 1, - 'name' => 'Vedas Bomjon', - 'email' => 'bedram@jobins.jp', + 'data' => [ + 'id' => 1, + 'name' => 'Vedas Bomjon', + 'email' => 'bedram@jobins.jp', 'company' => [ - 'name' => 'JoBins Inc.', + 'name' => 'JoBins Inc.', 'country' => 'Japan', ], ],