ConstantsTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. use OpenApi\Analyser;
  7. use OpenApi\StaticAnalyser;
  8. class ConstantsTest extends OpenApiTestCase
  9. {
  10. const URL = 'http://example.com';
  11. private static $counter = 0;
  12. public function testConstant()
  13. {
  14. self::$counter++;
  15. $const = 'OPENAPI_TEST_'.self::$counter;
  16. $this->assertFalse(defined($const));
  17. $this->assertOpenApiLogEntryStartsWith("[Semantical Error] Couldn't find constant ".$const);
  18. $this->parseComment('@OA\Contact(email='.$const.')');
  19. define($const, 'me@domain.org');
  20. $annotations = $this->parseComment('@OA\Contact(email='.$const.')');
  21. $this->assertSame('me@domain.org', $annotations[0]->email);
  22. }
  23. public function testFQCNConstant()
  24. {
  25. $annotations = $this->parseComment('@OA\Contact(url=OpenApiTests\ConstantsTest::URL)');
  26. $this->assertSame('http://example.com', $annotations[0]->url);
  27. $annotations = $this->parseComment('@OA\Contact(url=\OpenApiTests\ConstantsTest::URL)');
  28. $this->assertSame('http://example.com', $annotations[0]->url);
  29. }
  30. public function testInvalidClass()
  31. {
  32. $this->assertOpenApiLogEntryStartsWith("[Semantical Error] Couldn't find constant ConstantsTest::URL");
  33. $this->parseComment('@OA\Contact(url=ConstantsTest::URL)');
  34. }
  35. public function testAutoloadConstant()
  36. {
  37. if (class_exists('Zend\Validator\Timezone', false)) {
  38. $this->markTestSkipped();
  39. }
  40. $annotations = $this->parseComment('@OA\Contact(name=Zend\Validator\Timezone::INVALID_TIMEZONE_LOCATION)');
  41. $this->assertSame('invalidTimezoneLocation', $annotations[0]->name);
  42. }
  43. public function testDynamicImports()
  44. {
  45. $backup = Analyser::$whitelist;
  46. Analyser::$whitelist = false;
  47. $analyser = new StaticAnalyser();
  48. $analysis = $analyser->fromFile(__DIR__.'/Fixtures/Customer.php');
  49. // @todo Only tests that $whitelist=false doesn't trigger errors,
  50. // No constants are used, because by default only class constants in the whitelisted namespace are allowed and no class in OpenApi\Annotation namespace has a constant.
  51. // Scanning without whitelisting causes issues, to check uncomment next.
  52. // $analyser->fromFile(__DIR__ . '/Fixtures/ThirdPartyAnnotations.php');
  53. Analyser::$whitelist = $backup;
  54. }
  55. public function testDefaultImports()
  56. {
  57. $backup = Analyser::$defaultImports;
  58. Analyser::$defaultImports = [
  59. 'contact' => 'OpenApi\Annotations\Contact', // use OpenApi\Annotations\Contact;
  60. 'ctest' => 'OpenApiTests\ConstantsTesT' // use OpenApiTests\ConstantsTesT as CTest;
  61. ];
  62. $annotations = $this->parseComment('@Contact(url=CTest::URL)');
  63. $this->assertSame('http://example.com', $annotations[0]->url);
  64. Analyser::$defaultImports = $backup;
  65. }
  66. }