4
0

AbstractAnnotationTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php declare(strict_types=1);
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace OpenApiTests;
  6. class AbstractAnnotationTest extends OpenApiTestCase
  7. {
  8. public function testVendorFields()
  9. {
  10. $annotations = $this->parseComment('@OA\Get(x={"internal-id": 123})');
  11. $output = $annotations[0]->jsonSerialize();
  12. $prefixedProperty = 'x-internal-id';
  13. $this->assertSame(123, $output->$prefixedProperty);
  14. }
  15. public function testInvalidField()
  16. {
  17. $this->assertOpenApiLogEntryStartsWith('Unexpected field "doesnot" for @OA\Get(), expecting');
  18. $this->parseComment('@OA\Get(doesnot="exist")');
  19. }
  20. public function testUmergedAnnotation()
  21. {
  22. $openapi = $this->createOpenApiWithInfo();
  23. $openapi->merge($this->parseComment('@OA\Items()'));
  24. $this->assertOpenApiLogEntryStartsWith('Unexpected @OA\Items(), expected to be inside @OA\\');
  25. $openapi->validate();
  26. }
  27. public function testConflictedNesting()
  28. {
  29. $comment = <<<END
  30. @OA\Info(
  31. title="Info only has one contact field..",
  32. version="test",
  33. @OA\Contact(name="first"),
  34. @OA\Contact(name="second")
  35. )
  36. END;
  37. $annotations = $this->parseComment($comment);
  38. $this->assertOpenApiLogEntryStartsWith('Only one @OA\Contact() allowed for @OA\Info() multiple found in:');
  39. $annotations[0]->validate();
  40. }
  41. public function testKey()
  42. {
  43. $comment = <<<END
  44. @OA\Response(
  45. @OA\Header(header="X-CSRF-Token",description="Token to prevent Cross Site Request Forgery")
  46. )
  47. END;
  48. $annotations = $this->parseComment($comment);
  49. $this->assertEquals('{"headers":{"X-CSRF-Token":{"description":"Token to prevent Cross Site Request Forgery"}}}', json_encode($annotations[0]));
  50. }
  51. public function testConflictingKey()
  52. {
  53. $comment = <<<END
  54. @OA\Response(
  55. description="The headers in response must have unique header values",
  56. @OA\Header(header="X-CSRF-Token", @OA\Schema(type="string"), description="first"),
  57. @OA\Header(header="X-CSRF-Token", @OA\Schema(type="string"), description="second")
  58. )
  59. END;
  60. $annotations = $this->parseComment($comment);
  61. $this->assertOpenApiLogEntryStartsWith('Multiple @OA\Header() with the same header="X-CSRF-Token":');
  62. $annotations[0]->validate();
  63. }
  64. public function testRequiredFields()
  65. {
  66. $annotations = $this->parseComment('@OA\Info()');
  67. $info = $annotations[0];
  68. $this->assertOpenApiLogEntryStartsWith('Missing required field "title" for @OA\Info() in ');
  69. $this->assertOpenApiLogEntryStartsWith('Missing required field "version" for @OA\Info() in ');
  70. $info->validate();
  71. }
  72. public function testTypeValidation()
  73. {
  74. $comment = <<<END
  75. @OA\Parameter(
  76. name=123,
  77. in="dunno",
  78. required="maybe",
  79. @OA\Schema(
  80. type="strig",
  81. )
  82. )
  83. END;
  84. $annotations = $this->parseComment($comment);
  85. $parameter = $annotations[0];
  86. $this->assertOpenApiLogEntryStartsWith('@OA\Parameter(name=123,in="dunno")->name is a "integer", expecting a "string" in ');
  87. $this->assertOpenApiLogEntryStartsWith('@OA\Parameter(name=123,in="dunno")->in "dunno" is invalid, expecting "query", "header", "path", "cookie" in ');
  88. $this->assertOpenApiLogEntryStartsWith('@OA\Parameter(name=123,in="dunno")->required is a "string", expecting a "boolean" in ');
  89. // $this->assertOpenApiLogEntryStartsWith('@OA\Parameter(name=123,in="dunno")->maximum is a "string", expecting a "number" in ');
  90. // $this->assertOpenApiLogEntryStartsWith('@OA\Parameter(name=123,in="dunno")->type must be "string", "number", "integer", "boolean", "array", "file" when @OA\Parameter()->in != "body" in ');
  91. $parameter->validate();
  92. }
  93. }