InheritPropertiesTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace SwaggerTests;
  6. use Swagger\Annotations\Info;
  7. use Swagger\Processors\AugmentDefinitions;
  8. use Swagger\Processors\AugmentProperties;
  9. use Swagger\Processors\InheritProperties;
  10. use Swagger\Processors\MergeIntoSwagger;
  11. use Swagger\StaticAnalyser;
  12. class InheritPropertiesTest extends SwaggerTestCase
  13. {
  14. public function testInheritProperties()
  15. {
  16. $analyser = new StaticAnalyser();
  17. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/Child.php');
  18. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/GrandAncestor.php'));
  19. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/Ancestor.php'));
  20. $analysis->process([
  21. new MergeIntoSwagger(),
  22. new AugmentDefinitions(),
  23. new AugmentProperties()
  24. ]);
  25. $definitions = $analysis->getAnnotationsOfType('\Swagger\Annotations\Definition');
  26. $childDefinition = $definitions[0];
  27. $this->assertSame('Child', $childDefinition->definition);
  28. $this->assertCount(1, $childDefinition->properties);
  29. $analysis->process(new InheritProperties());
  30. $this->assertCount(3, $childDefinition->properties);
  31. $analysis->swagger->info = new Info(['title' => 'test', 'version' => 1]);
  32. $analysis->validate();
  33. }
  34. /**
  35. * Tests, if InheritProperties works even without any
  36. * docBlocks at all in the parent class.
  37. */
  38. public function testInheritPropertiesWithoutDocBlocks()
  39. {
  40. $analyser = new StaticAnalyser();
  41. // this class has docblocks
  42. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/ChildWithDocBlocks.php');
  43. // this one doesn't
  44. $analysis->addAnalysis($analyser->fromFile(__DIR__ . '/Fixtures/AncestorWithoutDocBlocks.php'));
  45. $analysis->process([
  46. new MergeIntoSwagger(),
  47. new AugmentDefinitions(),
  48. new AugmentProperties()
  49. ]);
  50. $definitions = $analysis->getAnnotationsOfType('\Swagger\Annotations\Definition');
  51. $childDefinition = $definitions[0];
  52. $this->assertSame('ChildWithDocBlocks', $childDefinition->definition);
  53. $this->assertCount(1, $childDefinition->properties);
  54. // no error occurs
  55. $analysis->process(new InheritProperties());
  56. $this->assertCount(1, $childDefinition->properties);
  57. $analysis->swagger->info = new Info(['title' => 'test', 'version' => 1]);
  58. $analysis->validate();
  59. }
  60. }