AnnotationParserTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests;
  3. use EasySwoole\HttpAnnotation\Annotation\MethodAnnotation;
  4. use EasySwoole\HttpAnnotation\Annotation\ObjectAnnotation;
  5. use EasySwoole\HttpAnnotation\Annotation\Parser;
  6. use EasySwoole\HttpAnnotation\Annotation\PropertyAnnotation;
  7. use EasySwoole\HttpAnnotation\AnnotationTag\Api;
  8. use EasySwoole\HttpAnnotation\AnnotationTag\ApiAuth;
  9. use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
  10. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFail;
  11. use EasySwoole\HttpAnnotation\AnnotationTag\ApiFailParam;
  12. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroupDescription;
  13. use EasySwoole\HttpAnnotation\AnnotationTag\ApiRequestExample;
  14. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccess;
  15. use EasySwoole\HttpAnnotation\AnnotationTag\ApiSuccessParam;
  16. use EasySwoole\HttpAnnotation\AnnotationTag\CircuitBreaker;
  17. use EasySwoole\HttpAnnotation\AnnotationTag\Context;
  18. use EasySwoole\HttpAnnotation\AnnotationTag\Controller;
  19. use EasySwoole\HttpAnnotation\AnnotationTag\Di;
  20. use EasySwoole\HttpAnnotation\AnnotationTag\Inject;
  21. use EasySwoole\HttpAnnotation\AnnotationTag\InjectParamsContext;
  22. use EasySwoole\HttpAnnotation\AnnotationTag\Method;
  23. use EasySwoole\HttpAnnotation\AnnotationTag\Param;
  24. use EasySwoole\HttpAnnotation\Tests\TestController\Annotation;
  25. use EasySwoole\HttpAnnotation\Tests\TestController\RouterPath;
  26. use PHPUnit\Framework\TestCase;
  27. use PHPUnit\Framework\TestResult;
  28. use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup as ApiGroupTag;
  29. class AnnotationParserTest extends TestCase
  30. {
  31. /**
  32. * @var ObjectAnnotation
  33. */
  34. protected $apiGroup;
  35. /** @var ObjectAnnotation */
  36. protected $resultB;
  37. function run(TestResult $result = null): TestResult
  38. {
  39. $parse = new Parser();
  40. $this->apiGroup = $parse->parseObject(new \ReflectionClass(Annotation::class));
  41. return parent::run($result);
  42. }
  43. function testApiGroup()
  44. {
  45. $this->assertInstanceOf(ApiGroupTag::class, $this->apiGroup->getApiGroupTag());
  46. $this->assertEquals('GroupA', $this->apiGroup->getApiGroupTag()->groupName);
  47. }
  48. function testApiGroupDescription()
  49. {
  50. $this->assertInstanceOf(ApiGroupDescription::class, $this->apiGroup->getApiGroupDescriptionTag());
  51. $this->assertEquals('GroupA desc', $this->apiGroup->getApiGroupDescriptionTag()->value);
  52. }
  53. function testApiGroupAuth()
  54. {
  55. $this->assertIsArray($this->apiGroup->getGroupAuthTag());
  56. $this->assertEquals(2, count($this->apiGroup->getGroupAuthTag()));
  57. $this->assertEquals('groupParamA', $this->apiGroup->getGroupAuthTag('groupParamA')->name);
  58. $this->assertEquals('groupParamB', $this->apiGroup->getGroupAuthTag('groupParamB')->name);
  59. }
  60. function testAnnotationMethod()
  61. {
  62. $this->assertEquals(null, $this->apiGroup->getMethod('noneFunc'));
  63. $this->assertInstanceOf(MethodAnnotation::class, $this->apiGroup->getMethod('func'));
  64. $this->assertEquals('func', $this->apiGroup->getMethod('func')->getMethodName());
  65. }
  66. function testApi()
  67. {
  68. /** @var MethodAnnotation $func */
  69. $func = $this->apiGroup->getMethod('func');
  70. $this->assertInstanceOf(Api::class, $func->getApiTag());
  71. $this->assertEquals('func', $func->getApiTag()->name);
  72. $this->assertEquals('/apiGroup/func', $func->getApiTag()->path);
  73. }
  74. function testApiAuth()
  75. {
  76. /** @var MethodAnnotation $func */
  77. $func = $this->apiGroup->getMethod('func');
  78. $this->assertEquals(2, count($func->getApiAuth()));
  79. $this->assertInstanceOf(ApiAuth::class, $func->getApiAuth('apiAuth1'));
  80. }
  81. function testApiDescription()
  82. {
  83. /** @var MethodAnnotation $func */
  84. $func = $this->apiGroup->getMethod('func');
  85. $this->assertInstanceOf(ApiDescription::class, $func->getApiDescriptionTag());
  86. $this->assertEquals('func desc', $func->getApiDescriptionTag()->value);
  87. }
  88. function testApiFail()
  89. {
  90. /** @var MethodAnnotation $func */
  91. $func = $this->apiGroup->getMethod('func');
  92. $this->assertEquals(2, count($func->getApiFail()));
  93. $this->assertInstanceOf(ApiFail::class, $func->getApiFail()[0]);
  94. $this->assertEquals('func fail example1', $func->getApiFail()[0]->value);
  95. }
  96. function testApiFailParam()
  97. {
  98. /** @var MethodAnnotation $func */
  99. $func = $this->apiGroup->getMethod('func');
  100. $this->assertEquals(2, count($func->getApiFailParam()));
  101. $this->assertInstanceOf(ApiFailParam::class, $func->getApiFailParam('failParam1'));
  102. $this->assertEquals('failParam1', $func->getApiFailParam('failParam1')->name);
  103. }
  104. function testApiRequestExample()
  105. {
  106. /** @var MethodAnnotation $func */
  107. $func = $this->apiGroup->getMethod('func');
  108. $this->assertEquals(2, count($func->getApiRequestExample()));
  109. $this->assertInstanceOf(ApiRequestExample::class, $func->getApiRequestExample()[0]);
  110. $this->assertEquals('func request example1', $func->getApiRequestExample()[0]->value);
  111. }
  112. function testApiSuccess()
  113. {
  114. /** @var MethodAnnotation $func */
  115. $func = $this->apiGroup->getMethod('func');
  116. $this->assertEquals(2, count($func->getApiSuccess()));
  117. $this->assertInstanceOf(ApiSuccess::class, $func->getApiSuccess()[0]);
  118. $this->assertEquals('func success example1', $func->getApiSuccess()[0]->value);
  119. }
  120. function testApiSuccessParam()
  121. {
  122. /** @var MethodAnnotation $func */
  123. $func = $this->apiGroup->getMethod('func');
  124. $this->assertEquals(2, count($func->getApiSuccessParam()));
  125. $this->assertInstanceOf(ApiSuccessParam::class, $func->getApiSuccessParam('successParam1'));
  126. $this->assertEquals('successParam1', $func->getApiSuccessParam('successParam1')->name);
  127. }
  128. function testCircuitBreaker()
  129. {
  130. /** @var MethodAnnotation $func */
  131. $func = $this->apiGroup->getMethod('func');
  132. $this->assertInstanceOf(CircuitBreaker::class, $func->getCircuitBreakerTag());
  133. $this->assertEquals(5.0, $func->getCircuitBreakerTag()->timeout);
  134. }
  135. function testInjectParamsContext()
  136. {
  137. /** @var MethodAnnotation $func */
  138. $func = $this->apiGroup->getMethod('func');
  139. $this->assertInstanceOf(InjectParamsContext::class, $func->getInjectParamsContextTag());
  140. $this->assertEquals('requestData', $func->getInjectParamsContextTag()->key);
  141. }
  142. function testMethod()
  143. {
  144. /** @var MethodAnnotation $func */
  145. $func = $this->apiGroup->getMethod('func');
  146. $this->assertInstanceOf(Method::class, $func->getMethodTag());
  147. $this->assertEquals(['POST', 'GET'], $func->getMethodTag()->allow);
  148. }
  149. function testParam()
  150. {
  151. /** @var MethodAnnotation $func */
  152. $func = $this->apiGroup->getMethod('func');
  153. $this->assertEquals(2, count($func->getParamTag()));
  154. $this->assertInstanceOf(Param::class, $func->getParamTag('param1'));
  155. $this->assertEquals('param1', $func->getParamTag('param1')->name);
  156. }
  157. function testProperty()
  158. {
  159. /** @var PropertyAnnotation $di */
  160. $di = $this->apiGroup->getProperty('di');
  161. $this->assertInstanceOf(PropertyAnnotation::class, $di);
  162. $this->assertEquals('di', $di->getName());
  163. /** @var PropertyAnnotation $context */
  164. $context = $this->apiGroup->getProperty('context');
  165. $this->assertInstanceOf(PropertyAnnotation::class, $context);
  166. $this->assertEquals('context', $context->getName());
  167. }
  168. function testDi()
  169. {
  170. /** @var PropertyAnnotation $di */
  171. $di = $this->apiGroup->getProperty('di');
  172. $this->assertInstanceOf(Di::class, $di->getDiTag());
  173. $this->assertEquals('di', $di->getDiTag()->key);
  174. }
  175. function testContext()
  176. {
  177. /** @var PropertyAnnotation $context */
  178. $context = $this->apiGroup->getProperty('context');
  179. $this->assertInstanceOf(Context::class, $context->getContextTag());
  180. $this->assertEquals('context', $context->getContextTag()->key);
  181. }
  182. function testInject()
  183. {
  184. /** @var PropertyAnnotation $inject */
  185. $inject = $this->apiGroup->getProperty('inject');
  186. $this->assertInstanceOf(Inject::class, $inject->getInjectTag());
  187. $this->assertEquals('\EasySwoole\HttpAnnotation\Tests\TestController\Test', $inject->getInjectTag()->className);
  188. $this->assertEquals([1, [1, 2]], $inject->getInjectTag()->args);
  189. }
  190. function testPrefix()
  191. {
  192. $parse = new Parser();
  193. $objAnnotation = $parse->parseObject(new \ReflectionClass(RouterPath::class));
  194. $this->assertInstanceOf(Controller::class, $objAnnotation->getController());
  195. $this->assertEquals('/Router', $objAnnotation->getController()->prefix);
  196. }
  197. }