StaticAnalyserTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace SwaggerTests;
  6. use Swagger\Analyser;
  7. use Swagger\Context;
  8. use Swagger\StaticAnalyser;
  9. class StaticAnalyserTest extends SwaggerTestCase
  10. {
  11. public function testWrongCommentType()
  12. {
  13. $analyser = new StaticAnalyser();
  14. $this->assertSwaggerLogEntryStartsWith('Annotations are only parsed inside `/**` DocBlocks');
  15. $analyser->fromCode("<?php\n/*\n * @SWG\Parameter() */", new Context([]));
  16. }
  17. public function testIndentationCorrection()
  18. {
  19. $analyser = new StaticAnalyser();
  20. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/routes.php');
  21. $this->assertCount(18, $analysis->annotations);
  22. }
  23. public function testTrait()
  24. {
  25. $analyser = new StaticAnalyser();
  26. $analysis = $analyser->fromFile(__DIR__ . '/Fixtures/HelloTrait.php');
  27. $this->assertCount(2, $analysis->annotations);
  28. $property = $analysis->getAnnotationsOfType('Swagger\Annotations\Property')[0];
  29. $this->assertSame('Hello', $property->_context->trait);
  30. }
  31. public function testThirdPartyAnnotations()
  32. {
  33. $backup = Analyser::$whitelist;
  34. Analyser::$whitelist = ['Swagger\Annotations\\'];
  35. $analyser = new StaticAnalyser();
  36. $defaultAnalysis = $analyser->fromFile(__DIR__ . '/Fixtures/ThirdPartyAnnotations.php');
  37. $this->assertCount(3, $defaultAnalysis->annotations, 'Only read the @SWG annotations, skip the others.');
  38. // Allow Swagger to parse 3rd party annotations
  39. // might contain useful info that could be extracted with a custom processor
  40. Analyser::$whitelist[] = 'Zend\Form\Annotation';
  41. $swagger = \Swagger\scan(__DIR__ . '/Fixtures/ThirdPartyAnnotations.php');
  42. $this->assertSame('api/3rd-party', $swagger->paths[0]->path);
  43. $this->assertCount(10, $swagger->_unmerged);
  44. Analyser::$whitelist = $backup;
  45. $analysis = $swagger->_analysis;
  46. $annotations = $analysis->getAnnotationsOfType('Zend\Form\Annotation\Name');
  47. $this->assertCount(1, $annotations);
  48. $context = $analysis->getContext($annotations[0]);
  49. $this->assertInstanceOf('Swagger\Context', $context);
  50. $this->assertSame('ThirdPartyAnnotations', $context->class);
  51. $this->assertSame('\SwaggerFixtures\ThirdPartyAnnotations', $context->fullyQualifiedName($context->class));
  52. $this->assertCount(2, $context->annotations);
  53. }
  54. public function testAnonymousClassProducesNoError()
  55. {
  56. try {
  57. $analyser = new StaticAnalyser(__DIR__ . '/Fixtures/php7.php');
  58. $this->assertTrue(true);
  59. } catch (\Exception $e) {
  60. $this->fail("Analyser produced an error: {$e->getMessage()}");
  61. }
  62. }
  63. }