Skip to content

Commit 9172fa8

Browse files
committed
refactor
1 parent bfe62cd commit 9172fa8

File tree

1 file changed

+44
-37
lines changed

1 file changed

+44
-37
lines changed

tests/Functional/OpenApiTest.php

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,19 @@ public function testOpenApiPolymorphicBookSchema(): void
668668
$this->assertArrayHasKey('Book', $schemas, 'Book parent schema should exist');
669669
$bookSchema = $schemas['Book'];
670670

671+
$this->assertArrayHasKey('type', $bookSchema);
672+
$this->assertArrayHasKey('properties', $bookSchema);
673+
674+
$bookSchemaProperties = $bookSchema['properties'];
675+
$this->assertIsArray($bookSchemaProperties);
676+
$this->assertCount(5, $bookSchemaProperties);
677+
678+
$this->assertArrayHasKey('id', $bookSchemaProperties);
679+
$this->assertArrayHasKey('title', $bookSchemaProperties);
680+
$this->assertArrayHasKey('author', $bookSchemaProperties);
681+
$this->assertArrayHasKey('isbn', $bookSchemaProperties);
682+
$this->assertArrayHasKey('bookType', $bookSchemaProperties);
683+
671684
$this->assertArrayHasKey('discriminator', $bookSchema);
672685
$discriminator = $bookSchema['discriminator'];
673686

@@ -697,48 +710,42 @@ public function testOpenApiPolymorphicBookSchema(): void
697710
$this->assertArrayHasKey('FictionBook', $schemas, 'FictionBook schema should exist');
698711
$fictionBookSchema = $schemas['FictionBook'];
699712

700-
$this->assertIsArray($fictionBookSchema['allOf']);
713+
$this->assertArrayHasKey('allOf', $fictionBookSchema);
714+
$fictionBookSchemaAllOf = $fictionBookSchema['allOf'];
715+
$this->assertIsArray($fictionBookSchemaAllOf);
716+
$this->assertCount(2, $fictionBookSchemaAllOf);
701717

702-
$fictionProperties = null;
703-
foreach ($fictionBookSchema['allOf'] as $part) {
704-
if (isset($part['properties'])) {
705-
$fictionProperties = $part['properties'];
706-
break;
707-
}
708-
}
709-
$this->assertNotNull($fictionProperties, 'FictionBook should have properties in allOf');
710-
$this->assertArrayHasKey('genre', $fictionProperties);
711-
$this->assertArrayHasKey('pageCount', $fictionProperties);
718+
$this->assertArrayHasKey('$ref', $fictionBookSchemaAllOf[0]);
719+
$this->assertSame('#/components/schemas/Book', $fictionBookSchemaAllOf[0]['$ref']);
720+
$this->assertArrayHasKey('type', $fictionBookSchemaAllOf[1]);
721+
$this->assertArrayHasKey('properties', $fictionBookSchemaAllOf[1]);
722+
723+
$fictionBookSchemaAllOfProperties = $fictionBookSchemaAllOf[1]['properties'];
724+
$this->assertIsArray($fictionBookSchemaAllOfProperties);
725+
$this->assertCount(2, $fictionBookSchemaAllOfProperties);
726+
727+
$this->assertArrayHasKey('genre', $fictionBookSchemaAllOfProperties);
728+
$this->assertArrayHasKey('pageCount', $fictionBookSchemaAllOfProperties);
712729

713730
$this->assertArrayHasKey('TechnicalBook', $schemas, 'TechnicalBook schema should exist');
714731
$technicalBookSchema = $schemas['TechnicalBook'];
715732

716-
if (isset($technicalBookSchema['allOf'])) {
717-
$this->assertIsArray($technicalBookSchema['allOf']);
718-
$technicalProperties = null;
719-
foreach ($technicalBookSchema['allOf'] as $part) {
720-
if (isset($part['properties'])) {
721-
$technicalProperties = $part['properties'];
722-
break;
723-
}
724-
}
725-
$this->assertNotNull($technicalProperties, 'TechnicalBook should have properties in allOf');
726-
$this->assertArrayHasKey('programmingLanguage', $technicalProperties);
727-
$this->assertArrayHasKey('difficultyLevel', $technicalProperties);
728-
$this->assertArrayHasKey('topic', $technicalProperties);
729-
} else {
730-
$this->assertArrayHasKey('properties', $technicalBookSchema);
731-
$this->assertArrayHasKey('programmingLanguage', $technicalBookSchema['properties']);
732-
$this->assertArrayHasKey('difficultyLevel', $technicalBookSchema['properties']);
733-
$this->assertArrayHasKey('topic', $technicalBookSchema['properties']);
734-
}
733+
$this->assertArrayHasKey('allOf', $technicalBookSchema);
734+
$technicalBookSchemaAllOf = $technicalBookSchema['allOf'];
735+
$this->assertIsArray($technicalBookSchemaAllOf);
736+
$this->assertCount(2, $technicalBookSchemaAllOf);
735737

736-
if (isset($bookSchema['mapping'])) {
737-
$this->assertArrayHasKey('fiction', $bookSchema['mapping']);
738-
$this->assertArrayHasKey('technical', $bookSchema['mapping']);
739-
} elseif (isset($bookSchema['discriminator']['mapping'])) {
740-
$this->assertArrayHasKey('fiction', $bookSchema['discriminator']['mapping']);
741-
$this->assertArrayHasKey('technical', $bookSchema['discriminator']['mapping']);
742-
}
738+
$this->assertArrayHasKey('$ref', $technicalBookSchemaAllOf[0]);
739+
$this->assertSame('#/components/schemas/Book', $technicalBookSchemaAllOf[0]['$ref']);
740+
$this->assertArrayHasKey('type', $technicalBookSchemaAllOf[1]);
741+
$this->assertArrayHasKey('properties', $technicalBookSchemaAllOf[1]);
742+
743+
$technicalBookSchemaAllOfProperties = $technicalBookSchemaAllOf[1]['properties'];
744+
$this->assertIsArray($technicalBookSchemaAllOfProperties);
745+
$this->assertCount(3, $technicalBookSchemaAllOfProperties);
746+
747+
$this->assertArrayHasKey('programmingLanguage', $technicalBookSchemaAllOfProperties);
748+
$this->assertArrayHasKey('difficultyLevel', $technicalBookSchemaAllOfProperties);
749+
$this->assertArrayHasKey('topic', $technicalBookSchemaAllOfProperties);
743750
}
744751
}

0 commit comments

Comments
 (0)