AbstractAnnotationTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @license Apache 2.0
  4. */
  5. namespace SwaggerTests;
  6. class AbstractAnnotationTest extends SwaggerTestCase
  7. {
  8. public function testVendorFields()
  9. {
  10. $annotations = $this->parseComment('@SWG\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->assertSwaggerLogEntryStartsWith('Unexpected field "doesnot" for @SWG\Get(), expecting');
  18. $this->parseComment('@SWG\Get(doesnot="exist")');
  19. }
  20. public function testUmergedAnnotation()
  21. {
  22. $swagger = $this->createSwaggerWithInfo();
  23. $swagger->merge($this->parseComment('@SWG\Items()'));
  24. $this->assertSwaggerLogEntryStartsWith('Unexpected @SWG\Items(), expected to be inside @SWG\\');
  25. $swagger->validate();
  26. }
  27. public function testConflictedNesting()
  28. {
  29. $comment = <<<END
  30. @SWG\Info(
  31. title="Info only has one contact field..",
  32. version="test",
  33. @SWG\Contact(name="first"),
  34. @SWG\Contact(name="second")
  35. )
  36. END;
  37. $annotations = $this->parseComment($comment);
  38. $this->assertSwaggerLogEntryStartsWith('Only one @SWG\Contact() allowed for @SWG\Info() multiple found in:');
  39. $annotations[0]->validate();
  40. }
  41. public function testKey()
  42. {
  43. $comment = <<<END
  44. @SWG\Response(
  45. @SWG\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. @SWG\Response(
  55. description="The headers in response must have unique header values",
  56. @SWG\Header(header="X-CSRF-Token", type="string", description="first"),
  57. @SWG\Header(header="X-CSRF-Token", type="string", description="second")
  58. )
  59. END;
  60. $annotations = $this->parseComment($comment);
  61. $this->assertSwaggerLogEntryStartsWith('Multiple @SWG\Header() with the same header="X-CSRF-Token":');
  62. $annotations[0]->validate();
  63. }
  64. public function testRequiredFields()
  65. {
  66. $annotations = $this->parseComment('@SWG\Info()');
  67. $info = $annotations[0];
  68. $this->assertSwaggerLogEntryStartsWith('Missing required field "title" for @SWG\Info() in ');
  69. $this->assertSwaggerLogEntryStartsWith('Missing required field "version" for @SWG\Info() in ');
  70. $info->validate();
  71. }
  72. public function testTypeValidation()
  73. {
  74. $comment = <<<END
  75. @SWG\Parameter(
  76. name=123,
  77. type="strig",
  78. in="dunno",
  79. required="maybe",
  80. maximum="twentytwo"
  81. )
  82. END;
  83. $annotations = $this->parseComment($comment);
  84. $parameter = $annotations[0];
  85. $this->assertSwaggerLogEntryStartsWith('@SWG\Parameter(name=123,in="dunno")->name is a "integer", expecting a "string" in ');
  86. $this->assertSwaggerLogEntryStartsWith('@SWG\Parameter(name=123,in="dunno")->in "dunno" is invalid, expecting "query", "header", "path", "formData", "body" in ');
  87. $this->assertSwaggerLogEntryStartsWith('@SWG\Parameter(name=123,in="dunno")->required is a "string", expecting a "boolean" in ');
  88. $this->assertSwaggerLogEntryStartsWith('@SWG\Parameter(name=123,in="dunno")->maximum is a "string", expecting a "number" in ');
  89. $this->assertSwaggerLogEntryStartsWith('@SWG\Parameter(name=123,in="dunno")->type must be "string", "number", "integer", "boolean", "array", "file" when @SWG\Parameter()->in != "body" in ');
  90. $parameter->validate();
  91. }
  92. }