AugmentSchemasTest.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Processors\AugmentSchemas;
  7. use OpenApi\Processors\MergeIntoComponents;
  8. use OpenApi\Processors\MergeIntoOpenApi;
  9. use OpenApi\StaticAnalyser;
  10. use const OpenApi\UNDEFINED;
  11. class AugmentSchemasTest extends OpenApiTestCase
  12. {
  13. public function testAugmentSchemas()
  14. {
  15. $analyser = new StaticAnalyser();
  16. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/Customer.php');
  17. $analysis->process(new MergeIntoOpenApi()); // create openapi->components
  18. $analysis->process(new MergeIntoComponents()); // Merge standalone Scheme's into openapi->components
  19. $this->assertCount(1, $analysis->openapi->components->schemas);
  20. $customer = $analysis->openapi->components->schemas[0];
  21. $this->assertSame(UNDEFINED, $customer->schema, 'Sanity check. No scheme was defined');
  22. $this->assertSame(UNDEFINED, $customer->properties, 'Sanity check. @OA\Property\'s not yet merged ');
  23. $analysis->process(new AugmentSchemas());
  24. $this->assertSame('Customer', $customer->schema, '@OA\Schema()->schema based on classname');
  25. $this->assertCount(9, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class');
  26. }
  27. public function testAugmentSchemasForInterface()
  28. {
  29. $analyser = new StaticAnalyser();
  30. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/CustomerInterface.php');
  31. $analysis->process(new MergeIntoOpenApi()); // create openapi->components
  32. $analysis->process(new MergeIntoComponents()); // Merge standalone Scheme's into openapi->components
  33. $this->assertCount(1, $analysis->openapi->components->schemas);
  34. $customer = $analysis->openapi->components->schemas[0];
  35. $this->assertSame(UNDEFINED, $customer->properties, 'Sanity check. @OA\Property\'s not yet merged ');
  36. $analysis->process(new AugmentSchemas());
  37. $this->assertCount(9, $customer->properties, '@OA\Property()s are merged into the @OA\Schema of the class');
  38. }
  39. }