InheritPropertiesTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Annotations\Components;
  7. use OpenApi\Annotations\Info;
  8. use OpenApi\Annotations\PathItem;
  9. use OpenApi\Annotations\Schema;
  10. use OpenApi\Processors\AugmentProperties;
  11. use OpenApi\Processors\AugmentSchemas;
  12. use OpenApi\Processors\InheritProperties;
  13. use OpenApi\Processors\MergeIntoComponents;
  14. use OpenApi\Processors\MergeIntoOpenApi;
  15. use OpenApi\StaticAnalyser;
  16. use const OpenApi\UNDEFINED;
  17. class InheritPropertiesTest extends OpenApiTestCase
  18. {
  19. public function testInheritProperties()
  20. {
  21. $analyser = new StaticAnalyser();
  22. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Child.php');
  23. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/GrandAncestor.php'));
  24. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/Ancestor.php'));
  25. $analysis->process(
  26. [
  27. new MergeIntoOpenApi(),
  28. new AugmentSchemas(),
  29. new AugmentProperties(),
  30. ]
  31. );
  32. $schemas = $analysis->getAnnotationsOfType(Schema::class);
  33. $childSchema = $schemas[0];
  34. $this->assertSame('Child', $childSchema->schema);
  35. $this->assertCount(1, $childSchema->properties);
  36. $analysis->process(new InheritProperties());
  37. $this->assertCount(3, $childSchema->properties);
  38. $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0']);
  39. $analysis->openapi->paths = [new PathItem(['path' => '/test'])];
  40. $analysis->validate();
  41. }
  42. /**
  43. * Tests, if InheritProperties works even without any
  44. * docBlocks at all in the parent class.
  45. */
  46. public function testInheritPropertiesWithoutDocBlocks()
  47. {
  48. $analyser = new StaticAnalyser();
  49. // this class has docblocks
  50. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/ChildWithDocBlocks.php');
  51. // this one doesn't
  52. $analysis->addAnalysis($analyser->fromFile(__DIR__.'/Fixtures/InheritProperties/AncestorWithoutDocBlocks.php'));
  53. $analysis->process(
  54. [
  55. new MergeIntoOpenApi(),
  56. new AugmentSchemas(),
  57. new AugmentProperties(),
  58. ]
  59. );
  60. $schemas = $analysis->getAnnotationsOfType(Schema::class);
  61. $childSchema = $schemas[0];
  62. $this->assertSame('ChildWithDocBlocks', $childSchema->schema);
  63. $this->assertCount(1, $childSchema->properties);
  64. // no error occurs
  65. $analysis->process(new InheritProperties());
  66. $this->assertCount(1, $childSchema->properties);
  67. $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0']);
  68. $analysis->openapi->paths = [new PathItem(['path' => '/test'])];
  69. $analysis->validate();
  70. }
  71. /**
  72. * Tests inherit properties with all of block
  73. */
  74. public function testInheritPropertiesWithAllOf()
  75. {
  76. $analyser = new StaticAnalyser();
  77. // this class has all of
  78. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/Extended.php');
  79. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/Base.php'));
  80. $analysis->process(
  81. [
  82. new MergeIntoOpenApi(),
  83. new AugmentSchemas(),
  84. new AugmentProperties(),
  85. new MergeIntoComponents(),
  86. new InheritProperties()
  87. ]
  88. );
  89. $schemas = $analysis->getAnnotationsOfType(Schema::class, true);
  90. $this->assertCount(3, $schemas);
  91. /* @var Schema $extendedSchema */
  92. $extendedSchema = $schemas[0];
  93. $this->assertSame('ExtendedModel', $extendedSchema->schema);
  94. $this->assertSame(UNDEFINED, $extendedSchema->properties);
  95. $this->assertArrayHasKey(1, $extendedSchema->allOf);
  96. $this->assertEquals($extendedSchema->allOf[1]->properties[0]->property, 'extendedProperty');
  97. /* @var $includeSchemaWithRef Schema */
  98. $includeSchemaWithRef = $schemas[1];
  99. $this->assertSame(UNDEFINED, $includeSchemaWithRef->properties);
  100. $analysis->openapi->info = new Info(['title' => 'test', 'version' => "1.0.0"]);
  101. $analysis->openapi->paths = [new PathItem(['path' => '/test'])];
  102. $analysis->validate();
  103. }
  104. /**
  105. * Tests for inherit properties without all of block
  106. */
  107. public function testInheritPropertiesWithOtAllOf()
  108. {
  109. $analyser = new StaticAnalyser();
  110. // this class has all of
  111. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/ExtendedWithoutAllOf.php');
  112. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/Base.php'));
  113. $analysis->process(
  114. [
  115. new MergeIntoOpenApi(),
  116. new AugmentSchemas(),
  117. new AugmentProperties(),
  118. new MergeIntoComponents(),
  119. new InheritProperties()
  120. ]
  121. );
  122. $schemas = $analysis->getAnnotationsOfType(Schema::class, true);
  123. $this->assertCount(2, $schemas);
  124. /* @var Schema $extendedSchema */
  125. $extendedSchema = $schemas[0];
  126. $this->assertSame('ExtendedWithoutAllOf', $extendedSchema->schema);
  127. $this->assertSame(UNDEFINED, $extendedSchema->properties);
  128. $this->assertCount(2, $extendedSchema->allOf);
  129. $this->assertEquals($extendedSchema->allOf[0]->ref, Components::SCHEMA_REF . 'Base');
  130. $this->assertEquals($extendedSchema->allOf[1]->properties[0]->property, 'extendedProperty');
  131. $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0']);
  132. $analysis->openapi->paths = [new PathItem(['path' => '/test'])];
  133. $analysis->validate();
  134. }
  135. /**
  136. * Tests for inherit properties in object with two schemas in the same context
  137. */
  138. public function testInheritPropertiesWitTwoChildSchemas()
  139. {
  140. $analyser = new StaticAnalyser();
  141. // this class has all of
  142. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/ExtendedWithTwoSchemas.php');
  143. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/InheritProperties/Base.php'));
  144. $analysis->process(
  145. [
  146. new MergeIntoOpenApi(),
  147. new AugmentSchemas(),
  148. new AugmentProperties(),
  149. new MergeIntoComponents(),
  150. new InheritProperties()
  151. ]
  152. );
  153. $schemas = $analysis->getAnnotationsOfType(Schema::class, true);
  154. $this->assertCount(3, $schemas);
  155. /* @var Schema $extendedSchema */
  156. $extendedSchema = $schemas[0];
  157. $this->assertSame('ExtendedWithTwoSchemas', $extendedSchema->schema);
  158. $this->assertSame(UNDEFINED, $extendedSchema->properties);
  159. $this->assertCount(2, $extendedSchema->allOf);
  160. $this->assertEquals($extendedSchema->allOf[0]->ref, Components::SCHEMA_REF . 'Base');
  161. $this->assertEquals($extendedSchema->allOf[1]->properties[0]->property, 'nested');
  162. $this->assertEquals($extendedSchema->allOf[1]->properties[1]->property, 'extendedProperty');
  163. /* @var $nestedSchema Schema */
  164. $nestedSchema = $schemas[1];
  165. $this->assertSame(UNDEFINED, $nestedSchema->allOf);
  166. $this->assertCount(1, $nestedSchema->properties);
  167. $this->assertEquals($nestedSchema->properties[0]->property, 'nestedProperty');
  168. $analysis->openapi->info = new Info(['title' => 'test', 'version' => '1.0.0']);
  169. $analysis->openapi->paths = [new PathItem(['path' => '/test'])];
  170. $analysis->validate();
  171. }
  172. }