CleanUnmergedTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Analysis;
  7. use OpenApi\Annotations\Contact;
  8. use OpenApi\Annotations\License;
  9. use OpenApi\Processors\CleanUnmerged;
  10. use OpenApi\Processors\MergeIntoOpenApi;
  11. class CleanUnmergedTest extends OpenApiTestCase
  12. {
  13. public function testCleanUnmergedProcessor()
  14. {
  15. $comment = <<<END
  16. @OA\Info(
  17. title="Info only has one contact field.",
  18. version="test",
  19. )
  20. @OA\PathItem(path="/test"),
  21. @OA\License(
  22. name="MIT",
  23. @OA\Contact(
  24. name="Batman"
  25. )
  26. )
  27. END;
  28. $analysis = new Analysis($this->parseComment($comment));
  29. $this->assertCount(4, $analysis->annotations);
  30. $analysis->process(new MergeIntoOpenApi());
  31. $this->assertCount(5, $analysis->annotations);
  32. $before = $analysis->split();
  33. $this->assertCount(3, $before->merged->annotations, 'Generated @OA\OpenApi, @OA\PathItem and @OA\Info');
  34. $this->assertCount(2, $before->unmerged->annotations, '@OA\License + @OA\Contact');
  35. $this->assertCount(0, $analysis->openapi->_unmerged);
  36. $analysis->validate(); // Validation fails to detect the unmerged annotations.
  37. // CleanUnmerged should place the unmerged annotions into the swagger->_unmerged array.
  38. $analysis->process(new CleanUnmerged());
  39. $between = $analysis->split();
  40. $this->assertCount(3, $between->merged->annotations, 'Generated @OA\OpenApi, @OA\PathItem and @OA\Info');
  41. $this->assertCount(2, $between->unmerged->annotations, '@OA\License + @OA\Contact');
  42. $this->assertCount(2, $analysis->openapi->_unmerged); // 1 would also be oke, Could a'Only the @OA\License'
  43. $this->assertOpenApiLogEntryStartsWith('Unexpected @OA\License(), expected to be inside @OA\Info in ');
  44. $this->assertOpenApiLogEntryStartsWith('Unexpected @OA\Contact(), expected to be inside @OA\Info in ');
  45. $analysis->validate();
  46. // When a processor places a previously unmerged annotation into the swagger obect.
  47. $license = $analysis->getAnnotationsOfType(License::class)[0];
  48. $contact = $analysis->getAnnotationsOfType(Contact::class)[0];
  49. $analysis->openapi->info->contact = $contact;
  50. $this->assertCount(1, $license->_unmerged);
  51. $analysis->process(new CleanUnmerged());
  52. $this->assertCount(0, $license->_unmerged);
  53. $after = $analysis->split();
  54. $this->assertCount(4, $after->merged->annotations, 'Generated @OA\OpenApi, @OA\PathItem, @OA\Info and @OA\Contact');
  55. $this->assertCount(1, $after->unmerged->annotations, '@OA\License');
  56. $this->assertCount(1, $analysis->openapi->_unmerged);
  57. $this->assertOpenApiLogEntryStartsWith('Unexpected @OA\License(), expected to be inside @OA\Info in ');
  58. $analysis->validate();
  59. }
  60. }