CleanUnmergedTest.php 2.7 KB

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