diff --git a/.github/.kodiak.toml b/.github/.kodiak.toml deleted file mode 100644 index 60c34b6..0000000 --- a/.github/.kodiak.toml +++ /dev/null @@ -1,10 +0,0 @@ -version = 1 - -[merge] -automerge_label = "automerge" -blacklist_title_regex = "^WIP.*" -blacklist_labels = ["WIP"] -method = "rebase" -delete_branch_on_merge = true -notify_on_conflict = true -optimistic_updates = false diff --git a/.github/workflows/codesniffer.yml b/.github/workflows/codesniffer.yml index a58ac4f..590394f 100644 --- a/.github/workflows/codesniffer.yml +++ b/.github/workflows/codesniffer.yml @@ -15,4 +15,4 @@ jobs: name: "Codesniffer" uses: contributte/.github/.github/workflows/codesniffer.yml@master with: - php: "8.2" + php: "8.4" diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml index 860c47e..c51c356 100644 --- a/.github/workflows/coverage.yml +++ b/.github/workflows/coverage.yml @@ -8,11 +8,11 @@ on: branches: ["*"] schedule: - - cron: "0 8 * * 1" + - cron: "0 9 * * 1" jobs: coverage: name: "Nette Tester" uses: contributte/.github/.github/workflows/nette-tester-coverage-v2.yml@master with: - php: "8.2" + php: "8.4" diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index eb916bf..cd37eba 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -8,11 +8,11 @@ on: branches: ["*"] schedule: - - cron: "0 8 * * 1" + - cron: "0 10 * * 1" jobs: phpstan: name: "Phpstan" uses: contributte/.github/.github/workflows/phpstan.yml@master with: - php: "8.2" + php: "8.4" diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0bdeea0..ef17693 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -5,10 +5,10 @@ on: workflow_dispatch: push: - branches: [ "*" ] + branches: ["*"] schedule: - - cron: "0 8 * * 1" + - cron: "0 10 * * 1" jobs: test85: diff --git a/LICENSE b/LICENSE index 8d91c7a..9a5001e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2023 Contributte +Copyright (c) 2025 Contributte Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 4c74a0c..233d8db 100644 --- a/README.md +++ b/README.md @@ -1,18 +1,17 @@ -![](https://heatbadger.now.sh/github/readme/contributte/openapi/) +![](https://heatbadger.vercel.app/github/readme/contributte/openapi/)

- - - + + +

- - -

+ +

Website 🚀 contributte.org | Contact 👨🏻‍💻 f3l1x.io | Twitter 🐦 @contributte @@ -30,12 +29,12 @@ composer require contributte/openapi For details on how to use this package, check out our [documentation](.docs). -## Version +## Versions -| State | Version | Branch | Nette | PHP | -|-------------|---------|----------|------|---------| -| dev | `^0.2` | `master` | 4.0+ | `>=8.2` | -| stable | `^0.1` | `master` | 4.0+ | `>=8.1` | +| State | Version | Branch | Nette | PHP | +|-------------|---------|----------|--------|---------| +| dev | `^0.2` | `master` | `3.2+` | `>=8.2` | +| stable | `^0.1` | `master` | `3.2+` | `>=8.1` | ## Development diff --git a/composer.json b/composer.json index 1638e20..96ac27a 100644 --- a/composer.json +++ b/composer.json @@ -23,9 +23,9 @@ "nette/utils": "^4.0.0" }, "require-dev": { - "contributte/qa": "^0.4", - "contributte/tester": "^0.4", - "contributte/phpstan": "^0.2", + "contributte/qa": "^0.4.0", + "contributte/tester": "^0.4.0", + "contributte/phpstan": "^0.2.0", "tracy/tracy": "^2.11.0", "symfony/yaml": "^6.4.0 || ^7.0.0 || ^8.0.0" }, diff --git a/phpstan.neon b/phpstan.neon index e99d19e..cd64186 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -2,7 +2,7 @@ includes: - vendor/contributte/phpstan/phpstan.neon parameters: - level: 8 + level: 9 phpVersion: 80200 scanDirectories: @@ -14,5 +14,3 @@ parameters: paths: - src - .docs - - ignoreErrors: diff --git a/src/Schema/Components.php b/src/Schema/Components.php index 31f4120..d0f2fa3 100644 --- a/src/Schema/Components.php +++ b/src/Schema/Components.php @@ -44,7 +44,9 @@ public static function fromArray(array $data): Components { $components = new Components(); - foreach ($data['schemas'] ?? [] as $schemaKey => $schemaData) { + /** @var array $schemas */ + $schemas = $data['schemas'] ?? []; + foreach ($schemas as $schemaKey => $schemaData) { if (isset($schemaData['$ref'])) { $components->setSchema($schemaKey, Reference::fromArray($schemaData)); } else { @@ -52,31 +54,47 @@ public static function fromArray(array $data): Components } } - foreach ($data['responses'] ?? [] as $responseKey => $responseData) { + /** @var array $responses */ + $responses = $data['responses'] ?? []; + foreach ($responses as $responseKey => $responseData) { if (isset($responseData['$ref'])) { - $components->setResponse((string) $responseKey, Reference::fromArray($responseData)); + $components->setResponse($responseKey, Reference::fromArray($responseData)); } else { - $components->setResponse((string) $responseKey, Response::fromArray($responseData)); + $components->setResponse($responseKey, Response::fromArray($responseData)); } } foreach ($data['parameters'] ?? [] as $parameterKey => $parameterData) { + if (!is_array($parameterData)) { + continue; + } + if (isset($parameterData['$ref'])) { $components->setParameter($parameterKey, Reference::fromArray($parameterData)); } else { - $components->setParameter($parameterKey, Parameter::fromArray($parameterData)); + /** @var array{name: string, in: string, description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $param */ + $param = $parameterData; + $components->setParameter($parameterKey, Parameter::fromArray($param)); } } foreach ($data['examples'] ?? [] as $exampleKey => $exampleData) { + if (!is_array($exampleData)) { + continue; + } + if (isset($exampleData['$ref'])) { $components->setExample($exampleKey, Reference::fromArray($exampleData)); } else { - $components->setExample($exampleKey, Example::fromArray($exampleData)); + /** @var array{summary?: string, description?: string, value?: mixed, externalValue?: string} $example */ + $example = $exampleData; + $components->setExample($exampleKey, Example::fromArray($example)); } } - foreach ($data['requestBodies'] ?? [] as $requestBodyKey => $requestBodyData) { + /** @var array $requestBodies */ + $requestBodies = $data['requestBodies'] ?? []; + foreach ($requestBodies as $requestBodyKey => $requestBodyData) { if (isset($requestBodyData['$ref'])) { $components->setRequestBody($requestBodyKey, Reference::fromArray($requestBodyData)); } else { @@ -85,14 +103,22 @@ public static function fromArray(array $data): Components } foreach ($data['headers'] ?? [] as $headerKey => $headerData) { + if (!is_array($headerData)) { + continue; + } + if (isset($headerData['$ref'])) { $components->setHeader($headerKey, Reference::fromArray($headerData)); } else { - $components->setHeader($headerKey, Header::fromArray($headerData)); + /** @var array{description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $header */ + $header = $headerData; + $components->setHeader($headerKey, Header::fromArray($header)); } } - foreach ($data['securitySchemes'] ?? [] as $securitySchemeKey => $securitySchemeData) { + /** @var array $securitySchemes */ + $securitySchemes = $data['securitySchemes'] ?? []; + foreach ($securitySchemes as $securitySchemeKey => $securitySchemeData) { if (isset($securitySchemeData['$ref'])) { $components->setSecurityScheme($securitySchemeKey, Reference::fromArray($securitySchemeData)); } else { @@ -109,14 +135,22 @@ public static function fromArray(array $data): Components } foreach ($data['links'] ?? [] as $linkKey => $linkData) { + if (!is_array($linkData)) { + continue; + } + if (isset($linkData['$ref'])) { $components->setLink($linkKey, Reference::fromArray($linkData)); } else { - $components->setLink($linkKey, Link::fromArray($linkData)); + /** @var array{operationRef?: string, operationId?: string, parameters?: mixed[], requestBody?: mixed, description?: string, server?: array{url: string, description?: string, variables?: array, description?: string}>}} $link */ + $link = $linkData; + $components->setLink($linkKey, Link::fromArray($link)); } } - foreach ($data['pathItems'] ?? [] as $pathItemKey => $pathItemData) { + /** @var array $pathItems */ + $pathItems = $data['pathItems'] ?? []; + foreach ($pathItems as $pathItemKey => $pathItemData) { if (isset($pathItemData['$ref'])) { $components->setPathItem($pathItemKey, Reference::fromArray($pathItemData)); } else { diff --git a/src/Schema/Contact.php b/src/Schema/Contact.php index 9dfb815..1e0d2d6 100644 --- a/src/Schema/Contact.php +++ b/src/Schema/Contact.php @@ -14,7 +14,7 @@ class Contact private ?VendorExtensions $vendorExtensions = null; /** - * @param mixed[] $data + * @param array{name?: string, url?: string, email?: string} $data */ public static function fromArray(array $data): Contact { diff --git a/src/Schema/Encoding.php b/src/Schema/Encoding.php index 8d89d6b..035f71b 100644 --- a/src/Schema/Encoding.php +++ b/src/Schema/Encoding.php @@ -25,13 +25,21 @@ public static function fromArray(array $data): self { $encoding = new Encoding(); - $encoding->contentType = $data['contentType'] ?? null; + /** @var string|null $contentType */ + $contentType = $data['contentType'] ?? null; + $encoding->contentType = $contentType; foreach ($data['headers'] ?? [] as $name => $header) { + if (!is_array($header)) { + continue; + } + if (isset($header['$ref'])) { $encoding->addHeader($name, Reference::fromArray($header)); } else { - $encoding->addHeader($name, Header::fromArray($header)); + /** @var array{description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $headerData */ + $headerData = $header; + $encoding->addHeader($name, Header::fromArray($headerData)); } } diff --git a/src/Schema/Example.php b/src/Schema/Example.php index 1f3beef..e11f215 100644 --- a/src/Schema/Example.php +++ b/src/Schema/Example.php @@ -16,7 +16,7 @@ class Example private ?VendorExtensions $vendorExtensions = null; /** - * @param mixed[] $data + * @param array{summary?: string, description?: string, value?: mixed, externalValue?: string} $data */ public static function fromArray(array $data): self { diff --git a/src/Schema/ExternalDocumentation.php b/src/Schema/ExternalDocumentation.php index f69a1a9..c12edf9 100644 --- a/src/Schema/ExternalDocumentation.php +++ b/src/Schema/ExternalDocumentation.php @@ -17,7 +17,7 @@ public function __construct(string $url) } /** - * @param mixed[] $data + * @param array{url: string, description?: string} $data */ public static function fromArray(array $data): ExternalDocumentation { diff --git a/src/Schema/Header.php b/src/Schema/Header.php index 4410d16..65ae187 100644 --- a/src/Schema/Header.php +++ b/src/Schema/Header.php @@ -27,7 +27,7 @@ class Header private array $examples = []; /** - * @param mixed[] $data + * @param array{description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: mixed[]} $data */ public static function fromArray(array $data): Header { diff --git a/src/Schema/Info.php b/src/Schema/Info.php index aff7a49..6dc6c23 100644 --- a/src/Schema/Info.php +++ b/src/Schema/Info.php @@ -28,7 +28,7 @@ public function __construct(string $title, string $version) } /** - * @param mixed[] $data + * @param array{title: string, version: string, summary?: string, description?: string, termsOfService?: string, license?: array{name: string, identifier?: string, url?: string}, contact?: array{name?: string, url?: string, email?: string}} $data */ public static function fromArray(array $data): Info { diff --git a/src/Schema/License.php b/src/Schema/License.php index 3df764b..c1a14b7 100644 --- a/src/Schema/License.php +++ b/src/Schema/License.php @@ -19,7 +19,7 @@ public function __construct(string $name) } /** - * @param mixed[] $data + * @param array{name: string, identifier?: string, url?: string} $data */ public static function fromArray(array $data): License { diff --git a/src/Schema/Link.php b/src/Schema/Link.php index b0e5dd7..054e6a9 100644 --- a/src/Schema/Link.php +++ b/src/Schema/Link.php @@ -21,7 +21,7 @@ class Link private ?VendorExtensions $vendorExtensions = null; /** - * @param mixed[] $data + * @param array{operationRef?: string, operationId?: string, parameters?: mixed[], requestBody?: mixed, description?: string, server?: array{url: string, description?: string, variables?: array}} $data */ public static function fromArray(array $data): Link { diff --git a/src/Schema/MediaType.php b/src/Schema/MediaType.php index 6e5f671..f56ba19 100644 --- a/src/Schema/MediaType.php +++ b/src/Schema/MediaType.php @@ -25,30 +25,38 @@ public static function fromArray(array $data): MediaType $mediaType = new MediaType(); if (isset($data['schema'])) { - if (isset($data['schema']['$ref'])) { - $mediaType->setSchema(Reference::fromArray($data['schema'])); + /** @var mixed[] $schema */ + $schema = $data['schema']; + if (isset($schema['$ref'])) { + $mediaType->setSchema(Reference::fromArray($schema)); } else { - $mediaType->setSchema(Schema::fromArray($data['schema'])); + $mediaType->setSchema(Schema::fromArray($schema)); } } $mediaType->setExample($data['example'] ?? null); - if (isset($data['examples'])) { - foreach ($data['examples'] as $name => $example) { - if (isset($example['$ref'])) { - $mediaType->addExample($name, Reference::fromArray($example)); - } else { - $mediaType->addExample($name, Example::fromArray($example)); - } + foreach ($data['examples'] ?? [] as $name => $example) { + if (!is_array($example)) { + continue; + } + + if (isset($example['$ref'])) { + $mediaType->addExample($name, Reference::fromArray($example)); + } else { + /** @var array{summary?: string, description?: string, value?: mixed, externalValue?: string} $exampleData */ + $exampleData = $example; + $mediaType->addExample($name, Example::fromArray($exampleData)); } } - foreach ($data['encoding'] ?? [] as $name => $encoding) { - if (isset($encoding['$ref'])) { - $mediaType->addEncoding($name, Reference::fromArray($encoding)); + /** @var array $encoding */ + $encoding = $data['encoding'] ?? []; + foreach ($encoding as $name => $encodingItem) { + if (isset($encodingItem['$ref'])) { + $mediaType->addEncoding($name, Reference::fromArray($encodingItem)); } else { - $mediaType->addEncoding($name, Encoding::fromArray($encoding)); + $mediaType->addEncoding($name, Encoding::fromArray($encodingItem)); } } diff --git a/src/Schema/OAuthFlow.php b/src/Schema/OAuthFlow.php index 4f7c0cf..fd53ef3 100644 --- a/src/Schema/OAuthFlow.php +++ b/src/Schema/OAuthFlow.php @@ -26,7 +26,7 @@ public function __construct(string $authorizationUrl, string $tokenUrl, string $ } /** - * @param mixed[] $data + * @param array{authorizationUrl: string, tokenUrl: string, refreshUrl: string, scopes: array} $data */ public static function fromArray(array $data): self { diff --git a/src/Schema/OpenApi.php b/src/Schema/OpenApi.php index 171bc18..4f4ce13 100644 --- a/src/Schema/OpenApi.php +++ b/src/Schema/OpenApi.php @@ -43,41 +43,69 @@ public function __construct(string $openapi, Info $info, ?Paths $paths = null) */ public static function fromArray(array $data): OpenApi { + /** @var array{title: string, version: string, summary?: string, description?: string, termsOfService?: string, contact?: array{name?: string, url?: string, email?: string}, license?: array{name: string, identifier?: string, url?: string}} $info */ + $info = $data['info']; + /** @var string $openapi */ + $openapi = $data['openapi']; $openApi = new OpenApi( - $data['openapi'], - Info::fromArray($data['info']), + $openapi, + Info::fromArray($info), ); - if (isset($data['jsonSchemaDialect'])) { - $openApi->jsonSchemaDialect = $data['jsonSchemaDialect']; - } + /** @var string|null $jsonSchemaDialect */ + $jsonSchemaDialect = $data['jsonSchemaDialect'] ?? null; + $openApi->jsonSchemaDialect = $jsonSchemaDialect; foreach ($data['servers'] ?? [] as $serverData) { - $openApi->addServer(Server::fromArray($serverData)); + if (!is_array($serverData)) { + continue; + } + + /** @var array{url: string, description?: string, variables?: array, description?: string}>} $server */ + $server = $serverData; + $openApi->addServer(Server::fromArray($server)); } if (isset($data['paths'])) { $openApi->paths = Paths::fromArray($data['paths']); } - foreach ($data['webhooks'] ?? [] as $webhookId => $webhookData) { + /** @var array $webhooks */ + $webhooks = $data['webhooks'] ?? []; + foreach ($webhooks as $webhookId => $webhookData) { $webhook = isset($webhookData['$ref']) ? Reference::fromArray($webhookData) : PathItem::fromArray($webhookData); - $openApi->webhooks[(string) $webhookId] = $webhook; + $openApi->webhooks[$webhookId] = $webhook; } if (isset($data['components'])) { - $openApi->setComponents(Components::fromArray($data['components'])); + /** @var mixed[] $components */ + $components = $data['components']; + $openApi->setComponents(Components::fromArray($components)); } foreach ($data['tags'] ?? [] as $tagData) { - $openApi->addTag(Tag::fromArray($tagData)); + if (!is_array($tagData)) { + continue; + } + + /** @var array{name: string, description?: string, externalDocs?: array{description?: string, url: string}} $tag */ + $tag = $tagData; + $openApi->addTag(Tag::fromArray($tag)); } if (isset($data['externalDocs'])) { - $openApi->externalDocs = ExternalDocumentation::fromArray($data['externalDocs']); + /** @var array{description?: string, url: string} $externalDocs */ + $externalDocs = $data['externalDocs']; + $openApi->externalDocs = ExternalDocumentation::fromArray($externalDocs); } - foreach ($data['security'] ?? [] as $security) { + foreach ($data['security'] ?? [] as $securityItem) { + if (!is_array($securityItem)) { + continue; + } + + /** @var array> $security */ + $security = $securityItem; $openApi->addSecurityRequirement(SecurityRequirement::fromArray($security)); } diff --git a/src/Schema/Operation.php b/src/Schema/Operation.php index 8fefd3f..daa7151 100644 --- a/src/Schema/Operation.php +++ b/src/Schema/Operation.php @@ -51,7 +51,9 @@ public static function fromArray(array $data): Operation $operation = new Operation(); if (isset($data['deprecated'])) { - $operation->setDeprecated($data['deprecated']); + /** @var bool $deprecated */ + $deprecated = $data['deprecated']; + $operation->setDeprecated($deprecated); } $operation->setOperationId($data['operationId'] ?? null); @@ -60,22 +62,30 @@ public static function fromArray(array $data): Operation $operation->setDescription($data['description'] ?? null); if (isset($data['externalDocs'])) { - $operation->setExternalDocs(ExternalDocumentation::fromArray($data['externalDocs'])); + /** @var array{description?: string, url: string} $externalDocs */ + $externalDocs = $data['externalDocs']; + $operation->setExternalDocs(ExternalDocumentation::fromArray($externalDocs)); } foreach ($data['parameters'] ?? [] as $parameterData) { + if (!is_array($parameterData)) { + continue; + } + if (isset($parameterData['$ref'])) { $operation->addParameter(Reference::fromArray($parameterData)); continue; } - $parameter = Parameter::fromArray($parameterData); + /** @var array{name: string, in: string, description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $param */ + $param = $parameterData; + $parameter = Parameter::fromArray($param); if ($operation->hasParameter($parameter)) { $operation->mergeParameter($parameter); } else { - $operation->addParameter(Parameter::fromArray($parameterData)); + $operation->addParameter($parameter); } } @@ -83,7 +93,9 @@ public static function fromArray(array $data): Operation if (isset($data['requestBody']['$ref'])) { $operation->setRequestBody(Reference::fromArray($data['requestBody'])); } else { - $operation->setRequestBody(RequestBody::fromArray($data['requestBody'])); + /** @var mixed[] $requestBody */ + $requestBody = $data['requestBody']; + $operation->setRequestBody(RequestBody::fromArray($requestBody)); } } @@ -96,11 +108,23 @@ public static function fromArray(array $data): Operation } foreach ($data['security'] ?? [] as $securityRequirementData) { - $operation->addSecurityRequirement(SecurityRequirement::fromArray($securityRequirementData)); + if (!is_array($securityRequirementData)) { + continue; + } + + /** @var array> $security */ + $security = $securityRequirementData; + $operation->addSecurityRequirement(SecurityRequirement::fromArray($security)); } foreach ($data['servers'] ?? [] as $server) { - $operation->addServer(Server::fromArray($server)); + if (!is_array($server)) { + continue; + } + + /** @var array{url: string, description?: string, variables?: array, description?: string}>} $serverData */ + $serverData = $server; + $operation->addServer(Server::fromArray($serverData)); } foreach ($data['callbacks'] ?? [] as $expression => $callback) { @@ -165,7 +189,9 @@ public function mergeParameter(Parameter $parameter): void $originalParameter = $this->parameters[$this->getParameterKey($parameter)]; $merged = Helpers::merge($parameter->toArray(), $originalParameter->toArray()); - $parameter = Parameter::fromArray($merged); + /** @var array{name: string, in: string, description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $mergedArray */ + $mergedArray = is_array($merged) ? $merged : []; + $parameter = Parameter::fromArray($mergedArray); $this->parameters[$this->getParameterKey($parameter)] = $parameter; } diff --git a/src/Schema/Parameter.php b/src/Schema/Parameter.php index cf4080b..aa10830 100644 --- a/src/Schema/Parameter.php +++ b/src/Schema/Parameter.php @@ -61,7 +61,7 @@ public function __construct(string $name, string $in) } /** - * @param mixed[] $data + * @param array{name: string, in: string, description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: mixed[]} $data */ public static function fromArray(array $data): Parameter { diff --git a/src/Schema/PathItem.php b/src/Schema/PathItem.php index 4287755..6d92e4a 100644 --- a/src/Schema/PathItem.php +++ b/src/Schema/PathItem.php @@ -53,21 +53,35 @@ public static function fromArray(array $pathItemData): PathItem continue; } - $pathItem->setOperation($allowedOperation, Operation::fromArray($pathItemData[$allowedOperation])); + /** @var mixed[] $operationData */ + $operationData = $pathItemData[$allowedOperation]; + $pathItem->setOperation($allowedOperation, Operation::fromArray($operationData)); } $pathItem->setSummary($pathItemData['summary'] ?? null); $pathItem->setDescription($pathItemData['description'] ?? null); foreach ($pathItemData['servers'] ?? [] as $server) { - $pathItem->addServer(Server::fromArray($server)); + if (!is_array($server)) { + continue; + } + + /** @var array{url: string, description?: string, variables?: array, description?: string}>} $serverData */ + $serverData = $server; + $pathItem->addServer(Server::fromArray($serverData)); } foreach ($pathItemData['parameters'] ?? [] as $parameter) { + if (!is_array($parameter)) { + continue; + } + if (isset($parameter['$ref'])) { $pathItem->addParameter(Reference::fromArray($parameter)); } else { - $pathItem->addParameter(Parameter::fromArray($parameter)); + /** @var array{name: string, in: string, description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $param */ + $param = $parameter; + $pathItem->addParameter(Parameter::fromArray($param)); } } diff --git a/src/Schema/Paths.php b/src/Schema/Paths.php index c59b845..b1ff1aa 100644 --- a/src/Schema/Paths.php +++ b/src/Schema/Paths.php @@ -11,17 +11,23 @@ class Paths private ?VendorExtensions $vendorExtensions = null; /** - * @param mixed[] $data + * @param array $data */ public static function fromArray(array $data): Paths { $paths = new Paths(); foreach ($data as $path => $pathItemData) { + if (!is_array($pathItemData)) { + continue; + } + if (isset($pathItemData['$ref'])) { $paths->setPathItem($path, Reference::fromArray($pathItemData)); } else { - $paths->setPathItem($path, PathItem::fromArray($pathItemData)); + /** @var mixed[] $pathItem */ + $pathItem = $pathItemData; + $paths->setPathItem($path, PathItem::fromArray($pathItem)); } } diff --git a/src/Schema/Reference.php b/src/Schema/Reference.php index 8f9e919..c1b6e8e 100644 --- a/src/Schema/Reference.php +++ b/src/Schema/Reference.php @@ -17,13 +17,19 @@ public function __construct(string $ref) } /** - * @param mixed[] $data + * @param array $data */ public static function fromArray(array $data): Reference { - $reference = new Reference($data['$ref']); - $reference->setSummary($data['summary'] ?? null); - $reference->setDescription($data['description'] ?? null); + /** @var string $ref */ + $ref = $data['$ref']; + $reference = new Reference($ref); + /** @var string|null $summary */ + $summary = $data['summary'] ?? null; + $reference->setSummary($summary); + /** @var string|null $description */ + $description = $data['description'] ?? null; + $reference->setDescription($description); return $reference; } diff --git a/src/Schema/RequestBody.php b/src/Schema/RequestBody.php index f6dcfd7..3af7996 100644 --- a/src/Schema/RequestBody.php +++ b/src/Schema/RequestBody.php @@ -20,10 +20,14 @@ class RequestBody public static function fromArray(array $data): RequestBody { $requestBody = new RequestBody(); - $requestBody->setRequired($data['required'] ?? false); + /** @var bool $required */ + $required = $data['required'] ?? false; + $requestBody->setRequired($required); $requestBody->setDescription($data['description'] ?? null); - foreach ($data['content'] ?? [] as $key => $mediaType) { + /** @var array $content */ + $content = $data['content'] ?? []; + foreach ($content as $key => $mediaType) { $requestBody->addMediaType($key, MediaType::fromArray($mediaType)); } diff --git a/src/Schema/Response.php b/src/Schema/Response.php index 3e29841..830817d 100644 --- a/src/Schema/Response.php +++ b/src/Schema/Response.php @@ -28,29 +28,44 @@ public function __construct(string $description) */ public static function fromArray(array $data): Response { - $response = new Response($data['description']); + /** @var string $description */ + $description = $data['description']; + $response = new Response($description); foreach ($data['headers'] ?? [] as $key => $headerData) { + if (!is_array($headerData)) { + continue; + } + if (isset($headerData['$ref'])) { $response->setHeader($key, Reference::fromArray($headerData)); } else { - $response->setHeader($key, Header::fromArray($headerData)); + /** @var array{description?: string, required?: bool, deprecated?: bool, allowEmptyValue?: bool, style?: string, explode?: bool, allowReserved?: bool, schema?: mixed[], example?: mixed, examples?: array} $header */ + $header = $headerData; + $response->setHeader($key, Header::fromArray($header)); } } if (isset($data['content'])) { $response->content = []; - } - - foreach ($data['content'] ?? [] as $key => $contentData) { - $response->setContent($key, MediaType::fromArray($contentData)); + /** @var array $content */ + $content = $data['content']; + foreach ($content as $key => $contentData) { + $response->setContent($key, MediaType::fromArray($contentData)); + } } foreach ($data['links'] ?? [] as $key => $linkData) { + if (!is_array($linkData)) { + continue; + } + if (isset($linkData['$ref'])) { $response->setLink($key, Reference::fromArray($linkData)); } else { - $response->setLink($key, Link::fromArray($linkData)); + /** @var array{operationRef?: string, operationId?: string, parameters?: mixed[], requestBody?: mixed, description?: string, server?: array{url: string, description?: string, variables?: array, description?: string}>}} $link */ + $link = $linkData; + $response->setLink($key, Link::fromArray($link)); } } diff --git a/src/Schema/Responses.php b/src/Schema/Responses.php index 4697fba..f7ae245 100644 --- a/src/Schema/Responses.php +++ b/src/Schema/Responses.php @@ -11,17 +11,23 @@ class Responses private ?VendorExtensions $vendorExtensions = null; /** - * @param mixed[] $data + * @param array $data */ public static function fromArray(array $data): Responses { $responses = new Responses(); foreach ($data as $key => $responseData) { + if (!is_array($responseData)) { + continue; + } + if (isset($responseData['$ref'])) { - $responses->setResponse((string) $key, Reference::fromArray($responseData)); + $responses->setResponse($key, Reference::fromArray($responseData)); } else { - $responses->setResponse((string) $key, Response::fromArray($responseData)); + /** @var mixed[] $response */ + $response = $responseData; + $responses->setResponse($key, Response::fromArray($response)); } } diff --git a/src/Schema/SecurityScheme.php b/src/Schema/SecurityScheme.php index 9d82d35..7bb5f6e 100644 --- a/src/Schema/SecurityScheme.php +++ b/src/Schema/SecurityScheme.php @@ -65,14 +65,19 @@ public function __construct(string $type) */ public static function fromArray(array $data): SecurityScheme { + /** @var string $type */ $type = $data['type']; $securityScheme = new SecurityScheme($type); - $securityScheme->setName($data['name'] ?? null); + /** @var string|null $name */ + $name = $data['name'] ?? null; + $securityScheme->setName($name); $securityScheme->setDescription($data['description'] ?? null); $securityScheme->setIn($data['in'] ?? null); $securityScheme->setScheme($data['scheme'] ?? null); $securityScheme->setBearerFormat($data['bearerFormat'] ?? null); - $securityScheme->setFlows(array_map(static fn (array $flow): OAuthFlow => OAuthFlow::fromArray($flow), $data['flows'] ?? [])); + /** @var array}> $flows */ + $flows = $data['flows'] ?? []; + $securityScheme->setFlows(array_map(static fn (array $flow): OAuthFlow => OAuthFlow::fromArray($flow), $flows)); $securityScheme->setOpenIdConnectUrl($data['openIdConnectUrl'] ?? null); return $securityScheme; diff --git a/src/Schema/Server.php b/src/Schema/Server.php index b38ed2e..2613c56 100644 --- a/src/Schema/Server.php +++ b/src/Schema/Server.php @@ -20,7 +20,7 @@ public function __construct(string $url) } /** - * @param mixed[] $data + * @param array{url: string, description?: string, variables?: array} $data */ public static function fromArray(array $data): Server { diff --git a/src/Schema/ServerVariable.php b/src/Schema/ServerVariable.php index aa74d7b..e045a19 100644 --- a/src/Schema/ServerVariable.php +++ b/src/Schema/ServerVariable.php @@ -20,7 +20,7 @@ public function __construct(string $default) } /** - * @param mixed[] $data + * @param array{default: string, enum?: string[], description?: string} $data */ public static function fromArray(array $data): ServerVariable { diff --git a/src/Schema/Tag.php b/src/Schema/Tag.php index 7afe40f..7be2c21 100644 --- a/src/Schema/Tag.php +++ b/src/Schema/Tag.php @@ -19,7 +19,7 @@ public function __construct(string $name) } /** - * @param mixed[] $data + * @param array{name: string, description?: string, externalDocs?: array{url: string, description?: string}} $data */ public static function fromArray(array $data): Tag { diff --git a/tests/Cases/Schema/ContactTest.php b/tests/Cases/Schema/ContactTest.php deleted file mode 100644 index c58f579..0000000 --- a/tests/Cases/Schema/ContactTest.php +++ /dev/null @@ -1,53 +0,0 @@ -setName('API Support'); - $contact->setEmail('support@example.com'); - $contact->setUrl('http://www.example.com/support'); - - Assert::same('API Support', $contact->getName()); - Assert::same('http://www.example.com/support', $contact->getUrl()); - Assert::same('support@example.com', $contact->getEmail()); - - $realData = $contact->toArray(); - $expectedData = [ - 'name' => 'API Support', - 'url' => 'http://www.example.com/support', - 'email' => 'support@example.com', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Contact::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $contact = new Contact(); - - Assert::null($contact->getName()); - Assert::null($contact->getUrl()); - Assert::null($contact->getEmail()); - - $realData = $contact->toArray(); - $expectedData = []; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Contact::fromArray($realData)->toArray()); - } - -} - -(new ContactTest())->run(); diff --git a/tests/Cases/Schema/ContactTest.phpt b/tests/Cases/Schema/ContactTest.phpt new file mode 100644 index 0000000..5f6f114 --- /dev/null +++ b/tests/Cases/Schema/ContactTest.phpt @@ -0,0 +1,46 @@ +setName('API Support'); + $contact->setEmail('support@example.com'); + $contact->setUrl('http://www.example.com/support'); + + Assert::same('API Support', $contact->getName()); + Assert::same('http://www.example.com/support', $contact->getUrl()); + Assert::same('support@example.com', $contact->getEmail()); + + $realData = $contact->toArray(); + $expectedData = [ + 'name' => 'API Support', + 'url' => 'http://www.example.com/support', + 'email' => 'support@example.com', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Contact::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $contact = new Contact(); + + Assert::null($contact->getName()); + Assert::null($contact->getUrl()); + Assert::null($contact->getEmail()); + + $realData = $contact->toArray(); + $expectedData = []; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Contact::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ExampleTest.php b/tests/Cases/Schema/ExampleTest.php deleted file mode 100644 index 916a934..0000000 --- a/tests/Cases/Schema/ExampleTest.php +++ /dev/null @@ -1,45 +0,0 @@ -setSummary($summary); - $example->setDescription($description); - $example->setValue($value); - $example->setExternalValue($externalValue); - - Assert::same($summary, $example->getSummary()); - Assert::same($description, $example->getDescription()); - Assert::same($value, $example->getValue()); - Assert::same($externalValue, $example->getExternalValue()); - - $realData = $example->toArray(); - $expectedData = [ - 'summary' => $summary, - 'description' => $description, - 'value' => $value, - 'externalValue' => $externalValue, - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Example::fromArray($realData)->toArray()); - } - -} - -(new ExampleTest())->run(); diff --git a/tests/Cases/Schema/ExampleTest.phpt b/tests/Cases/Schema/ExampleTest.phpt new file mode 100644 index 0000000..99f08a4 --- /dev/null +++ b/tests/Cases/Schema/ExampleTest.phpt @@ -0,0 +1,38 @@ +setSummary($summary); + $example->setDescription($description); + $example->setValue($value); + $example->setExternalValue($externalValue); + + Assert::same($summary, $example->getSummary()); + Assert::same($description, $example->getDescription()); + Assert::same($value, $example->getValue()); + Assert::same($externalValue, $example->getExternalValue()); + + $realData = $example->toArray(); + $expectedData = [ + 'summary' => $summary, + 'description' => $description, + 'value' => $value, + 'externalValue' => $externalValue, + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Example::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ExamplesTest.php b/tests/Cases/Schema/ExamplesTest.php deleted file mode 100644 index 245a453..0000000 --- a/tests/Cases/Schema/ExamplesTest.php +++ /dev/null @@ -1,131 +0,0 @@ -toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testCallbackExample(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/callback-example.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testLinkExample(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/link-example.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testGitLab(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/gitlab.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testPetstore(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/petstore.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testPetstoreExpanded(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/petstore-expanded.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testUspto(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/uspto.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testWebhooks(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/webhook-example.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testNonOauthScopes(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/non-oauth-scopes.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - public function testRedoclyMuseum(): void - { - $rawData = Yaml::parseFile(__DIR__ . '/examples/redocly-museum.yaml'); - $openApi = OpenApi::fromArray($rawData); - $openApiData = $openApi->toArray(); - self::assertSameDataStructure($rawData, $openApiData); - } - - /** - * @param mixed[] $expected - * @param mixed[] $actual - */ - private static function assertSameDataStructure(array $expected, array $actual): void - { - $expected = self::recursiveSort($expected); - $actual = self::recursiveSort($actual); - Assert::same($expected, $actual); - } - - /** - * @param mixed[] $data - * @return mixed[] - */ - private static function recursiveSort(array $data): array - { - foreach ($data as $key => $value) { - if (!is_array($value)) { - continue; - } - - $data[$key] = self::recursiveSort($value); - } - - unset($value); - ksort($data); - - return $data; - } - -} - -(new ExamplesTest())->run(); diff --git a/tests/Cases/Schema/ExamplesTest.phpt b/tests/Cases/Schema/ExamplesTest.phpt new file mode 100644 index 0000000..86bb4b7 --- /dev/null +++ b/tests/Cases/Schema/ExamplesTest.phpt @@ -0,0 +1,121 @@ + $value) { + if (!is_array($value)) { + continue; + } + + $data[$key] = recursiveSort($value); + } + + unset($value); + ksort($data); + + return $data; +} + +/** + * @param mixed[] $expected + * @param mixed[] $actual + */ +function assertSameDataStructure(array $expected, array $actual): void +{ + $expected = recursiveSort($expected); + $actual = recursiveSort($actual); + Assert::same($expected, $actual); +} + +// Test api-with-examples.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/api-with-examples.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test callback-example.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/callback-example.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test link-example.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/link-example.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test gitlab.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/gitlab.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test petstore.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/petstore.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test petstore-expanded.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/petstore-expanded.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test uspto.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/uspto.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test webhook-example.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/webhook-example.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test non-oauth-scopes.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/non-oauth-scopes.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); + +// Test redocly-museum.yaml +Toolkit::test(static function (): void { + $rawData = Yaml::parseFile(__DIR__ . '/examples/redocly-museum.yaml'); + $openApi = OpenApi::fromArray($rawData); + $openApiData = $openApi->toArray(); + assertSameDataStructure($rawData, $openApiData); +}); diff --git a/tests/Cases/Schema/ExternalDocumentationTest.php b/tests/Cases/Schema/ExternalDocumentationTest.php deleted file mode 100644 index d10b714..0000000 --- a/tests/Cases/Schema/ExternalDocumentationTest.php +++ /dev/null @@ -1,48 +0,0 @@ -setDescription('Find more info here'); - - Assert::same('https://example.com', $documentation->getUrl()); - Assert::same('Find more info here', $documentation->getDescription()); - - $realData = $documentation->toArray(); - $expectedData = [ - 'url' => 'https://example.com', - 'description' => 'Find more info here', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, ExternalDocumentation::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $documentation = new ExternalDocumentation('https://example.com'); - - Assert::same('https://example.com', $documentation->getUrl()); - Assert::null($documentation->getDescription()); - - $realData = $documentation->toArray(); - $expectedData = ['url' => 'https://example.com']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, ExternalDocumentation::fromArray($realData)->toArray()); - } - -} - -(new ExternalDocumentationTest())->run(); diff --git a/tests/Cases/Schema/ExternalDocumentationTest.phpt b/tests/Cases/Schema/ExternalDocumentationTest.phpt new file mode 100644 index 0000000..dc6e44b --- /dev/null +++ b/tests/Cases/Schema/ExternalDocumentationTest.phpt @@ -0,0 +1,41 @@ +setDescription('Find more info here'); + + Assert::same('https://example.com', $documentation->getUrl()); + Assert::same('Find more info here', $documentation->getDescription()); + + $realData = $documentation->toArray(); + $expectedData = [ + 'url' => 'https://example.com', + 'description' => 'Find more info here', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, ExternalDocumentation::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $documentation = new ExternalDocumentation('https://example.com'); + + Assert::same('https://example.com', $documentation->getUrl()); + Assert::null($documentation->getDescription()); + + $realData = $documentation->toArray(); + $expectedData = ['url' => 'https://example.com']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, ExternalDocumentation::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/InfoTest.php b/tests/Cases/Schema/InfoTest.php deleted file mode 100644 index 7a50b95..0000000 --- a/tests/Cases/Schema/InfoTest.php +++ /dev/null @@ -1,72 +0,0 @@ -setDescription('This is a sample server for a pet store.'); - $info->setTermsOfService('http://example.com/terms/'); - - $contact = new Contact(); - $info->setContact($contact); - - $license = new License('Apache 2.0'); - $info->setLicense($license); - - Assert::same('Sample Pet Store App', $info->getTitle()); - Assert::same('This is a sample server for a pet store.', $info->getDescription()); - Assert::same('http://example.com/terms/', $info->getTermsOfService()); - Assert::same($contact, $info->getContact()); - Assert::same($license, $info->getLicense()); - Assert::same('1.0.1', $info->getVersion()); - - $realData = $info->toArray(); - $expectedData = [ - 'title' => 'Sample Pet Store App', - 'description' => 'This is a sample server for a pet store.', - 'termsOfService' => 'http://example.com/terms/', - 'contact' => [], - 'license' => ['name' => 'Apache 2.0'], - 'version' => '1.0.1', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Info::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $info = new Info('Sample Pet Store App', '1.0.1'); - - Assert::same('Sample Pet Store App', $info->getTitle()); - Assert::same(null, $info->getDescription()); - Assert::same(null, $info->getTermsOfService()); - Assert::same(null, $info->getContact()); - Assert::same(null, $info->getLicense()); - Assert::same('1.0.1', $info->getVersion()); - - $realData = $info->toArray(); - $expectedData = [ - 'title' => 'Sample Pet Store App', - 'version' => '1.0.1', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Info::fromArray($realData)->toArray()); - } - -} - -(new InfoTest())->run(); diff --git a/tests/Cases/Schema/InfoTest.phpt b/tests/Cases/Schema/InfoTest.phpt new file mode 100644 index 0000000..8f871cc --- /dev/null +++ b/tests/Cases/Schema/InfoTest.phpt @@ -0,0 +1,65 @@ +setDescription('This is a sample server for a pet store.'); + $info->setTermsOfService('http://example.com/terms/'); + + $contact = new Contact(); + $info->setContact($contact); + + $license = new License('Apache 2.0'); + $info->setLicense($license); + + Assert::same('Sample Pet Store App', $info->getTitle()); + Assert::same('This is a sample server for a pet store.', $info->getDescription()); + Assert::same('http://example.com/terms/', $info->getTermsOfService()); + Assert::same($contact, $info->getContact()); + Assert::same($license, $info->getLicense()); + Assert::same('1.0.1', $info->getVersion()); + + $realData = $info->toArray(); + $expectedData = [ + 'title' => 'Sample Pet Store App', + 'description' => 'This is a sample server for a pet store.', + 'termsOfService' => 'http://example.com/terms/', + 'contact' => [], + 'license' => ['name' => 'Apache 2.0'], + 'version' => '1.0.1', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Info::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $info = new Info('Sample Pet Store App', '1.0.1'); + + Assert::same('Sample Pet Store App', $info->getTitle()); + Assert::same(null, $info->getDescription()); + Assert::same(null, $info->getTermsOfService()); + Assert::same(null, $info->getContact()); + Assert::same(null, $info->getLicense()); + Assert::same('1.0.1', $info->getVersion()); + + $realData = $info->toArray(); + $expectedData = [ + 'title' => 'Sample Pet Store App', + 'version' => '1.0.1', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Info::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/LicenseTest.php b/tests/Cases/Schema/LicenseTest.php deleted file mode 100644 index 904a11e..0000000 --- a/tests/Cases/Schema/LicenseTest.php +++ /dev/null @@ -1,51 +0,0 @@ -setIdentifier('Apache-2.0'); - $license->setUrl('https://www.apache.org/licenses/LICENSE-2.0.html'); - - Assert::same('Apache 2.0', $license->getName()); - Assert::same('Apache-2.0', $license->getIdentifier()); - Assert::same('https://www.apache.org/licenses/LICENSE-2.0.html', $license->getUrl()); - - $realData = $license->toArray(); - $expectedData = [ - 'name' => 'Apache 2.0', - 'identifier' => 'Apache-2.0', - 'url' => 'https://www.apache.org/licenses/LICENSE-2.0.html', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, License::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $license = new License('Apache 2.0'); - - Assert::same('Apache 2.0', $license->getName()); - Assert::null($license->getUrl()); - - $realData = $license->toArray(); - $expectedData = ['name' => 'Apache 2.0']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, License::fromArray($realData)->toArray()); - } - -} - -(new LicenseTest())->run(); diff --git a/tests/Cases/Schema/LicenseTest.phpt b/tests/Cases/Schema/LicenseTest.phpt new file mode 100644 index 0000000..acda633 --- /dev/null +++ b/tests/Cases/Schema/LicenseTest.phpt @@ -0,0 +1,44 @@ +setIdentifier('Apache-2.0'); + $license->setUrl('https://www.apache.org/licenses/LICENSE-2.0.html'); + + Assert::same('Apache 2.0', $license->getName()); + Assert::same('Apache-2.0', $license->getIdentifier()); + Assert::same('https://www.apache.org/licenses/LICENSE-2.0.html', $license->getUrl()); + + $realData = $license->toArray(); + $expectedData = [ + 'name' => 'Apache 2.0', + 'identifier' => 'Apache-2.0', + 'url' => 'https://www.apache.org/licenses/LICENSE-2.0.html', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, License::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $license = new License('Apache 2.0'); + + Assert::same('Apache 2.0', $license->getName()); + Assert::null($license->getUrl()); + + $realData = $license->toArray(); + $expectedData = ['name' => 'Apache 2.0']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, License::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/MediaTypeTest.php b/tests/Cases/Schema/MediaTypeTest.php deleted file mode 100644 index 0d5adb8..0000000 --- a/tests/Cases/Schema/MediaTypeTest.php +++ /dev/null @@ -1,62 +0,0 @@ -setExample('whatever'); - - $schema = new Schema([]); - $mediaType->setSchema($schema); - - $realData = $mediaType->toArray(); - $expectedData = ['schema' => [], 'example' => 'whatever']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $mediaType = new MediaType(); - - Assert::null($mediaType->getExample()); - Assert::null($mediaType->getSchema()); - - $realData = $mediaType->toArray(); - $expectedData = []; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); - } - - public function testSchemaReference(): void - { - $mediaType = new MediaType(); - $schema = new Reference('ref'); - $mediaType->setSchema($schema); - - Assert::same($schema, $mediaType->getSchema()); - - $realData = $mediaType->toArray(); - $expectedData = ['schema' => ['$ref' => 'ref']]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); - } - -} - -(new MediaTypeTest())->run(); diff --git a/tests/Cases/Schema/MediaTypeTest.phpt b/tests/Cases/Schema/MediaTypeTest.phpt new file mode 100644 index 0000000..9df8c7c --- /dev/null +++ b/tests/Cases/Schema/MediaTypeTest.phpt @@ -0,0 +1,55 @@ +setExample('whatever'); + + $schema = new Schema([]); + $mediaType->setSchema($schema); + + $realData = $mediaType->toArray(); + $expectedData = ['schema' => [], 'example' => 'whatever']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $mediaType = new MediaType(); + + Assert::null($mediaType->getExample()); + Assert::null($mediaType->getSchema()); + + $realData = $mediaType->toArray(); + $expectedData = []; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); +}); + +// Test schema reference +Toolkit::test(static function (): void { + $mediaType = new MediaType(); + $schema = new Reference('ref'); + $mediaType->setSchema($schema); + + Assert::same($schema, $mediaType->getSchema()); + + $realData = $mediaType->toArray(); + $expectedData = ['schema' => ['$ref' => 'ref']]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, MediaType::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/OAuthFlowTest.php b/tests/Cases/Schema/OAuthFlowTest.php deleted file mode 100644 index c220e6f..0000000 --- a/tests/Cases/Schema/OAuthFlowTest.php +++ /dev/null @@ -1,41 +0,0 @@ - 'Read access', 'write' => 'Write access']; - $flow = new OAuthFlow($authorizationUrl, $tokenUrl, $refreshUrl, $scopes); - - Assert::same($authorizationUrl, $flow->getAuthorizationUrl()); - Assert::same($tokenUrl, $flow->getTokenUrl()); - Assert::same($refreshUrl, $flow->getRefreshUrl()); - Assert::same($scopes, $flow->getScopes()); - - $realData = $flow->toArray(); - $expectedData = [ - 'authorizationUrl' => $authorizationUrl, - 'tokenUrl' => $tokenUrl, - 'refreshUrl' => $refreshUrl, - 'scopes' => $scopes, - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, OAuthFlow::fromArray($realData)->toArray()); - } - -} - -(new OAuthFlowTest())->run(); diff --git a/tests/Cases/Schema/OAuthFlowTest.phpt b/tests/Cases/Schema/OAuthFlowTest.phpt new file mode 100644 index 0000000..1ee4b40 --- /dev/null +++ b/tests/Cases/Schema/OAuthFlowTest.phpt @@ -0,0 +1,34 @@ + 'Read access', 'write' => 'Write access']; + $flow = new OAuthFlow($authorizationUrl, $tokenUrl, $refreshUrl, $scopes); + + Assert::same($authorizationUrl, $flow->getAuthorizationUrl()); + Assert::same($tokenUrl, $flow->getTokenUrl()); + Assert::same($refreshUrl, $flow->getRefreshUrl()); + Assert::same($scopes, $flow->getScopes()); + + $realData = $flow->toArray(); + $expectedData = [ + 'authorizationUrl' => $authorizationUrl, + 'tokenUrl' => $tokenUrl, + 'refreshUrl' => $refreshUrl, + 'scopes' => $scopes, + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, OAuthFlow::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/OperationTest.php b/tests/Cases/Schema/OperationTest.php deleted file mode 100644 index cf64a6d..0000000 --- a/tests/Cases/Schema/OperationTest.php +++ /dev/null @@ -1,158 +0,0 @@ -setSummary('summary'); - $operation->setDescription('description'); - $operation->setOperationId('id'); - - $requestBody = new RequestBody(); - $operation->setRequestBody($requestBody); - - $externalDocs = new ExternalDocumentation('https://external-docs.example.com'); - $operation->setExternalDocs($externalDocs); - - $parameters = []; - $parameters['path-p1'] = $p1 = new Parameter('p1', Parameter::IN_PATH); - $operation->addParameter($p1); - $parameters['cookie-p2'] = $p2 = new Parameter('p2', Parameter::IN_COOKIE); - $operation->addParameter($p2); - $parameters[] = $p3 = new Reference('r1'); - $operation->addParameter($p3); - - $callbacks = []; - $callbacks['onFirst'] = $callback1 = new Callback([]); - $operation->addCallback('onFirst', $callback1); - $callbacks['onSecond'] = $callback2 = new Callback([]); - $operation->addCallback('onSecond', $callback2); - $callbacks['onThird'] = $callback3 = new Reference('ref'); - $operation->addCallback('onThird', $callback3); - - $securityRequirements = []; - $securityRequirements[] = $sr1 = new SecurityRequirement([]); - $operation->addSecurityRequirement($sr1); - - $servers = []; - $servers[] = $server1 = new Server('https://server-one.example.com'); - $operation->addServer($server1); - $servers[] = $server2 = new Server('https://server-two.example.com'); - $operation->addServer($server2); - - $operation->setTags(['foo', 'bar', 'baz']); - $operation->setDeprecated(true); - - Assert::same('summary', $operation->getSummary()); - Assert::same('description', $operation->getDescription()); - Assert::same('id', $operation->getOperationId()); - Assert::same($requestBody, $operation->getRequestBody()); - Assert::same($externalDocs, $operation->getExternalDocs()); - - Assert::same($parameters, $operation->getParameters()); - Assert::same($callbacks, $operation->getCallbacks()); - Assert::same($securityRequirements, $operation->getSecurity()); - Assert::same($servers, $operation->getServers()); - Assert::same(['foo', 'bar', 'baz'], $operation->getTags()); - Assert::same($responses, $operation->getResponses()); - - Assert::true($operation->isDeprecated()); - - $realData = $operation->toArray(); - $expectedData = [ - 'deprecated' => true, - 'tags' => ['foo', 'bar', 'baz'], - 'summary' => 'summary', - 'description' => 'description', - 'externalDocs' => ['url' => 'https://external-docs.example.com'], - 'operationId' => 'id', - 'parameters' => [ - ['name' => 'p1', 'in' => 'path'], - ['name' => 'p2', 'in' => 'cookie'], - ['$ref' => 'r1'], - ], - 'requestBody' => ['content' => []], - 'security' => [[]], - 'responses' => [], - 'servers' => [ - ['url' => 'https://server-one.example.com'], - ['url' => 'https://server-two.example.com'], - ], - 'callbacks' => [ - 'onFirst' => [], - 'onSecond' => [], - 'onThird' => ['$ref' => 'ref'], - ], - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Operation::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $responses = new Responses(); - $operation = new Operation($responses); - - Assert::null($operation->getSummary()); - Assert::null($operation->getDescription()); - Assert::null($operation->getOperationId()); - Assert::null($operation->getRequestBody()); - Assert::null($operation->getExternalDocs()); - Assert::null($operation->getSecurity()); - - Assert::same([], $operation->getParameters()); - Assert::same([], $operation->getCallbacks()); - Assert::same([], $operation->getServers()); - Assert::same([], $operation->getTags()); - Assert::same($responses, $operation->getResponses()); - - Assert::false($operation->isDeprecated()); - - $realData = $operation->toArray(); - $expectedData = ['responses' => []]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Operation::fromArray($realData)->toArray()); - } - - public function testRequestBodyReference(): void - { - $responses = new Responses(); - $operation = new Operation($responses); - - $requestBody = new Reference('ref'); - $operation->setRequestBody($requestBody); - - Assert::same($requestBody, $operation->getRequestBody()); - - $realData = $operation->toArray(); - $expectedData = ['requestBody' => ['$ref' => 'ref'], 'responses' => []]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Operation::fromArray($realData)->toArray()); - } - -} - -(new OperationTest())->run(); diff --git a/tests/Cases/Schema/OperationTest.phpt b/tests/Cases/Schema/OperationTest.phpt new file mode 100644 index 0000000..49b72b1 --- /dev/null +++ b/tests/Cases/Schema/OperationTest.phpt @@ -0,0 +1,151 @@ +setSummary('summary'); + $operation->setDescription('description'); + $operation->setOperationId('id'); + + $requestBody = new RequestBody(); + $operation->setRequestBody($requestBody); + + $externalDocs = new ExternalDocumentation('https://external-docs.example.com'); + $operation->setExternalDocs($externalDocs); + + $parameters = []; + $parameters['path-p1'] = $p1 = new Parameter('p1', Parameter::IN_PATH); + $operation->addParameter($p1); + $parameters['cookie-p2'] = $p2 = new Parameter('p2', Parameter::IN_COOKIE); + $operation->addParameter($p2); + $parameters[] = $p3 = new Reference('r1'); + $operation->addParameter($p3); + + $callbacks = []; + $callbacks['onFirst'] = $callback1 = new Callback([]); + $operation->addCallback('onFirst', $callback1); + $callbacks['onSecond'] = $callback2 = new Callback([]); + $operation->addCallback('onSecond', $callback2); + $callbacks['onThird'] = $callback3 = new Reference('ref'); + $operation->addCallback('onThird', $callback3); + + $securityRequirements = []; + $securityRequirements[] = $sr1 = new SecurityRequirement([]); + $operation->addSecurityRequirement($sr1); + + $servers = []; + $servers[] = $server1 = new Server('https://server-one.example.com'); + $operation->addServer($server1); + $servers[] = $server2 = new Server('https://server-two.example.com'); + $operation->addServer($server2); + + $operation->setTags(['foo', 'bar', 'baz']); + $operation->setDeprecated(true); + + Assert::same('summary', $operation->getSummary()); + Assert::same('description', $operation->getDescription()); + Assert::same('id', $operation->getOperationId()); + Assert::same($requestBody, $operation->getRequestBody()); + Assert::same($externalDocs, $operation->getExternalDocs()); + + Assert::same($parameters, $operation->getParameters()); + Assert::same($callbacks, $operation->getCallbacks()); + Assert::same($securityRequirements, $operation->getSecurity()); + Assert::same($servers, $operation->getServers()); + Assert::same(['foo', 'bar', 'baz'], $operation->getTags()); + Assert::same($responses, $operation->getResponses()); + + Assert::true($operation->isDeprecated()); + + $realData = $operation->toArray(); + $expectedData = [ + 'deprecated' => true, + 'tags' => ['foo', 'bar', 'baz'], + 'summary' => 'summary', + 'description' => 'description', + 'externalDocs' => ['url' => 'https://external-docs.example.com'], + 'operationId' => 'id', + 'parameters' => [ + ['name' => 'p1', 'in' => 'path'], + ['name' => 'p2', 'in' => 'cookie'], + ['$ref' => 'r1'], + ], + 'requestBody' => ['content' => []], + 'security' => [[]], + 'responses' => [], + 'servers' => [ + ['url' => 'https://server-one.example.com'], + ['url' => 'https://server-two.example.com'], + ], + 'callbacks' => [ + 'onFirst' => [], + 'onSecond' => [], + 'onThird' => ['$ref' => 'ref'], + ], + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Operation::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $responses = new Responses(); + $operation = new Operation($responses); + + Assert::null($operation->getSummary()); + Assert::null($operation->getDescription()); + Assert::null($operation->getOperationId()); + Assert::null($operation->getRequestBody()); + Assert::null($operation->getExternalDocs()); + Assert::null($operation->getSecurity()); + + Assert::same([], $operation->getParameters()); + Assert::same([], $operation->getCallbacks()); + Assert::same([], $operation->getServers()); + Assert::same([], $operation->getTags()); + Assert::same($responses, $operation->getResponses()); + + Assert::false($operation->isDeprecated()); + + $realData = $operation->toArray(); + $expectedData = ['responses' => []]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Operation::fromArray($realData)->toArray()); +}); + +// Test request body reference +Toolkit::test(static function (): void { + $responses = new Responses(); + $operation = new Operation($responses); + + $requestBody = new Reference('ref'); + $operation->setRequestBody($requestBody); + + Assert::same($requestBody, $operation->getRequestBody()); + + $realData = $operation->toArray(); + $expectedData = ['requestBody' => ['$ref' => 'ref'], 'responses' => []]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Operation::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ParameterTest.php b/tests/Cases/Schema/ParameterTest.php deleted file mode 100644 index 36ff9e3..0000000 --- a/tests/Cases/Schema/ParameterTest.php +++ /dev/null @@ -1,108 +0,0 @@ -setDescription('description'); - - $parameter->setRequired(true); - $parameter->setDeprecated(true); - $parameter->setAllowEmptyValue(true); - - $parameter->setStyle('whatever'); - $parameter->setExample('whatever'); - - $schema = new Schema([]); - $parameter->setSchema($schema); - - Assert::same('p1', $parameter->getName()); - Assert::same(Parameter::IN_COOKIE, $parameter->getIn()); - Assert::same('description', $parameter->getDescription()); - - Assert::true($parameter->isRequired()); - Assert::true($parameter->isDeprecated()); - Assert::true($parameter->isAllowEmptyValue()); - - Assert::same('whatever', $parameter->getStyle()); - Assert::same('whatever', $parameter->getExample()); - Assert::same($schema, $parameter->getSchema()); - - $realData = $parameter->toArray(); - $expectedData = [ - 'name' => 'p1', - 'in' => 'cookie', - 'description' => 'description', - 'required' => true, - 'deprecated' => true, - 'allowEmptyValue' => true, - 'style' => 'whatever', - 'schema' => [], - 'example' => 'whatever', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $parameter = new Parameter('p1', Parameter::IN_PATH); - - Assert::same('p1', $parameter->getName()); - Assert::same(Parameter::IN_PATH, $parameter->getIn()); - Assert::null($parameter->getDescription()); - - Assert::null($parameter->isRequired()); - Assert::null($parameter->isDeprecated()); - Assert::null($parameter->isAllowEmptyValue()); - - Assert::null($parameter->getStyle()); - Assert::null($parameter->getExample()); - Assert::null($parameter->getSchema()); - - $realData = $parameter->toArray(); - $expectedData = ['name' => 'p1', 'in' => 'path']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); - } - - public function testInvalidIn(): void - { - Assert::exception(static function (): void { - new Parameter('foo', 'invalid'); - }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "in" given. It must be one of "cookie, header, path, query".'); - } - - public function testSchemaReference(): void - { - $parameter = new Parameter('p1', Parameter::IN_PATH); - $schema = new Reference('ref'); - $parameter->setSchema($schema); - - Assert::same($schema, $parameter->getSchema()); - - $realData = $parameter->toArray(); - $expectedData = ['name' => 'p1', 'in' => 'path', 'schema' => ['$ref' => 'ref']]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); - } - -} - -(new ParameterTest())->run(); diff --git a/tests/Cases/Schema/ParameterTest.phpt b/tests/Cases/Schema/ParameterTest.phpt new file mode 100644 index 0000000..1eb3d34 --- /dev/null +++ b/tests/Cases/Schema/ParameterTest.phpt @@ -0,0 +1,101 @@ +setDescription('description'); + + $parameter->setRequired(true); + $parameter->setDeprecated(true); + $parameter->setAllowEmptyValue(true); + + $parameter->setStyle('whatever'); + $parameter->setExample('whatever'); + + $schema = new Schema([]); + $parameter->setSchema($schema); + + Assert::same('p1', $parameter->getName()); + Assert::same(Parameter::IN_COOKIE, $parameter->getIn()); + Assert::same('description', $parameter->getDescription()); + + Assert::true($parameter->isRequired()); + Assert::true($parameter->isDeprecated()); + Assert::true($parameter->isAllowEmptyValue()); + + Assert::same('whatever', $parameter->getStyle()); + Assert::same('whatever', $parameter->getExample()); + Assert::same($schema, $parameter->getSchema()); + + $realData = $parameter->toArray(); + $expectedData = [ + 'name' => 'p1', + 'in' => 'cookie', + 'description' => 'description', + 'required' => true, + 'deprecated' => true, + 'allowEmptyValue' => true, + 'style' => 'whatever', + 'schema' => [], + 'example' => 'whatever', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $parameter = new Parameter('p1', Parameter::IN_PATH); + + Assert::same('p1', $parameter->getName()); + Assert::same(Parameter::IN_PATH, $parameter->getIn()); + Assert::null($parameter->getDescription()); + + Assert::null($parameter->isRequired()); + Assert::null($parameter->isDeprecated()); + Assert::null($parameter->isAllowEmptyValue()); + + Assert::null($parameter->getStyle()); + Assert::null($parameter->getExample()); + Assert::null($parameter->getSchema()); + + $realData = $parameter->toArray(); + $expectedData = ['name' => 'p1', 'in' => 'path']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); +}); + +// Test invalid in +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + new Parameter('foo', 'invalid'); + }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "in" given. It must be one of "cookie, header, path, query".'); +}); + +// Test schema reference +Toolkit::test(static function (): void { + $parameter = new Parameter('p1', Parameter::IN_PATH); + $schema = new Reference('ref'); + $parameter->setSchema($schema); + + Assert::same($schema, $parameter->getSchema()); + + $realData = $parameter->toArray(); + $expectedData = ['name' => 'p1', 'in' => 'path', 'schema' => ['$ref' => 'ref']]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Parameter::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ReferenceTest.php b/tests/Cases/Schema/ReferenceTest.php deleted file mode 100644 index 6cfa139..0000000 --- a/tests/Cases/Schema/ReferenceTest.php +++ /dev/null @@ -1,52 +0,0 @@ -setSummary('Server error'); - $ref->setDescription('Server error response, e.g. unhandled exception'); - - Assert::same('#/components/responses/ServerError', $ref->getRef()); - Assert::same('Server error', $ref->getSummary()); - Assert::same('Server error response, e.g. unhandled exception', $ref->getDescription()); - - $realData = $ref->toArray(); - $expectedData = [ - '$ref' => '#/components/responses/ServerError', - 'summary' => 'Server error', - 'description' => 'Server error response, e.g. unhandled exception', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Reference::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $ref = new Reference('#/components/responses/ServerError'); - - Assert::same('#/components/responses/ServerError', $ref->getRef()); - Assert::null($ref->getSummary()); - Assert::null($ref->getDescription()); - - $realData = $ref->toArray(); - $expectedData = ['$ref' => '#/components/responses/ServerError']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Reference::fromArray($realData)->toArray()); - } - -} - -(new ReferenceTest())->run(); diff --git a/tests/Cases/Schema/ReferenceTest.phpt b/tests/Cases/Schema/ReferenceTest.phpt new file mode 100644 index 0000000..57a58b5 --- /dev/null +++ b/tests/Cases/Schema/ReferenceTest.phpt @@ -0,0 +1,45 @@ +setSummary('Server error'); + $ref->setDescription('Server error response, e.g. unhandled exception'); + + Assert::same('#/components/responses/ServerError', $ref->getRef()); + Assert::same('Server error', $ref->getSummary()); + Assert::same('Server error response, e.g. unhandled exception', $ref->getDescription()); + + $realData = $ref->toArray(); + $expectedData = [ + '$ref' => '#/components/responses/ServerError', + 'summary' => 'Server error', + 'description' => 'Server error response, e.g. unhandled exception', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Reference::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $ref = new Reference('#/components/responses/ServerError'); + + Assert::same('#/components/responses/ServerError', $ref->getRef()); + Assert::null($ref->getSummary()); + Assert::null($ref->getDescription()); + + $realData = $ref->toArray(); + $expectedData = ['$ref' => '#/components/responses/ServerError']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Reference::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/RequestBodyTest.php b/tests/Cases/Schema/RequestBodyTest.php deleted file mode 100644 index 67b1926..0000000 --- a/tests/Cases/Schema/RequestBodyTest.php +++ /dev/null @@ -1,61 +0,0 @@ -addMediaType('text/*', $mediaType1); - $body->addMediaType('application/json', $mediaType1); // Intentionally added twice, tests overriding - $content['application/json'] = $mediaType2 = new MediaType(); - $body->addMediaType('application/json', $mediaType2); - - $body->setDescription('description'); - $body->setRequired(true); - - Assert::same($content, $body->getContent()); - Assert::same('description', $body->getDescription()); - Assert::true($body->isRequired()); - - $realData = $body->toArray(); - $expectedData = [ - 'description' => 'description', - 'content' => ['text/*' => [], 'application/json' => []], - 'required' => true, - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, RequestBody::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $body = new RequestBody(); - - Assert::same([], $body->getContent()); - Assert::null($body->getDescription()); - Assert::false($body->isRequired()); - - $realData = $body->toArray(); - $expectedData = ['content' => []]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, RequestBody::fromArray($realData)->toArray()); - } - -} - -(new RequestBodyTest())->run(); diff --git a/tests/Cases/Schema/RequestBodyTest.phpt b/tests/Cases/Schema/RequestBodyTest.phpt new file mode 100644 index 0000000..4e3aa9d --- /dev/null +++ b/tests/Cases/Schema/RequestBodyTest.phpt @@ -0,0 +1,54 @@ +addMediaType('text/*', $mediaType1); + $body->addMediaType('application/json', $mediaType1); // Intentionally added twice, tests overriding + $content['application/json'] = $mediaType2 = new MediaType(); + $body->addMediaType('application/json', $mediaType2); + + $body->setDescription('description'); + $body->setRequired(true); + + Assert::same($content, $body->getContent()); + Assert::same('description', $body->getDescription()); + Assert::true($body->isRequired()); + + $realData = $body->toArray(); + $expectedData = [ + 'description' => 'description', + 'content' => ['text/*' => [], 'application/json' => []], + 'required' => true, + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, RequestBody::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $body = new RequestBody(); + + Assert::same([], $body->getContent()); + Assert::null($body->getDescription()); + Assert::false($body->isRequired()); + + $realData = $body->toArray(); + $expectedData = ['content' => []]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, RequestBody::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ResponseTest.php b/tests/Cases/Schema/ResponseTest.php deleted file mode 100644 index 05c2c76..0000000 --- a/tests/Cases/Schema/ResponseTest.php +++ /dev/null @@ -1,65 +0,0 @@ - 'Description', - 'headers' => [ - 'WWW-Authenticate' => [ - 'description' => 'The authentication method that should be used to gain access to a resource', - 'schema' => ['type' => 'string'], - ], - ], - ]; - $response = new Response('Description'); - $header = new Header(); - $header->setDescription('The authentication method that should be used to gain access to a resource'); - $headerSchema = new Schema(['type' => 'string']); - $header->setSchema($headerSchema); - $response->setHeader('WWW-Authenticate', $header); - Assert::same($array, $response->toArray()); - Assert::equal($response, Response::fromArray($array)); - } - - public function testRequired(): void - { - $array = ['description' => 'Description']; - $response = new Response('Description'); - Assert::same($array, $response->toArray()); - Assert::equal($response, Response::fromArray($array)); - } - - public function testHeaderReference(): void - { - $array = [ - 'description' => 'API key or user token is missing or invalid', - 'headers' => [ - 'WWW-Authenticate' => [ - '$ref' => '#/components/header/WWW-Authenticate', - ], - ], - ]; - $response = new Response('API key or user token is missing or invalid'); - $headerReference = new Reference('#/components/header/WWW-Authenticate'); - $response->setHeader('WWW-Authenticate', $headerReference); - Assert::same($array, $response->toArray()); - Assert::equal($response, Response::fromArray($array)); - } - -} - -(new ResponseTest())->run(); diff --git a/tests/Cases/Schema/ResponseTest.phpt b/tests/Cases/Schema/ResponseTest.phpt new file mode 100644 index 0000000..b1f4457 --- /dev/null +++ b/tests/Cases/Schema/ResponseTest.phpt @@ -0,0 +1,58 @@ + 'Description', + 'headers' => [ + 'WWW-Authenticate' => [ + 'description' => 'The authentication method that should be used to gain access to a resource', + 'schema' => ['type' => 'string'], + ], + ], + ]; + $response = new Response('Description'); + $header = new Header(); + $header->setDescription('The authentication method that should be used to gain access to a resource'); + $headerSchema = new Schema(['type' => 'string']); + $header->setSchema($headerSchema); + $response->setHeader('WWW-Authenticate', $header); + Assert::same($array, $response->toArray()); + Assert::equal($response, Response::fromArray($array)); +}); + +// Test required fields +Toolkit::test(static function (): void { + $array = ['description' => 'Description']; + $response = new Response('Description'); + Assert::same($array, $response->toArray()); + Assert::equal($response, Response::fromArray($array)); +}); + +// Test header reference +Toolkit::test(static function (): void { + $array = [ + 'description' => 'API key or user token is missing or invalid', + 'headers' => [ + 'WWW-Authenticate' => [ + '$ref' => '#/components/header/WWW-Authenticate', + ], + ], + ]; + $response = new Response('API key or user token is missing or invalid'); + $headerReference = new Reference('#/components/header/WWW-Authenticate'); + $response->setHeader('WWW-Authenticate', $headerReference); + Assert::same($array, $response->toArray()); + Assert::equal($response, Response::fromArray($array)); +}); diff --git a/tests/Cases/Schema/ResponsesTest.php b/tests/Cases/Schema/ResponsesTest.php deleted file mode 100644 index a2b6d20..0000000 --- a/tests/Cases/Schema/ResponsesTest.php +++ /dev/null @@ -1,47 +0,0 @@ - ['description' => self::S200_DESCRIPTION], - '401' => ['$ref' => self::S401_REFERENCE], - ]; - - private const S200_DESCRIPTION = 'Success'; - - private const S401_REFERENCE = '#/components/responses/UnauthorizedError'; - - private Responses $responses; - - public function testFromArray(): void - { - $actual = Responses::fromArray(self::ARRAY); - Assert::equal($this->responses, $actual); - } - - public function testToArray(): void - { - Assert::equal(self::ARRAY, $this->responses->toArray()); - } - - protected function setUp(): void - { - $this->responses = new Responses(); - $this->responses->setResponse('200', new Response(self::S200_DESCRIPTION)); - $this->responses->setResponse('401', new Reference(self::S401_REFERENCE)); - } - -} - -(new ResponsesTest())->run(); diff --git a/tests/Cases/Schema/ResponsesTest.phpt b/tests/Cases/Schema/ResponsesTest.phpt new file mode 100644 index 0000000..e1abecd --- /dev/null +++ b/tests/Cases/Schema/ResponsesTest.phpt @@ -0,0 +1,40 @@ + ['description' => 'Success'], + '401' => ['$ref' => '#/components/responses/UnauthorizedError'], + ]; + + $responses = new Responses(); + $responses->setResponse('200', new Response('Success')); + $responses->setResponse('401', new Reference('#/components/responses/UnauthorizedError')); + + $actual = Responses::fromArray($array); + Assert::equal($responses, $actual); +}); + +// Test toArray +Toolkit::test(static function (): void { + $array = [ + '200' => ['description' => 'Success'], + '401' => ['$ref' => '#/components/responses/UnauthorizedError'], + ]; + + $responses = new Responses(); + $responses->setResponse('200', new Response('Success')); + $responses->setResponse('401', new Reference('#/components/responses/UnauthorizedError')); + + Assert::equal($array, $responses->toArray()); +}); diff --git a/tests/Cases/Schema/SecuritySchemeTest.php b/tests/Cases/Schema/SecuritySchemeTest.php deleted file mode 100644 index 1ce5f93..0000000 --- a/tests/Cases/Schema/SecuritySchemeTest.php +++ /dev/null @@ -1,203 +0,0 @@ - SecurityScheme::TYPE_API_KEY, - 'name' => 'api_key', - 'in' => SecurityScheme::IN_HEADER, - ], - ], - [ - [ - 'type' => SecurityScheme::TYPE_HTTP, - 'scheme' => 'basic', - ], - ], - [ - [ - 'type' => SecurityScheme::TYPE_HTTP, - 'scheme' => 'bearer', - 'bearerFormat' => 'JWT', - ], - ], - [ - [ - 'type' => SecurityScheme::TYPE_OAUTH2, - 'flows' => [ - 'implicit' => [ - 'authorizationUrl' => 'https://example.com/authorization', - 'tokenUrl' => 'https://example.com/token', - 'refreshUrl' => 'https://example.com/refresh', - 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], - ], - 'password' => [ - 'authorizationUrl' => 'https://example.com/authorization', - 'tokenUrl' => 'https://example.com/token', - 'refreshUrl' => 'https://example.com/refresh', - 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], - ], - 'clientCredentials' => [ - 'authorizationUrl' => 'https://example.com/authorization', - 'tokenUrl' => 'https://example.com/token', - 'refreshUrl' => 'https://example.com/refresh', - 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], - ], - 'authorizationCode' => [ - 'authorizationUrl' => 'https://example.com/authorization', - 'tokenUrl' => 'https://example.com/token', - 'refreshUrl' => 'https://example.com/refresh', - 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], - ], - ], - ], - ], - [ - [ - 'type' => SecurityScheme::TYPE_OPEN_ID_CONNECT, - 'openIdConnectUrl' => 'https://example.com/.well-known/openid-configuration', - ], - ], - ]; - } - - /** - * @dataProvider getRequiredData - * @param mixed[] $array - */ - public function testRequired(array $array): void - { - $securityScheme = SecurityScheme::fromArray($array); - Assert::same($array, $securityScheme->toArray()); - } - - public function testOptional(): void - { - $type = SecurityScheme::TYPE_API_KEY; - $name = 'api_key'; - $in = SecurityScheme::IN_HEADER; - $description = 'API key'; - $securityScheme = new SecurityScheme($type); - $securityScheme->setName($name); - $securityScheme->setIn($in); - $securityScheme->setDescription($description); - - Assert::same($type, $securityScheme->getType()); - Assert::same($name, $securityScheme->getName()); - Assert::same($in, $securityScheme->getIn()); - Assert::same($description, $securityScheme->getDescription()); - - $array = $securityScheme->toArray(); - $expected = [ - 'type' => $type, - 'name' => $name, - 'description' => $description, - 'in' => $in, - ]; - Assert::same($expected, $array); - Assert::same($expected, SecurityScheme::fromArray($array)->toArray()); - } - - public function testInvalidType(): void - { - Assert::exception(static function (): void { - new SecurityScheme('invalid'); - }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "type" given. It must be one of "apiKey, http, mutualTLS, oauth2, openIdConnect".'); - } - - public function testMissingName(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); - $securityScheme->setIn(SecurityScheme::IN_HEADER); - $securityScheme->setName(null); - }, InvalidArgumentException::class, 'Attribute "name" is required for type "apiKey".'); - } - - public function testMissingIn(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); - $securityScheme->setName('api_key'); - $securityScheme->setIn(null); - }, InvalidArgumentException::class, 'Attribute "in" is required for type "apiKey".'); - } - - public function testInvalidIn(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); - $securityScheme->setName('api_key'); - $securityScheme->setIn('invalid'); - }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "in" given. It must be one of "cookie, header, query".'); - } - - public function testMissingScheme(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_HTTP); - $securityScheme->setScheme(null); - }, InvalidArgumentException::class, 'Attribute "scheme" is required for type "http".'); - } - - public function testMissingBearerFormat(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_HTTP); - $securityScheme->setScheme('bearer'); - $securityScheme->setBearerFormat(null); - }, InvalidArgumentException::class, 'Attribute "bearerFormat" is required for type "http" and scheme "bearer".'); - } - - public function testMissingFlows(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OAUTH2); - $securityScheme->setFlows([]); - }, InvalidArgumentException::class, 'Attribute "flows" is required for type "oauth2".'); - } - - public function testMissingFlow(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OAUTH2); - $securityScheme->setFlows([ - 'implicit' => OAuthFlow::fromArray([ - 'authorizationUrl' => 'https://example.com/authorization', - 'tokenUrl' => 'https://example.com/token', - 'refreshUrl' => 'https://example.com/refresh', - 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], - ]), - ]); - }, InvalidArgumentException::class, 'Attribute "flows" is missing required key "password".'); - } - - public function testMissingOpenIdConnectUrl(): void - { - Assert::exception(static function (): void { - $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OPEN_ID_CONNECT); - $securityScheme->setOpenIdConnectUrl(null); - }, InvalidArgumentException::class, 'Attribute "openIdConnectUrl" is required for type "openIdConnect".'); - } - -} - -(new SecuritySchemeTest())->run(); diff --git a/tests/Cases/Schema/SecuritySchemeTest.phpt b/tests/Cases/Schema/SecuritySchemeTest.phpt new file mode 100644 index 0000000..583f24c --- /dev/null +++ b/tests/Cases/Schema/SecuritySchemeTest.phpt @@ -0,0 +1,197 @@ + SecurityScheme::TYPE_API_KEY, + 'name' => 'api_key', + 'in' => SecurityScheme::IN_HEADER, + ]; + $securityScheme = SecurityScheme::fromArray($array); + Assert::same($array, $securityScheme->toArray()); +}); + +// Test required fields - HTTP basic +Toolkit::test(static function (): void { + $array = [ + 'type' => SecurityScheme::TYPE_HTTP, + 'scheme' => 'basic', + ]; + $securityScheme = SecurityScheme::fromArray($array); + Assert::same($array, $securityScheme->toArray()); +}); + +// Test required fields - HTTP bearer +Toolkit::test(static function (): void { + $array = [ + 'type' => SecurityScheme::TYPE_HTTP, + 'scheme' => 'bearer', + 'bearerFormat' => 'JWT', + ]; + $securityScheme = SecurityScheme::fromArray($array); + Assert::same($array, $securityScheme->toArray()); +}); + +// Test required fields - OAuth2 +Toolkit::test(static function (): void { + $array = [ + 'type' => SecurityScheme::TYPE_OAUTH2, + 'flows' => [ + 'implicit' => [ + 'authorizationUrl' => 'https://example.com/authorization', + 'tokenUrl' => 'https://example.com/token', + 'refreshUrl' => 'https://example.com/refresh', + 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], + ], + 'password' => [ + 'authorizationUrl' => 'https://example.com/authorization', + 'tokenUrl' => 'https://example.com/token', + 'refreshUrl' => 'https://example.com/refresh', + 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], + ], + 'clientCredentials' => [ + 'authorizationUrl' => 'https://example.com/authorization', + 'tokenUrl' => 'https://example.com/token', + 'refreshUrl' => 'https://example.com/refresh', + 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], + ], + 'authorizationCode' => [ + 'authorizationUrl' => 'https://example.com/authorization', + 'tokenUrl' => 'https://example.com/token', + 'refreshUrl' => 'https://example.com/refresh', + 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], + ], + ], + ]; + $securityScheme = SecurityScheme::fromArray($array); + Assert::same($array, $securityScheme->toArray()); +}); + +// Test required fields - OpenID Connect +Toolkit::test(static function (): void { + $array = [ + 'type' => SecurityScheme::TYPE_OPEN_ID_CONNECT, + 'openIdConnectUrl' => 'https://example.com/.well-known/openid-configuration', + ]; + $securityScheme = SecurityScheme::fromArray($array); + Assert::same($array, $securityScheme->toArray()); +}); + +// Test optional fields +Toolkit::test(static function (): void { + $type = SecurityScheme::TYPE_API_KEY; + $name = 'api_key'; + $in = SecurityScheme::IN_HEADER; + $description = 'API key'; + $securityScheme = new SecurityScheme($type); + $securityScheme->setName($name); + $securityScheme->setIn($in); + $securityScheme->setDescription($description); + + Assert::same($type, $securityScheme->getType()); + Assert::same($name, $securityScheme->getName()); + Assert::same($in, $securityScheme->getIn()); + Assert::same($description, $securityScheme->getDescription()); + + $array = $securityScheme->toArray(); + $expected = [ + 'type' => $type, + 'name' => $name, + 'description' => $description, + 'in' => $in, + ]; + Assert::same($expected, $array); + Assert::same($expected, SecurityScheme::fromArray($array)->toArray()); +}); + +// Test invalid type +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + new SecurityScheme('invalid'); + }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "type" given. It must be one of "apiKey, http, mutualTLS, oauth2, openIdConnect".'); +}); + +// Test missing name +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); + $securityScheme->setIn(SecurityScheme::IN_HEADER); + $securityScheme->setName(null); + }, InvalidArgumentException::class, 'Attribute "name" is required for type "apiKey".'); +}); + +// Test missing in +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); + $securityScheme->setName('api_key'); + $securityScheme->setIn(null); + }, InvalidArgumentException::class, 'Attribute "in" is required for type "apiKey".'); +}); + +// Test invalid in +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_API_KEY); + $securityScheme->setName('api_key'); + $securityScheme->setIn('invalid'); + }, InvalidArgumentException::class, 'Invalid value "invalid" for attribute "in" given. It must be one of "cookie, header, query".'); +}); + +// Test missing scheme +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_HTTP); + $securityScheme->setScheme(null); + }, InvalidArgumentException::class, 'Attribute "scheme" is required for type "http".'); +}); + +// Test missing bearer format +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_HTTP); + $securityScheme->setScheme('bearer'); + $securityScheme->setBearerFormat(null); + }, InvalidArgumentException::class, 'Attribute "bearerFormat" is required for type "http" and scheme "bearer".'); +}); + +// Test missing flows +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OAUTH2); + $securityScheme->setFlows([]); + }, InvalidArgumentException::class, 'Attribute "flows" is required for type "oauth2".'); +}); + +// Test missing flow +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OAUTH2); + $securityScheme->setFlows([ + 'implicit' => OAuthFlow::fromArray([ + 'authorizationUrl' => 'https://example.com/authorization', + 'tokenUrl' => 'https://example.com/token', + 'refreshUrl' => 'https://example.com/refresh', + 'scopes' => ['read' => 'Read access', 'write' => 'Write access'], + ]), + ]); + }, InvalidArgumentException::class, 'Attribute "flows" is missing required key "password".'); +}); + +// Test missing openIdConnectUrl +Toolkit::test(static function (): void { + Assert::exception(static function (): void { + $securityScheme = new SecurityScheme(SecurityScheme::TYPE_OPEN_ID_CONNECT); + $securityScheme->setOpenIdConnectUrl(null); + }, InvalidArgumentException::class, 'Attribute "openIdConnectUrl" is required for type "openIdConnect".'); +}); diff --git a/tests/Cases/Schema/ServerTest.php b/tests/Cases/Schema/ServerTest.php deleted file mode 100644 index 9c9b76e..0000000 --- a/tests/Cases/Schema/ServerTest.php +++ /dev/null @@ -1,62 +0,0 @@ -setDescription('description'); - - $variables = []; - $variables['var1'] = $variable1 = new ServerVariable('default'); - $server->addVariable('var1', $variable1); - $server->addVariable('var2', $variable1); // Intentionally added twice, tests overriding - $variables['var2'] = $variable2 = new ServerVariable('default'); - $server->addVariable('var2', $variable2); - - Assert::same('https://example.com', $server->getUrl()); - Assert::same('description', $server->getDescription()); - Assert::same($variables, $server->getVariables()); - - $realData = $server->toArray(); - $expectedData = [ - 'url' => 'https://example.com', - 'description' => 'description', - 'variables' => [ - 'var1' => ['default' => 'default'], - 'var2' => ['default' => 'default'], - ], - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Server::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $server = new Server('https://example.com'); - - Assert::same('https://example.com', $server->getUrl()); - Assert::null($server->getDescription()); - Assert::same([], $server->getVariables()); - - $realData = $server->toArray(); - $expectedData = ['url' => 'https://example.com']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Server::fromArray($realData)->toArray()); - } - -} - -(new ServerTest())->run(); diff --git a/tests/Cases/Schema/ServerTest.phpt b/tests/Cases/Schema/ServerTest.phpt new file mode 100644 index 0000000..1aa6899 --- /dev/null +++ b/tests/Cases/Schema/ServerTest.phpt @@ -0,0 +1,55 @@ +setDescription('description'); + + $variables = []; + $variables['var1'] = $variable1 = new ServerVariable('default'); + $server->addVariable('var1', $variable1); + $server->addVariable('var2', $variable1); // Intentionally added twice, tests overriding + $variables['var2'] = $variable2 = new ServerVariable('default'); + $server->addVariable('var2', $variable2); + + Assert::same('https://example.com', $server->getUrl()); + Assert::same('description', $server->getDescription()); + Assert::same($variables, $server->getVariables()); + + $realData = $server->toArray(); + $expectedData = [ + 'url' => 'https://example.com', + 'description' => 'description', + 'variables' => [ + 'var1' => ['default' => 'default'], + 'var2' => ['default' => 'default'], + ], + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Server::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $server = new Server('https://example.com'); + + Assert::same('https://example.com', $server->getUrl()); + Assert::null($server->getDescription()); + Assert::same([], $server->getVariables()); + + $realData = $server->toArray(); + $expectedData = ['url' => 'https://example.com']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Server::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/ServerVariableTest.php b/tests/Cases/Schema/ServerVariableTest.php deleted file mode 100644 index 47881b0..0000000 --- a/tests/Cases/Schema/ServerVariableTest.php +++ /dev/null @@ -1,52 +0,0 @@ -setDescription('description'); - $variable->setEnum(['foo', 'bar', 'baz']); - - Assert::same('default', $variable->getDefault()); - Assert::same('description', $variable->getDescription()); - Assert::same(['foo', 'bar', 'baz'], $variable->getEnum()); - - $realData = $variable->toArray(); - $expectedData = [ - 'enum' => ['foo', 'bar', 'baz'], - 'default' => 'default', - 'description' => 'description', - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, ServerVariable::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $variable = new ServerVariable('default'); - - Assert::same('default', $variable->getDefault()); - Assert::null($variable->getDescription()); - Assert::same([], $variable->getEnum()); - - $realData = $variable->toArray(); - $expectedData = ['default' => 'default']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, ServerVariable::fromArray($realData)->toArray()); - } - -} - -(new ServerVariableTest())->run(); diff --git a/tests/Cases/Schema/ServerVariableTest.phpt b/tests/Cases/Schema/ServerVariableTest.phpt new file mode 100644 index 0000000..ad5109d --- /dev/null +++ b/tests/Cases/Schema/ServerVariableTest.phpt @@ -0,0 +1,45 @@ +setDescription('description'); + $variable->setEnum(['foo', 'bar', 'baz']); + + Assert::same('default', $variable->getDefault()); + Assert::same('description', $variable->getDescription()); + Assert::same(['foo', 'bar', 'baz'], $variable->getEnum()); + + $realData = $variable->toArray(); + $expectedData = [ + 'enum' => ['foo', 'bar', 'baz'], + 'default' => 'default', + 'description' => 'description', + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, ServerVariable::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $variable = new ServerVariable('default'); + + Assert::same('default', $variable->getDefault()); + Assert::null($variable->getDescription()); + Assert::same([], $variable->getEnum()); + + $realData = $variable->toArray(); + $expectedData = ['default' => 'default']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, ServerVariable::fromArray($realData)->toArray()); +}); diff --git a/tests/Cases/Schema/TagTest.php b/tests/Cases/Schema/TagTest.php deleted file mode 100644 index 9d0fea8..0000000 --- a/tests/Cases/Schema/TagTest.php +++ /dev/null @@ -1,55 +0,0 @@ -setDescription('Pets operations'); - - $externalDocs = new ExternalDocumentation('https://example.com'); - $tag->setExternalDocs($externalDocs); - - Assert::same('pet', $tag->getName()); - Assert::same('Pets operations', $tag->getDescription()); - Assert::same($externalDocs, $tag->getExternalDocs()); - - $realData = $tag->toArray(); - $expectedData = [ - 'name' => 'pet', - 'description' => 'Pets operations', - 'externalDocs' => ['url' => 'https://example.com'], - ]; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Tag::fromArray($realData)->toArray()); - } - - public function testRequired(): void - { - $tag = new Tag('pet'); - - Assert::same('pet', $tag->getName()); - Assert::null($tag->getDescription()); - Assert::null($tag->getExternalDocs()); - - $realData = $tag->toArray(); - $expectedData = ['name' => 'pet']; - - Assert::same($expectedData, $realData); - Assert::same($expectedData, Tag::fromArray($realData)->toArray()); - } - -} - -(new TagTest())->run(); diff --git a/tests/Cases/Schema/TagTest.phpt b/tests/Cases/Schema/TagTest.phpt new file mode 100644 index 0000000..8d92913 --- /dev/null +++ b/tests/Cases/Schema/TagTest.phpt @@ -0,0 +1,48 @@ +setDescription('Pets operations'); + + $externalDocs = new ExternalDocumentation('https://example.com'); + $tag->setExternalDocs($externalDocs); + + Assert::same('pet', $tag->getName()); + Assert::same('Pets operations', $tag->getDescription()); + Assert::same($externalDocs, $tag->getExternalDocs()); + + $realData = $tag->toArray(); + $expectedData = [ + 'name' => 'pet', + 'description' => 'Pets operations', + 'externalDocs' => ['url' => 'https://example.com'], + ]; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Tag::fromArray($realData)->toArray()); +}); + +// Test required fields +Toolkit::test(static function (): void { + $tag = new Tag('pet'); + + Assert::same('pet', $tag->getName()); + Assert::null($tag->getDescription()); + Assert::null($tag->getExternalDocs()); + + $realData = $tag->toArray(); + $expectedData = ['name' => 'pet']; + + Assert::same($expectedData, $realData); + Assert::same($expectedData, Tag::fromArray($realData)->toArray()); +});