From ee8147745c92dd4404adb531751ce32676b888d4 Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Fri, 13 Jan 2023 13:51:01 +1300 Subject: [PATCH] Enum cleanup (#1381) Simplifies enum expansion rules and adds documentation for it. --- Examples/using-refs/using-refs.yaml | 2 +- docs/guide/common-techniques.md | 179 +++++++++++++++++- docs/reference/processors.md | 6 +- src/Processors/Concerns/TypesTrait.php | 7 + src/Processors/ExpandEnums.php | 52 +++-- tests/Analysers/ReflectionAnalyserTest.php | 30 ++- tests/Analysers/TokenAnalyserTest.php | 26 ++- tests/Analysers/TokenScannerTest.php | 12 +- .../AnnotationPropertiesDefinedTest.php | 4 +- tests/Annotations/AttachableTest.php | 9 +- tests/Annotations/ComponentsTest.php | 10 +- tests/Annotations/LicenseTest.php | 6 +- tests/Annotations/OpenApiTest.php | 6 +- tests/Annotations/SecuritySchemesTest.php | 12 +- tests/Annotations/ValidateRelationsTest.php | 4 +- tests/ContextTest.php | 18 +- tests/ExamplesTest.php | 34 ++-- .../Fixtures/Annotations/CustomAttachable.php | 7 +- tests/Fixtures/Apis/Attributes/basic.php | 1 - .../Fixtures/Attributes/CustomAttachable.php | 7 +- tests/Fixtures/PHP/AbstractKeyword.php | 8 +- tests/Fixtures/PHP/Enums/ReferencesEnum.php | 108 ----------- tests/Fixtures/PHP/Enums/StatusEnum.php | 4 +- tests/Fixtures/PHP/Enums/StatusEnumBacked.php | 4 +- .../PHP/Enums/StatusEnumIntegerBacked.php | 4 +- .../PHP/Enums/StatusEnumStringBacked.php | 4 +- tests/Fixtures/PHP/Php8PromotedProperties.php | 4 +- tests/Fixtures/PHP/ReferencesEnum.php | 71 +++++++ tests/OpenApiTestCase.php | 28 ++- tests/Processors/AugmentPropertiesTest.php | 4 +- tests/Processors/BuildPathsTest.php | 23 +-- tests/Processors/CleanUnmergedTest.php | 9 +- tests/Processors/DocBlockDescriptionsTest.php | 6 +- tests/Processors/ExpandClassesTest.php | 41 ++-- tests/Processors/ExpandEnumsTest.php | 45 +++-- tests/Processors/MergeIntoComponentsTest.php | 7 +- tests/Processors/MergeIntoOpenApiTest.php | 7 +- tests/Processors/MergeJsonContentTest.php | 15 +- tests/Processors/MergeXmlContentTest.php | 15 +- tests/Processors/OperationIdTest.php | 15 +- tests/RefTest.php | 7 +- tests/ScratchTest.php | 4 +- tests/SerializerTest.php | 45 +++-- tests/UtilTest.php | 7 +- 44 files changed, 518 insertions(+), 399 deletions(-) delete mode 100644 tests/Fixtures/PHP/Enums/ReferencesEnum.php create mode 100644 tests/Fixtures/PHP/ReferencesEnum.php diff --git a/Examples/using-refs/using-refs.yaml b/Examples/using-refs/using-refs.yaml index 443e5eb68..28a14067f 100644 --- a/Examples/using-refs/using-refs.yaml +++ b/Examples/using-refs/using-refs.yaml @@ -64,7 +64,7 @@ components: stockLevel: $ref: '#/components/schemas/StockLevel' StockLevel: - type: integer + type: string enum: - AVAILABLE - SOLD_OUT diff --git a/docs/guide/common-techniques.md b/docs/guide/common-techniques.md index dd3a19fe0..fd0b5d206 100644 --- a/docs/guide/common-techniques.md +++ b/docs/guide/common-techniques.md @@ -8,7 +8,7 @@ relevant source code as appropriate. `swagger-php` will scan your project and merge all meta-data into one` @OA\OpenApi` annotation. ::: warning -As of `swagger-php` v4 all annotations or attributes must be associated with +As of `swagger-php` v4 all annotations or attributes must be associated with a structural element (`class`, `method`, `parameter` or `enum`) ::: @@ -188,3 +188,180 @@ info: - version: "2.1" level: fullapi ``` + +## Enums + +[PHP 8.1 enums](https://www.php.net/manual/en/language.enumerations.basics.php) are supported in two main use cases. + +### Enum cases as value + +Enum cases can be used as value in an `enum` list just like a `string`, `integer` or any other primitive type. + +**Basic enum:** + +```php +use OpenApi\Attributes as OAT; + +enum Suit +{ + case Hearts; + case Diamonds; + case Clubs; + case Spades; +} + +class Model +{ + #[OAT\Property(enum: [Suit::Hearts, Suit::Diamonds])] + protected array $someSuits; +} +``` + +**Results in:** + +```yaml +openapi: 3.0.0 +components: + schemas: + Model: + properties: + someSuits: + type: array + enum: + - Hearts + - Diamonds + type: object + +``` + +**Backed enums** + +If the enum is a backed enum, the case backing value is used instead of the name. + +## Enum schemas + +The simples way of using enums is to annotate them as `Schema`. This allows you to reference them like any other schema in your spec. + +```php +use OpenApi\Attributes as OAT; + +#[OAT\Schema()] +enum Colour +{ + case GREEN; + case BLUE; + case RED; +} + +#[OAT\Schema()] +class Product +{ + #[OAT\Property()] + public Colour $colour; +} +``` + +**Results in:** + +```yaml +openapi: 3.0.0 +components: + schemas: + Colour: + type: string + enum: + - GREEN + - BLUE + - RED + Product: + properties: + colour: + $ref: '#/components/schemas/Colour' + type: object +``` + +**Backed enums** + +For backed enums there exist two rules that determine whether the name or backing value is used: +1. If no schema type is given, the enum name is used. +2. If a schema type is given, and it matches the backing type, the enum backing value is used. + +**Using the name of a backed enum:** + +```php +use OpenApi\Attributes as OAT; + +#[OAT\Schema()] +enum Colour: int +{ + case GREEN = 1; + case BLUE = 2; + case RED = 3; +} + +#[OAT\Schema()] +class Product +{ + #[OAT\Property()] + public Colour $colour; +} +``` + +**Results in:** + +```yaml +openapi: 3.0.0 +components: + schemas: + Colour: + type: string + enum: + - GREEN + - BLUE + - RED + Product: + properties: + colour: + $ref: '#/components/schemas/Colour' + type: object +``` + +**Using the backing value:** + +```php +use OpenApi\Attributes as OAT; + +#[OAT\Schema(type: 'integer')] +enum Colour: int +{ + case GREEN = 1; + case BLUE = 2; + case RED = 3; +} + +#[OAT\Schema()] +class Product +{ + #[OAT\Property()] + public Colour $colour; +} +``` + +**Results in:** + +```yaml +openapi: 3.0.0 +components: + schemas: + Colour: + type: integer + enum: + - 1 + - 2 + - 3 + Product: + properties: + colour: + $ref: '#/components/schemas/Colour' + type: object +``` diff --git a/docs/reference/processors.md b/docs/reference/processors.md index 366b43401..0f674e557 100644 --- a/docs/reference/processors.md +++ b/docs/reference/processors.md @@ -35,9 +35,9 @@ Look at all (direct) traits for a schema and: - inherit from the trait if it has a schema (allOf). ## [ExpandEnums](https://github.com/zircote/swagger-php/tree/master/src/Processors/ExpandEnums.php) -Look at all enums with a schema and: -- set the name `schema` -- set `enum` values. +Expands PHP enums. + +Determines `schema`, `enum` and `type`. ## [AugmentSchemas](https://github.com/zircote/swagger-php/tree/master/src/Processors/AugmentSchemas.php) Use the Schema context to extract useful information and inject that into the annotation. diff --git a/src/Processors/Concerns/TypesTrait.php b/src/Processors/Concerns/TypesTrait.php index feb6907c2..1da3ee5ee 100644 --- a/src/Processors/Concerns/TypesTrait.php +++ b/src/Processors/Concerns/TypesTrait.php @@ -51,4 +51,11 @@ public function mapNativeType(OA\Schema $schema, string $type): bool return true; } + + public function native2spec(string $type): string + { + $mapped = array_key_exists($type, self::$NATIVE_TYPE_MAP) ? self::$NATIVE_TYPE_MAP[$type] : $type; + + return is_array($mapped) ? $mapped[0] : $mapped; + } } diff --git a/src/Processors/ExpandEnums.php b/src/Processors/ExpandEnums.php index d31b712f9..517e55bf4 100644 --- a/src/Processors/ExpandEnums.php +++ b/src/Processors/ExpandEnums.php @@ -12,9 +12,9 @@ use OpenApi\Generator; /** - * Look at all enums with a schema and: - * - set the name `schema` - * - set `enum` values. + * Expands PHP enums. + * + * Determines `schema`, `enum` and `type`. */ class ExpandEnums { @@ -30,39 +30,37 @@ public function __invoke(Analysis $analysis) $this->expandSchemaEnum($analysis); } - private function expandContextEnum(Analysis $analysis): void + protected function expandContextEnum(Analysis $analysis): void { /** @var OA\Schema[] $schemas */ $schemas = $analysis->getAnnotationsOfType([OA\Schema::class, OAT\Schema::class], true); foreach ($schemas as $schema) { if ($schema->_context->is('enum')) { - $source = $schema->_context->enum; - $re = new \ReflectionEnum($schema->_context->fullyQualifiedName($source)); + $re = new \ReflectionEnum($schema->_context->fullyQualifiedName($schema->_context->enum)); $schema->schema = !Generator::isDefault($schema->schema) ? $schema->schema : $re->getShortName(); - $type = 'string'; - $schemaType = 'string'; - if ($re->isBacked() && ($backingType = $re->getBackingType()) && method_exists($backingType, 'getName')) { - if (Generator::isDefault($schema->type)) { - $type = $backingType->getName(); - } else { - $type = $schema->type; - $schemaType = $schema->type; - } + + $schemaType = $schema->type; + $enumType = null; + if ($re->isBacked() && ($backingType = $re->getBackingType()) && $backingType instanceof \ReflectionNamedType) { + $enumType = $backingType->getName(); } - $schema->enum = array_map(function ($case) use ($re, $schemaType, $type) { - if ($re->isBacked() && $type === $schemaType) { - return $case->getBackingValue(); - } - return $case->name; + // no (or invalid) schema type means name + $useName = Generator::isDefault($schemaType) || ($enumType && $this->native2spec($enumType) != $schemaType); + + $schema->enum = array_map(function ($case) use ($useName) { + return $useName ? $case->name : $case->getBackingValue(); }, $re->getCases()); - $this->mapNativeType($schema, $type); + + $schema->type = $useName ? 'string' : $enumType; + + $this->mapNativeType($schema, $schemaType); } } } - private function expandSchemaEnum(Analysis $analysis): void + protected function expandSchemaEnum(Analysis $analysis): void { /** @var OA\Schema[] $schemas */ $schemas = $analysis->getAnnotationsOfType([ @@ -78,21 +76,21 @@ private function expandSchemaEnum(Analysis $analysis): void } if (is_string($schema->enum)) { - // might be enum class + // might be enum class-string if (is_a($schema->enum, \UnitEnum::class, true)) { - $source = $schema->enum::cases(); + $cases = $schema->enum::cases(); } else { throw new \InvalidArgumentException("Unexpected enum value, requires specifying the Enum class string: $schema->enum"); } } elseif (is_array($schema->enum)) { - // might be array of enum, string, int, etc... - $source = $schema->enum; + // might be an array of \UnitEnum::class, string, int, etc... + $cases = $schema->enum; } else { throw new \InvalidArgumentException('Unexpected enum value, requires Enum class string or array'); } $enums = []; - foreach ($source as $enum) { + foreach ($cases as $enum) { if (is_a($enum, \UnitEnum::class)) { $enums[] = $enum->value ?? $enum->name; } else { diff --git a/tests/Analysers/ReflectionAnalyserTest.php b/tests/Analysers/ReflectionAnalyserTest.php index 6be2409c9..f92f8fd50 100644 --- a/tests/Analysers/ReflectionAnalyserTest.php +++ b/tests/Analysers/ReflectionAnalyserTest.php @@ -13,12 +13,8 @@ use OpenApi\Analysers\ReflectionAnalyser; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Analysis; -use OpenApi\Annotations\OpenApi; -use OpenApi\Annotations\Operation; -use OpenApi\Annotations\Property; -use OpenApi\Annotations\Response; -use OpenApi\Annotations\Schema; -use OpenApi\Attributes\Get; +use OpenApi\Annotations as OA; +use OpenApi\Attributes as OAT; use OpenApi\Context; use OpenApi\Generator; use OpenApi\Processors\CleanUnusedComponents; @@ -91,7 +87,7 @@ public function testApiDocBlockBasic(AnalyserInterface $analyser): void require_once $this->fixture('Apis/DocBlocks/basic.php'); $analysis = (new Generator()) - ->setVersion(OpenApi::VERSION_3_1_0) + ->setVersion(OA\OpenApi::VERSION_3_1_0) ->withContext(function (Generator $generator) use ($analyser) { $analyser->setGenerator($generator); $analysis = $analyser->fromFile($this->fixture('Apis/DocBlocks/basic.php'), $this->getContext([], $generator->getVersion())); @@ -100,7 +96,7 @@ public function testApiDocBlockBasic(AnalyserInterface $analyser): void return $analysis; }); - $operations = $analysis->getAnnotationsOfType(Operation::class); + $operations = $analysis->getAnnotationsOfType(OA\Operation::class); $this->assertIsArray($operations); $spec = $this->fixture('Apis/basic.yaml'); @@ -130,7 +126,7 @@ public function testApiAttributesBasic(AnalyserInterface $analyser): void return $analysis; }); - $operations = $analysis->getAnnotationsOfType(Operation::class); + $operations = $analysis->getAnnotationsOfType(OA\Operation::class); $this->assertIsArray($operations); $spec = $this->fixture('Apis/basic.yaml'); @@ -139,14 +135,14 @@ public function testApiAttributesBasic(AnalyserInterface $analyser): void $this->assertSpecEquals($analysis->openapi, file_get_contents($spec)); // check CustomAttachable is only attached to @OA\Get - /** @var Get[] $gets */ - $gets = $analysis->getAnnotationsOfType(Get::class, true); + /** @var OA\Get[] $gets */ + $gets = $analysis->getAnnotationsOfType(OAT\Get::class, true); $this->assertCount(2, $gets); $this->assertTrue(is_array($gets[0]->attachables), 'Attachables not set'); $this->assertCount(1, $gets[0]->attachables); - /** @var Response[] $responses */ - $responses = $analysis->getAnnotationsOfType(Response::class, true); + /** @var OA\Response[] $responses */ + $responses = $analysis->getAnnotationsOfType(OA\Response::class, true); foreach ($responses as $response) { $this->assertEquals(Generator::UNDEFINED, $response->attachables); } @@ -170,7 +166,7 @@ public function testApiMixedBasic(AnalyserInterface $analyser): void return $analysis; }); - $operations = $analysis->getAnnotationsOfType(Operation::class); + $operations = $analysis->getAnnotationsOfType(OA\Operation::class); $this->assertIsArray($operations); $spec = $this->fixture('Apis/basic.yaml'); @@ -189,13 +185,13 @@ public function testPhp8PromotedProperties(): void } $analysis = $this->analysisFromFixtures(['PHP/Php8PromotedProperties.php']); - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(1, $schemas); $analysis->process($this->processors([CleanUnusedComponents::class])); - /** @var Property[] $properties */ - $properties = $analysis->getAnnotationsOfType(Property::class); + /** @var OA\Property[] $properties */ + $properties = $analysis->getAnnotationsOfType(OA\Property::class); $this->assertCount(2, $properties); $this->assertEquals('id', $properties[0]->property); $this->assertEquals('labels', $properties[1]->property); diff --git a/tests/Analysers/TokenAnalyserTest.php b/tests/Analysers/TokenAnalyserTest.php index 6ad6c83e4..74280c83f 100644 --- a/tests/Analysers/TokenAnalyserTest.php +++ b/tests/Analysers/TokenAnalyserTest.php @@ -7,9 +7,7 @@ namespace OpenApi\Tests\Analysers; use OpenApi\Analysis; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\Property; -use OpenApi\Annotations\Schema; +use OpenApi\Annotations as OA; use OpenApi\Context; use OpenApi\Generator; use OpenApi\Analysers\TokenAnalyser; @@ -238,8 +236,8 @@ public function testDescription(array $type, string $name, string $fixture, stri public function testNamespacedConstAccess(): void { $analysis = $this->analysisFromFixtures(['Parser/User.php']); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(1, $schemas); $this->assertEquals(User::CONSTANT, $schemas[0]->example); @@ -251,14 +249,14 @@ public function testNamespacedConstAccess(): void public function testPhp8AttributeMix(): void { $analysis = $this->analysisFromFixtures(['PHP/Label.php', 'PHP/Php8AttrMix.php']); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(1, $schemas); $analysis->process($this->processors([CleanUnusedComponents::class])); - /** @var Property[] $properties */ - $properties = $analysis->getAnnotationsOfType(Property::class, true); + /** @var OA\Property[] $properties */ + $properties = $analysis->getAnnotationsOfType(OA\Property::class, true); $this->assertCount(2, $properties); $this->assertEquals('id', $properties[0]->property); $this->assertEquals('otherId', $properties[1]->property); @@ -270,13 +268,13 @@ public function testPhp8AttributeMix(): void public function testPhp8PromotedProperties(): void { $analysis = $this->analysisFromFixtures(['PHP/Php8PromotedProperties.php'], [], new TokenAnalyser()); - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(1, $schemas); $analysis->process($this->processors([CleanUnusedComponents::class])); - /** @var Property[] $properties */ - $properties = $analysis->getAnnotationsOfType(Property::class); + /** @var OA\Property[] $properties */ + $properties = $analysis->getAnnotationsOfType(OA\Property::class); // ignores the attribute on $id $this->assertCount(1, $properties); $this->assertEquals('labels', $properties[0]->property); @@ -287,7 +285,7 @@ public function testAnonymousFunctions(): void $analysis = $this->analysisFromFixtures(['PHP/AnonymousFunctions.php'], [], new TokenAnalyser()); $analysis->process((new Generator())->getProcessors()); - $infos = $analysis->getAnnotationsOfType(Info::class, true); + $infos = $analysis->getAnnotationsOfType(OA\Info::class, true); $this->assertCount(1, $infos); } @@ -297,7 +295,7 @@ public function testAnonymousFunctions(): void public function testPhp8NamedArguments(): void { $analysis = $this->analysisFromFixtures(['PHP/Php8NamedArguments.php'], [], new TokenAnalyser()); - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(1, $schemas); $analysis->process((new Generator())->getProcessors()); diff --git a/tests/Analysers/TokenScannerTest.php b/tests/Analysers/TokenScannerTest.php index 8b41b515d..5e950c247 100644 --- a/tests/Analysers/TokenScannerTest.php +++ b/tests/Analysers/TokenScannerTest.php @@ -19,7 +19,7 @@ public function scanCases(): iterable [ 'OpenApi\Tests\Fixtures\PHP\AbstractKeyword' => [ 'uses' => [ - 'Property' => 'OpenApi\Attributes\Property', + 'OAT' => 'OpenApi\Attributes', ], 'interfaces' => [], 'traits' => [], @@ -225,7 +225,7 @@ public function scanCases(): iterable [ 'OpenApi\\Tests\\Fixtures\\PHP\\Php8PromotedProperties' => [ 'uses' => [ - 'Property' => 'OpenApi\\Attributes\\Property', + 'OAT' => 'OpenApi\\Attributes', 'OA' => 'OpenApi\Annotations', ], 'interfaces' => [], @@ -326,7 +326,9 @@ public function scanCases(): iterable 'PHP/Enums/StatusEnum.php', [ 'OpenApi\\Tests\\Fixtures\\PHP\\Enums\\StatusEnum' => [ - 'uses' => ['Schema' => 'OpenApi\\Attributes\\Schema'], + 'uses' => [ + 'OAT' => 'OpenApi\\Attributes', + ], 'interfaces' => [], 'enums' => [], 'traits' => [], @@ -340,7 +342,9 @@ public function scanCases(): iterable 'PHP/Enums/StatusEnumBacked.php', [ 'OpenApi\\Tests\\Fixtures\\PHP\\Enums\\StatusEnumBacked' => [ - 'uses' => ['Schema' => 'OpenApi\\Attributes\\Schema'], + 'uses' => [ + 'OAT' => 'OpenApi\\Attributes', + ], 'interfaces' => [], 'enums' => [], 'traits' => [], diff --git a/tests/Annotations/AnnotationPropertiesDefinedTest.php b/tests/Annotations/AnnotationPropertiesDefinedTest.php index 9fd77e0d9..38bc7b91e 100644 --- a/tests/Annotations/AnnotationPropertiesDefinedTest.php +++ b/tests/Annotations/AnnotationPropertiesDefinedTest.php @@ -7,7 +7,7 @@ namespace OpenApi\Tests\Annotations; use function \get_class_vars; -use OpenApi\Annotations\AbstractAnnotation; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; class AnnotationPropertiesDefinedTest extends OpenApiTestCase @@ -18,7 +18,7 @@ class AnnotationPropertiesDefinedTest extends OpenApiTestCase public function testPropertiesAreNotUndefined(string $annotation): void { $properties = get_class_vars($annotation); - $skip = AbstractAnnotation::$_blacklist; + $skip = OA\AbstractAnnotation::$_blacklist; foreach ($properties as $property => $value) { if (in_array($property, $skip)) { continue; diff --git a/tests/Annotations/AttachableTest.php b/tests/Annotations/AttachableTest.php index c80c2121c..70e6f30cd 100644 --- a/tests/Annotations/AttachableTest.php +++ b/tests/Annotations/AttachableTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Annotations; use OpenApi\Analysis; -use OpenApi\Annotations\Attachable; -use OpenApi\Annotations\Schema; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\CleanUnusedComponents; use OpenApi\Tests\Fixtures\Annotations\CustomAttachable; @@ -20,10 +19,10 @@ public function testAttachablesAreAttached(): void { $analysis = $this->analysisFromFixtures(['UsingVar.php']); - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(2, $schemas[0]->attachables); - $this->assertInstanceOf(Attachable::class, $schemas[0]->attachables[0]); + $this->assertInstanceOf(OA\Attachable::class, $schemas[0]->attachables[0]); } public function testCustomAttachableImplementationsAreAttached(): void @@ -35,7 +34,7 @@ public function testCustomAttachableImplementationsAreAttached(): void ->setProcessors($this->processors([CleanUnusedComponents::class])) ->generate($this->fixtures(['UsingCustomAttachables.php']), $analysis); - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(2, $schemas[0]->attachables); $this->assertInstanceOf(CustomAttachable::class, $schemas[0]->attachables[0]); diff --git a/tests/Annotations/ComponentsTest.php b/tests/Annotations/ComponentsTest.php index 943ac3cc5..ae678d9ac 100644 --- a/tests/Annotations/ComponentsTest.php +++ b/tests/Annotations/ComponentsTest.php @@ -6,17 +6,15 @@ namespace OpenApi\Tests\Annotations; -use OpenApi\Annotations\Examples; -use OpenApi\Annotations\Components; -use OpenApi\Annotations\Schema; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; class ComponentsTest extends OpenApiTestCase { public function testRef(): void { - $this->assertEquals('#/components/schemas/foo', Components::ref('foo')); - $this->assertEquals('#/components/schemas/bar', Components::ref(new Schema(['ref' => null, 'schema' => 'bar', '_context' => $this->getContext()]))); - $this->assertEquals('#/components/examples/xx', Components::ref(new Examples(['example' => 'xx', '_context' => $this->getContext()]))); + $this->assertEquals('#/components/schemas/foo', OA\Components::ref('foo')); + $this->assertEquals('#/components/schemas/bar', OA\Components::ref(new OA\Schema(['ref' => null, 'schema' => 'bar', '_context' => $this->getContext()]))); + $this->assertEquals('#/components/examples/xx', OA\Components::ref(new OA\Examples(['example' => 'xx', '_context' => $this->getContext()]))); } } diff --git a/tests/Annotations/LicenseTest.php b/tests/Annotations/LicenseTest.php index 63499f99e..91acfea7a 100644 --- a/tests/Annotations/LicenseTest.php +++ b/tests/Annotations/LicenseTest.php @@ -6,14 +6,14 @@ namespace OpenApi\Tests\Annotations; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; class LicenseTest extends OpenApiTestCase { public function testValidation3_0_0(): void { - $annotations = $this->annotationsFromDocBlockParser('@OA\License(name="MIT", identifier="MIT", url="http://localhost")', [], OpenApi::VERSION_3_0_0); + $annotations = $this->annotationsFromDocBlockParser('@OA\License(name="MIT", identifier="MIT", url="http://localhost")', [], OA\OpenApi::VERSION_3_0_0); $annotations[0]->validate(); } @@ -21,7 +21,7 @@ public function testValidation3_1_0(): void { $this->assertOpenApiLogEntryContains('@OA\License() url and identifier are mutually exclusive'); - $annotations = $this->annotationsFromDocBlockParser('@OA\License(name="MIT", identifier="MIT", url="http://localhost")', [], OpenApi::VERSION_3_1_0); + $annotations = $this->annotationsFromDocBlockParser('@OA\License(name="MIT", identifier="MIT", url="http://localhost")', [], OA\OpenApi::VERSION_3_1_0); $annotations[0]->validate(); } } diff --git a/tests/Annotations/OpenApiTest.php b/tests/Annotations/OpenApiTest.php index ddac30d9c..cf7862647 100644 --- a/tests/Annotations/OpenApiTest.php +++ b/tests/Annotations/OpenApiTest.php @@ -6,7 +6,7 @@ namespace OpenApi\Tests\Annotations; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; class OpenApiTest extends OpenApiTestCase @@ -16,7 +16,7 @@ public function testValidVersion(): void $this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); $this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found'); - $openapi = new OpenApi(['_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); $openapi->openapi = '3.0.0'; $openapi->validate(); } @@ -25,7 +25,7 @@ public function testInvalidVersion(): void { $this->assertOpenApiLogEntryContains('Unsupported OpenAPI version "2". Allowed versions are: 3.0.0, 3.1.0'); - $openapi = new OpenApi(['_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); $openapi->openapi = '2'; $openapi->validate(); } diff --git a/tests/Annotations/SecuritySchemesTest.php b/tests/Annotations/SecuritySchemesTest.php index 3f76cb07e..342dc3e56 100644 --- a/tests/Annotations/SecuritySchemesTest.php +++ b/tests/Annotations/SecuritySchemesTest.php @@ -6,9 +6,7 @@ namespace OpenApi\Tests\Annotations; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\SecurityScheme; -use OpenApi\Annotations\Server; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; /** @@ -43,9 +41,9 @@ public function testParseServers(): void $annotations = $this->annotationsFromDocBlockParser($comment); $this->assertCount(3, $annotations); - $this->assertInstanceOf(Info::class, $annotations[0]); - $this->assertInstanceOf(Server::class, $annotations[1]); - $this->assertInstanceOf(Server::class, $annotations[2]); + $this->assertInstanceOf(OA\Info::class, $annotations[0]); + $this->assertInstanceOf(OA\Server::class, $annotations[1]); + $this->assertInstanceOf(OA\Server::class, $annotations[2]); $this->assertEquals('http://example.com', $annotations[1]->url); $this->assertEquals('First host', $annotations[1]->description); @@ -83,7 +81,7 @@ public function testImplicitFlowAnnotation(): void $this->assertCount(1, $annotations); /** @var \OpenApi\Annotations\SecurityScheme $security */ $security = $annotations[0]; - $this->assertInstanceOf(SecurityScheme::class, $security); + $this->assertInstanceOf(OA\SecurityScheme::class, $security); $this->assertCount(1, $security->flows); $this->assertEquals('implicit', $security->flows[0]->flow); diff --git a/tests/Annotations/ValidateRelationsTest.php b/tests/Annotations/ValidateRelationsTest.php index 602d7a4da..afe6418ea 100644 --- a/tests/Annotations/ValidateRelationsTest.php +++ b/tests/Annotations/ValidateRelationsTest.php @@ -6,7 +6,7 @@ namespace OpenApi\Tests\Annotations; -use OpenApi\Annotations\AbstractAnnotation; +use OpenApi\Annotations as OA; use OpenApi\Tests\OpenApiTestCase; /** @@ -38,7 +38,7 @@ public function testAncestors($class): void /** * @dataProvider allAnnotationClasses * - * @param class-string $class + * @param class-string $class */ public function testNested($class): void { diff --git a/tests/ContextTest.php b/tests/ContextTest.php index d0da259f2..4604b8679 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -32,15 +32,15 @@ public function testFullyQualifiedName(): void ->generate([$this->fixture('Customer.php')]); $context = $openapi->components->schemas[0]->_context; // resolve with namespace - $this->assertSame('\FullyQualified', $context->fullyQualifiedName('\FullyQualified')); - $this->assertSame('\OpenApi\Tests\Fixtures\Unqualified', $context->fullyQualifiedName('Unqualified')); - $this->assertSame('\OpenApi\Tests\Fixtures\Namespace\Qualified', $context->fullyQualifiedName('Namespace\Qualified')); + $this->assertSame('\\FullyQualified', $context->fullyQualifiedName('\FullyQualified')); + $this->assertSame('\\OpenApi\\Tests\\Fixtures\\Unqualified', $context->fullyQualifiedName('Unqualified')); + $this->assertSame('\\OpenApi\\Tests\\Fixtures\\Namespace\Qualified', $context->fullyQualifiedName('Namespace\\Qualified')); // respect use statements - $this->assertSame('\Exception', $context->fullyQualifiedName('Exception')); - $this->assertSame('\OpenApi\Tests\Fixtures\Customer', $context->fullyQualifiedName('Customer')); - $this->assertSame('\OpenApi\Generator', $context->fullyQualifiedName('Generator')); - $this->assertSame('\OpenApi\Generator', $context->fullyQualifiedName('gEnerator')); // php has case-insensitive class names :-( - $this->assertSame('\OpenApi\Generator', $context->fullyQualifiedName('OpenApiGenerator')); - $this->assertSame('\OpenApi\Annotations\QualifiedAlias', $context->fullyQualifiedName('OA\QualifiedAlias')); + $this->assertSame('\\Exception', $context->fullyQualifiedName('Exception')); + $this->assertSame('\\OpenApi\\Tests\\Fixtures\\Customer', $context->fullyQualifiedName('Customer')); + $this->assertSame('\\OpenApi\\Generator', $context->fullyQualifiedName('Generator')); + $this->assertSame('\\OpenApi\\Generator', $context->fullyQualifiedName('gEnerator')); // php has case-insensitive class names :-( + $this->assertSame('\\OpenApi\\Generator', $context->fullyQualifiedName('OpenApiGenerator')); + $this->assertSame('\\OpenApi\\Annotations\\QualifiedAlias', $context->fullyQualifiedName('OA\\QualifiedAlias')); } } diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index c474ca9dd..61d01d32a 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -12,7 +12,7 @@ use OpenApi\Analysers\DocBlockAnnotationFactory; use OpenApi\Analysers\ReflectionAnalyser; use OpenApi\Analysers\TokenAnalyser; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Serializer; @@ -21,105 +21,105 @@ class ExamplesTest extends OpenApiTestCase public function exampleDetails(): iterable { yield 'example-object' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'example-object', 'example-object.yaml', [], ]; yield 'misc' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'misc', 'misc.yaml', [], ]; yield 'nesting' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'nesting', 'nesting.yaml', [], ]; yield 'petstore-3.0' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'petstore-3.0', 'petstore-3.0.yaml', [], ]; yield 'petstore.swagger.io' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'petstore.swagger.io', 'petstore.swagger.io.yaml', [], ]; yield 'swagger-spec/petstore' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'swagger-spec/petstore', 'petstore.yaml', [], ]; yield 'swagger-spec/petstore-simple' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'swagger-spec/petstore-simple', 'petstore-simple.yaml', [], ]; yield 'swagger-spec/petstore-simple-3.1.0' => [ - OpenApi::VERSION_3_1_0, + OA\OpenApi::VERSION_3_1_0, 'swagger-spec/petstore-simple', 'petstore-simple-3.1.0.yaml', [], ]; yield 'swagger-spec/petstore-with-external-docs' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'swagger-spec/petstore-with-external-docs', 'petstore-with-external-docs.yaml', [], ]; yield 'polymorphism' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'polymorphism', 'polymorphism.yaml', [], ]; yield 'polymorphism-3.1.0' => [ - OpenApi::VERSION_3_1_0, + OA\OpenApi::VERSION_3_1_0, 'polymorphism', 'polymorphism-3.1.0.yaml', [], ]; yield 'using-interfaces' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'using-interfaces', 'using-interfaces.yaml', [], ]; yield 'using-refs' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'using-refs', 'using-refs.yaml', [], ]; yield 'using-traits' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'using-traits', 'using-traits.yaml', [], ]; yield 'using-links' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'using-links', 'using-links.yaml', [], @@ -127,7 +127,7 @@ public function exampleDetails(): iterable if (\PHP_VERSION_ID >= 80100) { yield 'using-links-php81' => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, 'using-links-php81', 'using-links-php81.yaml', ['JetBrains\PhpStorm\ArrayShape'], diff --git a/tests/Fixtures/Annotations/CustomAttachable.php b/tests/Fixtures/Annotations/CustomAttachable.php index 888d5b434..0681fd610 100644 --- a/tests/Fixtures/Annotations/CustomAttachable.php +++ b/tests/Fixtures/Annotations/CustomAttachable.php @@ -6,14 +6,13 @@ namespace OpenApi\Tests\Fixtures\Annotations; -use OpenApi\Annotations\Attachable; -use OpenApi\Annotations\Operation; +use OpenApi\Annotations as OA; use OpenApi\Generator; /** * @Annotation */ -class CustomAttachable extends Attachable +class CustomAttachable extends OA\Attachable { /** * The attribute value. @@ -29,6 +28,6 @@ class CustomAttachable extends Attachable public function allowedParents(): ?array { - return [Operation::class]; + return [OA\Operation::class]; } } diff --git a/tests/Fixtures/Apis/Attributes/basic.php b/tests/Fixtures/Apis/Attributes/basic.php index 75a7ca528..1c25d88cf 100644 --- a/tests/Fixtures/Apis/Attributes/basic.php +++ b/tests/Fixtures/Apis/Attributes/basic.php @@ -8,7 +8,6 @@ use OpenApi\Attributes as OAT; use OpenApi\Tests\Fixtures\Attributes as OAF; -use phpDocumentor\Reflection\DocBlock\Description; /** * The Spec. diff --git a/tests/Fixtures/Attributes/CustomAttachable.php b/tests/Fixtures/Attributes/CustomAttachable.php index bb78077c5..55f559f72 100644 --- a/tests/Fixtures/Attributes/CustomAttachable.php +++ b/tests/Fixtures/Attributes/CustomAttachable.php @@ -6,12 +6,11 @@ namespace OpenApi\Tests\Fixtures\Attributes; -use OpenApi\Attributes\Attachable; -use OpenApi\Attributes\Get; +use OpenApi\Attributes as OAT; use OpenApi\Generator; #[\Attribute(\Attribute::TARGET_ALL | \Attribute::IS_REPEATABLE)] -class CustomAttachable extends Attachable +class CustomAttachable extends OAT\Attachable { /** * The attribute value. @@ -34,6 +33,6 @@ public function __construct($value = Generator::UNDEFINED) public function allowedParents(): ?array { - return [Get::class]; + return [OAT\Get::class]; } } diff --git a/tests/Fixtures/PHP/AbstractKeyword.php b/tests/Fixtures/PHP/AbstractKeyword.php index 7cd1e980c..fa7ed2b1f 100644 --- a/tests/Fixtures/PHP/AbstractKeyword.php +++ b/tests/Fixtures/PHP/AbstractKeyword.php @@ -6,20 +6,20 @@ namespace OpenApi\Tests\Fixtures\PHP; -use OpenApi\Attributes\Property; +use OpenApi\Attributes as OAT; #[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::IS_REPEATABLE)] abstract class AbstractKeyword { - #[Property(property: 'stuff')] + #[OAT\Property(property: 'stuff')] abstract public function stuff(string $name, array $numbers): bool; - #[Property(property: 'other')] + #[OAT\Property(property: 'other')] public function other(): string { return 'other'; } - #[Property(property: 'another')] + #[OAT\Property(property: 'another')] abstract public function another(): void; } diff --git a/tests/Fixtures/PHP/Enums/ReferencesEnum.php b/tests/Fixtures/PHP/Enums/ReferencesEnum.php deleted file mode 100644 index beb01f470..000000000 --- a/tests/Fixtures/PHP/Enums/ReferencesEnum.php +++ /dev/null @@ -1,108 +0,0 @@ - StatusEnumStringBacked array */ - #[Property( - title: 'statusEnums', - description: 'StatusEnumStringBacked array', - type: 'array', - items: new Items( - title: 'itemsStatusEnumStringBacked', - type: 'string', - enum: StatusEnumStringBacked::class - ) - )] - public array $statusEnums; - - /** @var list StatusEnumStringBacked array */ - #[Property( - title: 'statusEnumsMixed', - description: 'StatusEnumStringBacked array mixed', - type: 'array', - items: new Items( - title: 'itemsStatusEnumStringBackedMixed', - type: 'string', - enum: [StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other'] - ) - )] - public array $statusEnumsMixed; -} diff --git a/tests/Fixtures/PHP/Enums/StatusEnum.php b/tests/Fixtures/PHP/Enums/StatusEnum.php index ca808bb6f..b196d3b9c 100644 --- a/tests/Fixtures/PHP/Enums/StatusEnum.php +++ b/tests/Fixtures/PHP/Enums/StatusEnum.php @@ -6,9 +6,9 @@ namespace OpenApi\Tests\Fixtures\PHP\Enums; -use OpenApi\Attributes\Schema; +use OpenApi\Attributes as OAT; -#[Schema()] +#[OAT\Schema()] enum StatusEnum { case DRAFT; diff --git a/tests/Fixtures/PHP/Enums/StatusEnumBacked.php b/tests/Fixtures/PHP/Enums/StatusEnumBacked.php index d52c3811c..ce9fef392 100644 --- a/tests/Fixtures/PHP/Enums/StatusEnumBacked.php +++ b/tests/Fixtures/PHP/Enums/StatusEnumBacked.php @@ -6,9 +6,9 @@ namespace OpenApi\Tests\Fixtures\PHP\Enums; -use OpenApi\Attributes\Schema; +use OpenApi\Attributes as OAT; -#[Schema()] +#[OAT\Schema()] enum StatusEnumBacked: int { case DRAFT = 1; diff --git a/tests/Fixtures/PHP/Enums/StatusEnumIntegerBacked.php b/tests/Fixtures/PHP/Enums/StatusEnumIntegerBacked.php index 456501684..75ce4f3d2 100644 --- a/tests/Fixtures/PHP/Enums/StatusEnumIntegerBacked.php +++ b/tests/Fixtures/PHP/Enums/StatusEnumIntegerBacked.php @@ -6,9 +6,9 @@ namespace OpenApi\Tests\Fixtures\PHP\Enums; -use OpenApi\Attributes\Schema; +use OpenApi\Attributes as OAT; -#[Schema(type: 'integer')] +#[OAT\Schema(type: 'integer')] enum StatusEnumIntegerBacked: int { case DRAFT = 1; diff --git a/tests/Fixtures/PHP/Enums/StatusEnumStringBacked.php b/tests/Fixtures/PHP/Enums/StatusEnumStringBacked.php index 6138af58d..4d24c92fe 100644 --- a/tests/Fixtures/PHP/Enums/StatusEnumStringBacked.php +++ b/tests/Fixtures/PHP/Enums/StatusEnumStringBacked.php @@ -6,9 +6,9 @@ namespace OpenApi\Tests\Fixtures\PHP\Enums; -use OpenApi\Attributes\Schema; +use OpenApi\Attributes as OAT; -#[Schema()] +#[OAT\Schema(type: 'string')] enum StatusEnumStringBacked: string { case DRAFT = 'draft'; diff --git a/tests/Fixtures/PHP/Php8PromotedProperties.php b/tests/Fixtures/PHP/Php8PromotedProperties.php index 2b00854f3..1cfeeb22e 100644 --- a/tests/Fixtures/PHP/Php8PromotedProperties.php +++ b/tests/Fixtures/PHP/Php8PromotedProperties.php @@ -6,7 +6,7 @@ namespace OpenApi\Tests\Fixtures\PHP; -use OpenApi\Attributes\Property; +use OpenApi\Attributes as OAT; use OpenApi\Annotations as OA; /** @@ -23,7 +23,7 @@ public function __construct( * @OA\Property() */ public ?array $labels, - #[Property()] + #[OAT\Property()] public int $id, ) { } diff --git a/tests/Fixtures/PHP/ReferencesEnum.php b/tests/Fixtures/PHP/ReferencesEnum.php new file mode 100644 index 000000000..dc8102335 --- /dev/null +++ b/tests/Fixtures/PHP/ReferencesEnum.php @@ -0,0 +1,71 @@ + StatusEnumStringBacked array */ + #[OAT\Property( + title: 'statusEnums', + description: 'StatusEnumStringBacked array', + type: 'array', + items: new OAT\Items(title: 'itemsStatusEnumStringBacked', type: 'string', enum: StatusEnumStringBacked::class) + )] + public array $statusEnums; + + /** @var list StatusEnumStringBacked array */ + #[OAT\Property( + title: 'statusEnumsMixed', + description: 'StatusEnumStringBacked array mixed', + type: 'array', + items: new OAT\Items(title: 'itemsStatusEnumStringBackedMixed', type: 'string', enum: [StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']) + )] + public array $statusEnumsMixed; +} diff --git a/tests/OpenApiTestCase.php b/tests/OpenApiTestCase.php index 44be1201b..682f5c3eb 100644 --- a/tests/OpenApiTestCase.php +++ b/tests/OpenApiTestCase.php @@ -12,9 +12,7 @@ use OpenApi\Analysers\DocBlockParser; use OpenApi\Analysers\ReflectionAnalyser; use OpenApi\Analysis; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\OpenApi; -use OpenApi\Annotations\PathItem; +use OpenApi\Annotations as OA; use OpenApi\Context; use OpenApi\Analysers\TokenAnalyser; use OpenApi\Generator; @@ -76,7 +74,7 @@ public function log($level, $message, array $context = []): void }; } - public function getContext(array $properties = [], ?string $version = OpenApi::DEFAULT_VERSION): Context + public function getContext(array $properties = [], ?string $version = OA\OpenApi::DEFAULT_VERSION): Context { return new Context( [ @@ -108,11 +106,11 @@ public function assertOpenApiLogEntryContains(string $needle, string $message = /** * Compare OpenApi specs assuming strings to contain YAML. * - * @param array|OpenApi|\stdClass|string $actual The generated output - * @param array|OpenApi|\stdClass|string $expected The specification - * @param string $message - * @param bool $normalized flag indicating whether the inputs are already normalized or - * not + * @param array|OA\OpenApi|\stdClass|string $actual The generated output + * @param array|OA\OpenApi|\stdClass|string $expected The specification + * @param string $message + * @param bool $normalized flag indicating whether the inputs are already normalized or + * not */ protected function assertSpecEquals($actual, $expected, string $message = '', bool $normalized = false): void { @@ -134,7 +132,7 @@ protected function assertSpecEquals($actual, $expected, string $message = '', bo }; $normalizeIn = function ($in) { - if ($in instanceof OpenApi) { + if ($in instanceof OA\OpenApi) { $in = $in->toYaml(); } @@ -172,16 +170,16 @@ protected function assertSpecEquals($actual, $expected, string $message = '', bo /** * Create a valid OpenApi object with Info. */ - protected function createOpenApiWithInfo(): OpenApi + protected function createOpenApiWithInfo(): OA\OpenApi { - return new OpenApi([ - 'info' => new Info([ + return new OA\OpenApi([ + 'info' => new OA\Info([ 'title' => 'swagger-php Test-API', 'version' => 'test', '_context' => $this->getContext(), ]), 'paths' => [ - new PathItem(['path' => '/test', '_context' => $this->getContext()]), + new OA\PathItem(['path' => '/test', '_context' => $this->getContext()]), ], '_context' => $this->getContext(), ]); @@ -234,7 +232,7 @@ public function analysisFromFixtures(array $files, array $processors = [], ?Anal return $analysis; } - protected function annotationsFromDocBlockParser(string $docBlock, array $extraAliases = [], string $version = OpenApi::DEFAULT_VERSION): array + protected function annotationsFromDocBlockParser(string $docBlock, array $extraAliases = [], string $version = OA\OpenApi::DEFAULT_VERSION): array { return (new Generator()) ->setVersion($version) diff --git a/tests/Processors/AugmentPropertiesTest.php b/tests/Processors/AugmentPropertiesTest.php index 1a260085b..30927ff02 100644 --- a/tests/Processors/AugmentPropertiesTest.php +++ b/tests/Processors/AugmentPropertiesTest.php @@ -7,7 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysers\ReflectionAnalyser; -use OpenApi\Annotations\Property; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\AugmentProperties; use OpenApi\Processors\AugmentSchemas; @@ -334,7 +334,7 @@ public function testTypedProperties(): void ); } - protected function assertName(Property $property, array $expectedValues): void + protected function assertName(OA\Property $property, array $expectedValues): void { foreach ($expectedValues as $key => $val) { $this->assertSame($val, $property->$key, '@OA\Property()->property based on propertyname'); diff --git a/tests/Processors/BuildPathsTest.php b/tests/Processors/BuildPathsTest.php index cdadb87ac..34cb5bf6d 100644 --- a/tests/Processors/BuildPathsTest.php +++ b/tests/Processors/BuildPathsTest.php @@ -7,10 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Get; -use OpenApi\Annotations\OpenApi; -use OpenApi\Annotations\PathItem; -use OpenApi\Annotations\Post; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\BuildPaths; use OpenApi\Processors\MergeIntoOpenApi; @@ -20,10 +17,10 @@ class BuildPathsTest extends OpenApiTestCase { public function testMergePathsWithSamePath(): void { - $openapi = new OpenApi(['_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); $openapi->paths = [ - new PathItem(['path' => '/comments', '_context' => $this->getContext()]), - new PathItem(['path' => '/comments', '_context' => $this->getContext()]), + new OA\PathItem(['path' => '/comments', '_context' => $this->getContext()]), + new OA\PathItem(['path' => '/comments', '_context' => $this->getContext()]), ]; $analysis = new Analysis([$openapi], $this->getContext()); $analysis->openapi = $openapi; @@ -35,12 +32,12 @@ public function testMergePathsWithSamePath(): void public function testMergeOperationsWithSamePath(): void { - $openapi = new OpenApi(['_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); $analysis = new Analysis( [ $openapi, - new Get(['path' => '/comments', '_context' => $this->getContext()]), - new Post(['path' => '/comments', '_context' => $this->getContext()]), + new OA\Get(['path' => '/comments', '_context' => $this->getContext()]), + new OA\Post(['path' => '/comments', '_context' => $this->getContext()]), ], $this->getContext() ); @@ -51,9 +48,9 @@ public function testMergeOperationsWithSamePath(): void $this->assertCount(1, $openapi->paths); $path = $openapi->paths[0]; $this->assertSame('/comments', $path->path); - $this->assertInstanceOf(PathItem::class, $path); - $this->assertInstanceOf(Get::class, $path->get); - $this->assertInstanceOf(Post::class, $path->post); + $this->assertInstanceOf(OA\PathItem::class, $path); + $this->assertInstanceOf(OA\Get::class, $path->get); + $this->assertInstanceOf(OA\Post::class, $path->post); $this->assertSame(Generator::UNDEFINED, $path->put); } } diff --git a/tests/Processors/CleanUnmergedTest.php b/tests/Processors/CleanUnmergedTest.php index bca351205..21dec86f1 100644 --- a/tests/Processors/CleanUnmergedTest.php +++ b/tests/Processors/CleanUnmergedTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Contact; -use OpenApi\Annotations\License; +use OpenApi\Annotations as OA; use OpenApi\Processors\CleanUnmerged; use OpenApi\Processors\MergeIntoOpenApi; use OpenApi\Tests\OpenApiTestCase; @@ -54,9 +53,9 @@ public function testCleanUnmergedProcessor(): void $analysis->validate(); // When a processor places a previously unmerged annotation into the swagger obect. - $license = $analysis->getAnnotationsOfType(License::class)[0]; - /** @var Contact $contact */ - $contact = $analysis->getAnnotationsOfType(Contact::class)[0]; + $license = $analysis->getAnnotationsOfType(OA\License::class)[0]; + /** @var OA\Contact $contact */ + $contact = $analysis->getAnnotationsOfType(OA\Contact::class)[0]; $analysis->openapi->info->contact = $contact; $this->assertCount(1, $license->_unmerged); $analysis->process([new CleanUnmerged()]); diff --git a/tests/Processors/DocBlockDescriptionsTest.php b/tests/Processors/DocBlockDescriptionsTest.php index 8e7a9ffd8..94b9d9583 100644 --- a/tests/Processors/DocBlockDescriptionsTest.php +++ b/tests/Processors/DocBlockDescriptionsTest.php @@ -6,7 +6,7 @@ namespace OpenApi\Tests\Processors; -use OpenApi\Annotations\Operation; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\DocBlockDescriptions; use OpenApi\Tests\OpenApiTestCase; @@ -21,8 +21,8 @@ public function testDocBlockDescription(): void $analysis->process([ new DocBlockDescriptions(), ]); - /** @var Operation[] $operations */ - $operations = $analysis->getAnnotationsOfType(Operation::class); + /** @var OA\Operation[] $operations */ + $operations = $analysis->getAnnotationsOfType(OA\Operation::class); $this->assertSame('api/test1', $operations[0]->path); $this->assertSame('Example summary.', $operations[0]->summary, 'Operation summary should be taken from phpDoc'); diff --git a/tests/Processors/ExpandClassesTest.php b/tests/Processors/ExpandClassesTest.php index 028999c00..603808e5a 100644 --- a/tests/Processors/ExpandClassesTest.php +++ b/tests/Processors/ExpandClassesTest.php @@ -7,10 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Components; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\PathItem; -use OpenApi\Annotations\Schema; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\AugmentProperties; use OpenApi\Processors\AugmentSchemas; @@ -28,8 +25,8 @@ class ExpandClassesTest extends OpenApiTestCase { protected function validate(Analysis $analysis): void { - $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0', '_context' => $this->getContext()]); - $analysis->openapi->paths = [new PathItem(['path' => '/test', '_context' => $this->getContext()])]; + $analysis->openapi->info = new OA\Info(['title' => 'test', 'version' => '1.0.0', '_context' => $this->getContext()]); + $analysis->openapi->paths = [new OA\PathItem(['path' => '/test', '_context' => $this->getContext()])]; $analysis->validate(); } @@ -53,8 +50,8 @@ public function testExpandClasses(): void ]); $this->validate($analysis); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class); $this->assertCount(4, $schemas); $childSchema = $schemas[0]; $this->assertSame('Child', $childSchema->schema); @@ -84,8 +81,8 @@ public function testExpandClassesWithoutDocBlocks(): void $analysis->process($this->processors([CleanUnusedComponents::class])); $this->validate($analysis); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class); $this->assertCount(2, $schemas); $childSchema = $schemas[0]; $this->assertSame('ChildWithDocBlocks', $childSchema->schema); @@ -109,8 +106,8 @@ public function testExpandClassesWithAllOf(): void $analysis->process($this->processors([CleanUnusedComponents::class])); $this->validate($analysis); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(5, $schemas); $extendedSchema = $schemas[0]; @@ -137,8 +134,8 @@ public function testExpandClassesWithOutAllOf(): void $analysis->process($this->processors([CleanUnusedComponents::class])); $this->validate($analysis); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(4, $schemas); $extendedSchema = $schemas[0]; @@ -147,7 +144,7 @@ public function testExpandClassesWithOutAllOf(): void $this->assertCount(2, $extendedSchema->allOf); - $this->assertEquals(Components::ref('Base'), $extendedSchema->allOf[0]->ref); + $this->assertEquals(OA\Components::ref('Base'), $extendedSchema->allOf[0]->ref); $this->assertEquals('extendedProperty', $extendedSchema->allOf[1]->properties[0]->property); } @@ -164,8 +161,8 @@ public function testExpandClassesWithTwoChildSchemas(): void $analysis->process($this->processors([CleanUnusedComponents::class])); $this->validate($analysis); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(7, $schemas); $extendedSchema = $schemas[0]; @@ -173,7 +170,7 @@ public function testExpandClassesWithTwoChildSchemas(): void $this->assertSame(Generator::UNDEFINED, $extendedSchema->properties); $this->assertCount(2, $extendedSchema->allOf); - $this->assertEquals(Components::ref('Base'), $extendedSchema->allOf[0]->ref); + $this->assertEquals(OA\Components::ref('Base'), $extendedSchema->allOf[0]->ref); $this->assertEquals('nested', $extendedSchema->allOf[1]->properties[0]->property); $this->assertEquals('extendedProperty', $extendedSchema->allOf[1]->properties[1]->property); @@ -197,12 +194,12 @@ public function testPreserveExistingAllOf(): void $analysis->process($this->processors([CleanUnusedComponents::class])); $this->validate($analysis); - $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0', '_context' => $this->getContext()]); - $analysis->openapi->paths = [new PathItem(['path' => '/test', '_context' => $this->getContext()])]; + $analysis->openapi->info = new OA\Info(['title' => 'test', 'version' => '1.0.0', '_context' => $this->getContext()]); + $analysis->openapi->paths = [new OA\PathItem(['path' => '/test', '_context' => $this->getContext()])]; $analysis->validate(); - /** @var Schema[] $schemas */ - $schemas = $analysis->getAnnotationsOfType(Schema::class, true); + /** @var OA\Schema[] $schemas */ + $schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true); $this->assertCount(9, $schemas); $baseInterface = $schemas[0]; diff --git a/tests/Processors/ExpandEnumsTest.php b/tests/Processors/ExpandEnumsTest.php index 98ddc0626..f5dc1c76d 100644 --- a/tests/Processors/ExpandEnumsTest.php +++ b/tests/Processors/ExpandEnumsTest.php @@ -7,9 +7,8 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysers\TokenAnalyser; -use OpenApi\Annotations\Items; -use OpenApi\Annotations\Property as AnnotationsProperty; -use OpenApi\Attributes\Property as AttributesProperty; +use OpenApi\Annotations as OA; +use OpenApi\Attributes as OAT; use OpenApi\Generator; use OpenApi\Processors\ExpandEnums; use OpenApi\Tests\Fixtures\PHP\Enums\StatusEnum; @@ -35,7 +34,8 @@ public function testExpandUnitEnum(): void $analysis->process([new ExpandEnums()]); $schema = $analysis->getSchemaForSource(StatusEnum::class); - self::assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum); + $this->assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum); + $this->assertEquals('string', $schema->type); } public function testExpandBackedEnum(): void @@ -44,7 +44,8 @@ public function testExpandBackedEnum(): void $analysis->process([new ExpandEnums()]); $schema = $analysis->getSchemaForSource(StatusEnumBacked::class); - self::assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum); + $this->assertEquals(['DRAFT', 'PUBLISHED', 'ARCHIVED'], $schema->enum); + $this->assertEquals('string', $schema->type); } public function testExpandBackedIntegerEnum(): void @@ -53,7 +54,8 @@ public function testExpandBackedIntegerEnum(): void $analysis->process([new ExpandEnums()]); $schema = $analysis->getSchemaForSource(StatusEnumIntegerBacked::class); - self::assertEquals([1, 2, 3], $schema->enum); + $this->assertEquals([1, 2, 3], $schema->enum); + $this->assertEquals('integer', $schema->type); } public function testExpandBackedStringEnum(): void @@ -62,7 +64,8 @@ public function testExpandBackedStringEnum(): void $analysis->process([new ExpandEnums()]); $schema = $analysis->getSchemaForSource(StatusEnumStringBacked::class); - self::assertEquals(['draft', 'published', 'archived'], $schema->enum); + $this->assertEquals(['draft', 'published', 'archived'], $schema->enum); + $this->assertEquals('string', $schema->type); } public function expandEnumClassStringFixtures(): iterable @@ -80,57 +83,57 @@ public function expandEnumClassStringFixtures(): iterable return [ 'statusEnum' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnum', $mapValues(StatusEnum::cases()), ], 'statusEnumMixed' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumMixed', $mapValues([StatusEnum::DRAFT, StatusEnum::ARCHIVED, 'OTHER']), ], 'statusEnumBacked' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumBacked', $mapValues(StatusEnumBacked::cases()), ], 'statusEnumBackedMixed' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumBackedMixed', $mapValues([StatusEnumBacked::DRAFT, StatusEnumBacked::ARCHIVED, 9]), ], 'statusEnumIntegerBacked' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumIntegerBacked', $mapValues(StatusEnumIntegerBacked::cases()), ], 'statusEnumStringBacked' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumStringBacked', $mapValues(StatusEnumStringBacked::cases()), ], 'statusEnumStringBackedMixed' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumStringBackedMixed', $mapValues([StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']), ], 'statusEnums' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnums', Generator::UNDEFINED, ], 'itemsStatusEnumStringBacked' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'itemsStatusEnumStringBacked', $mapValues(StatusEnumStringBacked::cases()), ], 'statusEnumsMixed' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'statusEnumsMixed', Generator::UNDEFINED, ], 'itemsStatusEnumStringBackedMixed' => [ - ['PHP/Enums/ReferencesEnum.php'], + ['PHP/ReferencesEnum.php'], 'itemsStatusEnumStringBackedMixed', $mapValues([StatusEnumStringBacked::DRAFT, StatusEnumStringBacked::ARCHIVED, 'other']), ], @@ -146,12 +149,12 @@ public function testExpandEnumClassString(array $files, string $title, mixed $ex { $analysis = $this->analysisFromFixtures($files); $analysis->process([new ExpandEnums()]); - $schemas = $analysis->getAnnotationsOfType([AnnotationsProperty::class, AttributesProperty::class, Items::class], true); + $schemas = $analysis->getAnnotationsOfType([OA\Property::class, OAT\Property::class, OA\Items::class], true); foreach ($schemas as $schema) { - if ($schema instanceof AnnotationsProperty || $schema instanceof Items) { + if ($schema instanceof OA\Property || $schema instanceof OA\Items) { if ($schema->title == $title) { - self::assertEquals($expected, $schema->enum); + $this->assertEquals($expected, $schema->enum); } } } diff --git a/tests/Processors/MergeIntoComponentsTest.php b/tests/Processors/MergeIntoComponentsTest.php index 25a2a8b21..4d7292358 100644 --- a/tests/Processors/MergeIntoComponentsTest.php +++ b/tests/Processors/MergeIntoComponentsTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\OpenApi; -use OpenApi\Annotations\Response; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\MergeIntoComponents; use OpenApi\Tests\OpenApiTestCase; @@ -17,8 +16,8 @@ class MergeIntoComponentsTest extends OpenApiTestCase { public function testProcessor(): void { - $openapi = new OpenApi(['_context' => $this->getContext()]); - $response = new Response(['response' => '2xx', '_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); + $response = new OA\Response(['response' => '2xx', '_context' => $this->getContext()]); $analysis = new Analysis( [ $openapi, diff --git a/tests/Processors/MergeIntoOpenApiTest.php b/tests/Processors/MergeIntoOpenApiTest.php index d35b62fd6..49e8975f5 100644 --- a/tests/Processors/MergeIntoOpenApiTest.php +++ b/tests/Processors/MergeIntoOpenApiTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\MergeIntoOpenApi; use OpenApi\Tests\OpenApiTestCase; @@ -17,8 +16,8 @@ class MergeIntoOpenApiTest extends OpenApiTestCase { public function testProcessor(): void { - $openapi = new OpenApi(['_context' => $this->getContext()]); - $info = new Info(['_context' => $this->getContext()]); + $openapi = new OA\OpenApi(['_context' => $this->getContext()]); + $info = new OA\Info(['_context' => $this->getContext()]); $analysis = new Analysis( [ $openapi, diff --git a/tests/Processors/MergeJsonContentTest.php b/tests/Processors/MergeJsonContentTest.php index f8d43167d..31a524552 100644 --- a/tests/Processors/MergeJsonContentTest.php +++ b/tests/Processors/MergeJsonContentTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Parameter; -use OpenApi\Annotations\Response; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\MergeJsonContent; use OpenApi\Tests\OpenApiTestCase; @@ -26,8 +25,8 @@ public function testJsonContent(): void END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); $this->assertCount(3, $analysis->annotations); - /** @var Response $response */ - $response = $analysis->getAnnotationsOfType(Response::class)[0]; + /** @var OA\Response $response */ + $response = $analysis->getAnnotationsOfType(OA\Response::class)[0]; $this->assertSame(Generator::UNDEFINED, $response->content); $this->assertCount(1, $response->_unmerged); $analysis->process([new MergeJsonContent()]); @@ -50,8 +49,8 @@ public function testMultipleMediaTypes(): void ) END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); - /** @var Response $response */ - $response = $analysis->getAnnotationsOfType(Response::class)[0]; + /** @var OA\Response $response */ + $response = $analysis->getAnnotationsOfType(OA\Response::class)[0]; $this->assertCount(1, $response->content); $analysis->process([new MergeJsonContent()]); @@ -68,8 +67,8 @@ public function testParameter(): void END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); $this->assertCount(4, $analysis->annotations); - /** @var Parameter $parameter */ - $parameter = $analysis->getAnnotationsOfType(Parameter::class)[0]; + /** @var OA\Parameter $parameter */ + $parameter = $analysis->getAnnotationsOfType(OA\Parameter::class)[0]; $this->assertSame(Generator::UNDEFINED, $parameter->content); $this->assertIsArray($parameter->_unmerged); $this->assertCount(1, $parameter->_unmerged); diff --git a/tests/Processors/MergeXmlContentTest.php b/tests/Processors/MergeXmlContentTest.php index 76bfc3ff0..c0f2286c7 100644 --- a/tests/Processors/MergeXmlContentTest.php +++ b/tests/Processors/MergeXmlContentTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests\Processors; use OpenApi\Analysis; -use OpenApi\Annotations\Parameter; -use OpenApi\Annotations\Response; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Processors\MergeXmlContent; use OpenApi\Tests\OpenApiTestCase; @@ -26,8 +25,8 @@ public function testXmlContent(): void END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); $this->assertCount(3, $analysis->annotations); - /** @var Response $response */ - $response = $analysis->getAnnotationsOfType(Response::class)[0]; + /** @var OA\Response $response */ + $response = $analysis->getAnnotationsOfType(OA\Response::class)[0]; $this->assertSame(Generator::UNDEFINED, $response->content); $this->assertCount(1, $response->_unmerged); $analysis->process([new MergeXmlContent()]); @@ -50,8 +49,8 @@ public function testMultipleMediaTypes(): void ) END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); - /** @var Response $response */ - $response = $analysis->getAnnotationsOfType(Response::class)[0]; + /** @var OA\Response $response */ + $response = $analysis->getAnnotationsOfType(OA\Response::class)[0]; $this->assertCount(1, $response->content); $analysis->process([new MergeXmlContent()]); $this->assertCount(2, $response->content); @@ -67,8 +66,8 @@ public function testParameter(): void END; $analysis = new Analysis($this->annotationsFromDocBlockParser($comment), $this->getContext()); $this->assertCount(4, $analysis->annotations); - /** @var Parameter $parameter */ - $parameter = $analysis->getAnnotationsOfType(Parameter::class)[0]; + /** @var OA\Parameter $parameter */ + $parameter = $analysis->getAnnotationsOfType(OA\Parameter::class)[0]; $this->assertSame(Generator::UNDEFINED, $parameter->content); $this->assertCount(1, $parameter->_unmerged); $analysis->process([new MergeXmlContent()]); diff --git a/tests/Processors/OperationIdTest.php b/tests/Processors/OperationIdTest.php index e714fd304..615503e34 100644 --- a/tests/Processors/OperationIdTest.php +++ b/tests/Processors/OperationIdTest.php @@ -6,10 +6,7 @@ namespace OpenApi\Tests\Processors; -use OpenApi\Annotations\Delete; -use OpenApi\Annotations\Get; -use OpenApi\Annotations\Operation; -use OpenApi\Annotations\Post; +use OpenApi\Annotations as OA; use OpenApi\Processors\OperationId; use OpenApi\Tests\OpenApiTestCase; @@ -23,21 +20,21 @@ public function testGeneratedOperationId(): void 'Processors/EntityControllerTrait.php', ]); $analysis->process([new OperationId(false)]); - /** @var Operation[] $operations */ - $operations = $analysis->getAnnotationsOfType(Operation::class); + /** @var OA\Operation[] $operations */ + $operations = $analysis->getAnnotationsOfType(OA\Operation::class); $this->assertCount(3, $operations); $this->assertSame('entity/{id}', $operations[0]->path); - $this->assertInstanceOf(Get::class, $operations[0]); + $this->assertInstanceOf(OA\Get::class, $operations[0]); $this->assertSame('GET::entity/{id}::OpenApi\Tests\Fixtures\Processors\EntityControllerClass::getEntry', $operations[0]->operationId); $this->assertSame('entity/{id}', $operations[1]->path); - $this->assertInstanceOf(Post::class, $operations[1]); + $this->assertInstanceOf(OA\Post::class, $operations[1]); $this->assertSame('POST::entity/{id}::OpenApi\Tests\Fixtures\Processors\EntityControllerInterface::updateEntity', $operations[1]->operationId); $this->assertSame('entities/{id}', $operations[2]->path); - $this->assertInstanceOf(Delete::class, $operations[2]); + $this->assertInstanceOf(OA\Delete::class, $operations[2]); $this->assertSame('DELETE::entities/{id}::OpenApi\Tests\Fixtures\Processors\EntityControllerTrait::deleteEntity', $operations[2]->operationId); } } diff --git a/tests/RefTest.php b/tests/RefTest.php index 30949843c..d860cfdbd 100644 --- a/tests/RefTest.php +++ b/tests/RefTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests; use OpenApi\Analysis; -use OpenApi\Annotations\Info; -use OpenApi\Annotations\Response; +use OpenApi\Annotations as OA; use OpenApi\Generator; class RefTest extends OpenApiTestCase @@ -17,7 +16,7 @@ public function testRef(): void { $openapi = $this->createOpenApiWithInfo(); $info = $openapi->ref('#/info'); - $this->assertInstanceOf(Info::class, $info); + $this->assertInstanceOf(OA\Info::class, $info); $comment = <<ref('#/paths/~1api~1~0~1endpoint/get/responses/default'); - $this->assertInstanceOf(Response::class, $response); + $this->assertInstanceOf(OA\Response::class, $response); $this->assertSame('A response', $response->description); } } diff --git a/tests/ScratchTest.php b/tests/ScratchTest.php index 24262e703..5d40ed39a 100644 --- a/tests/ScratchTest.php +++ b/tests/ScratchTest.php @@ -6,7 +6,7 @@ namespace OpenApi\Tests; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Generator; class ScratchTest extends OpenApiTestCase @@ -21,7 +21,7 @@ public function scratchTests(): iterable } yield $name => [ - OpenApi::VERSION_3_0_0, + OA\OpenApi::VERSION_3_0_0, $this->fixture("Scratch/$name.php"), $this->fixture("Scratch/$name.yaml"), [], diff --git a/tests/SerializerTest.php b/tests/SerializerTest.php index d8918dfa3..aeb56644e 100644 --- a/tests/SerializerTest.php +++ b/tests/SerializerTest.php @@ -6,25 +6,24 @@ namespace OpenApi\Tests; -use OpenApi\Annotations; -use OpenApi\Annotations\OpenApi; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Serializer; class SerializerTest extends OpenApiTestCase { - private function getExpected(): OpenApi + private function getExpected(): OA\OpenApi { - $path = new Annotations\PathItem(['_context' => $this->getContext()]); + $path = new OA\PathItem(['_context' => $this->getContext()]); $path->path = '/products'; - $path->post = new Annotations\Post(['_context' => $this->getContext()]); + $path->post = new OA\Post(['_context' => $this->getContext()]); $path->post->tags = ['products']; $path->post->summary = 's1'; $path->post->description = 'd1'; - $path->post->requestBody = new Annotations\RequestBody(['_context' => $this->getContext()]); - $mediaType = new Annotations\MediaType(['_context' => $this->getContext()]); + $path->post->requestBody = new OA\RequestBody(['_context' => $this->getContext()]); + $mediaType = new OA\MediaType(['_context' => $this->getContext()]); $mediaType->mediaType = 'application/json'; - $mediaType->schema = new Annotations\Schema(['_context' => $this->getContext()]); + $mediaType->schema = new OA\Schema(['_context' => $this->getContext()]); $mediaType->schema->type = 'object'; $mediaType->schema->additionalProperties = true; $path->post->requestBody->content = [$mediaType]; @@ -32,39 +31,39 @@ private function getExpected(): OpenApi $path->post->requestBody->x = []; $path->post->requestBody->x['repository'] = 'def'; - $resp = new Annotations\Response(['_context' => $this->getContext()]); + $resp = new OA\Response(['_context' => $this->getContext()]); $resp->response = '200'; $resp->description = 'Success'; - $content = new Annotations\MediaType(['_context' => $this->getContext()]); + $content = new OA\MediaType(['_context' => $this->getContext()]); $content->mediaType = 'application/json'; - $content->schema = new Annotations\Schema(['_context' => $this->getContext()]); + $content->schema = new OA\Schema(['_context' => $this->getContext()]); $content->schema->ref = '#/components/schemas/Pet'; $resp->content = [$content]; $resp->x = []; $resp->x['repository'] = 'def'; - $respRange = new Annotations\Response(['_context' => $this->getContext()]); + $respRange = new OA\Response(['_context' => $this->getContext()]); $respRange->response = '4XX'; $respRange->description = 'Client error response'; $path->post->responses = [$resp, $respRange]; - $expected = new Annotations\OpenApi(['_context' => $this->getContext()]); + $expected = new OA\OpenApi(['_context' => $this->getContext()]); $expected->openapi = '3.0.0'; $expected->paths = [ $path, ]; - $info = new Annotations\Info(['_context' => $this->getContext()]); + $info = new OA\Info(['_context' => $this->getContext()]); $info->title = 'Pet store'; $info->version = '1.0'; $expected->info = $info; - $schema = new Annotations\Schema(['_context' => $this->getContext()]); + $schema = new OA\Schema(['_context' => $this->getContext()]); $schema->schema = 'Pet'; $schema->required = ['name', 'photoUrls']; - $expected->components = new Annotations\Components(['_context' => $this->getContext()]); + $expected->components = new OA\Components(['_context' => $this->getContext()]); $expected->components->schemas = [$schema]; return $expected; @@ -133,10 +132,10 @@ public function testDeserializeAnnotation(): void } JSON; - /** @var Annotations\OpenApi $annotation */ - $annotation = $serializer->deserialize($json, 'OpenApi\Annotations\OpenApi'); + /** @var OA\OpenApi $annotation */ + $annotation = $serializer->deserialize($json, 'OpenApi\\Annotations\\OpenApi'); - $this->assertInstanceOf('OpenApi\Annotations\OpenApi', $annotation); + $this->assertInstanceOf('OpenApi\\Annotations\\OpenApi', $annotation); $this->assertJsonStringEqualsJsonString( $annotation->toJson(), $this->getExpected()->toJson() @@ -151,7 +150,7 @@ public function testPetstoreExample(): void $serializer = new Serializer(); $spec = $this->example('petstore.swagger.io/petstore.swagger.io.json'); $openapi = $serializer->deserializeFile($spec); - $this->assertInstanceOf(OpenApi::class, $openapi); + $this->assertInstanceOf(OA\OpenApi::class, $openapi); $this->assertJsonStringEqualsJsonString(file_get_contents($spec), $openapi->toJson()); } @@ -186,8 +185,8 @@ public function testDeserializeAllOfProperty(): void } } JSON; - /** @var Annotations\OpenApi $annotation */ - $annotation = $serializer->deserialize($json, Annotations\OpenApi::class); + /** @var OA\OpenApi $annotation */ + $annotation = $serializer->deserialize($json, OA\OpenApi::class); foreach ($annotation->components->schemas as $schemaObject) { $this->assertObjectHasAttribute('allOf', $schemaObject); @@ -195,7 +194,7 @@ public function testDeserializeAllOfProperty(): void $this->assertIsArray($schemaObject->allOf); $allOfItem = current($schemaObject->allOf); $this->assertIsObject($allOfItem); - $this->assertInstanceOf(Annotations\Schema::class, $allOfItem); + $this->assertInstanceOf(OA\Schema::class, $allOfItem); $this->assertObjectHasAttribute('ref', $allOfItem); $this->assertNotSame($allOfItem->ref, Generator::UNDEFINED); $this->assertSame('#/components/schemas/SomeSchema', $allOfItem->ref); diff --git a/tests/UtilTest.php b/tests/UtilTest.php index 152ab35de..15a5402e2 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -7,8 +7,7 @@ namespace OpenApi\Tests; use OpenApi\Analysers\TokenAnalyser; -use OpenApi\Annotations\Get; -use OpenApi\Annotations\Post; +use OpenApi\Annotations as OA; use OpenApi\Generator; use OpenApi\Util; use Symfony\Component\Finder\Finder; @@ -69,8 +68,8 @@ public function testFinder(): void public function shortenFixtures(): iterable { return [ - [[Get::class], ['@OA\Get']], - [[Get::class, Post::class], ['@OA\Get', '@OA\Post']], + [[OA\Get::class], ['@OA\Get']], + [[OA\Get::class, OA\Post::class], ['@OA\Get', '@OA\Post']], ]; }