ParamTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests;
  3. use EasySwoole\HttpAnnotation\Exception\Annotation\ParamValidateError;
  4. use EasySwoole\HttpAnnotation\Tests\TestController\Param;
  5. use PHPUnit\Framework\TestCase;
  6. class ParamTest extends TestCase
  7. {
  8. use ControllerBase;
  9. protected $controller;
  10. protected function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->controller = new Param();
  14. }
  15. public function testClassAuthError()
  16. {
  17. $response = $this->fakeResponse();
  18. $this->expectException(ParamValidateError::class);
  19. $this->expectExceptionMessage("validate fail for column groupAuth");
  20. $this->controller->__hook('index', $this->fakeRequest(), $response);
  21. $this->fail("test class auth error fail");
  22. }
  23. public function testClassParamError()
  24. {
  25. $response = $this->fakeResponse();
  26. $this->expectException(ParamValidateError::class);
  27. $this->expectExceptionMessage("validate fail for column groupParam");
  28. $this->controller->__hook('index', $this->fakeRequest('/', ['groupAuth' => 1]), $response);
  29. $this->fail("test class param error fail");
  30. }
  31. public function testOnRequestAuthError()
  32. {
  33. $response = $this->fakeResponse();
  34. $this->expectException(ParamValidateError::class);
  35. $this->expectExceptionMessage("validate fail for column onRequestAuth");
  36. $this->controller->__hook('index', $this->fakeRequest('/',
  37. ['groupAuth' => 1, 'groupParam' => 1]
  38. ), $response);
  39. $this->fail("test onRequest auth error fail");
  40. }
  41. public function testOnRequestParamError()
  42. {
  43. $response = $this->fakeResponse();
  44. $this->expectException(ParamValidateError::class);
  45. $this->expectExceptionMessage("validate fail for column onRequestParam");
  46. $this->controller->__hook('index', $this->fakeRequest('/',
  47. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1]
  48. ), $response);
  49. $this->fail("test onRequest param error fail");
  50. }
  51. public function testIndexAuthError()
  52. {
  53. $response = $this->fakeResponse();
  54. $this->expectException(ParamValidateError::class);
  55. $this->expectExceptionMessage("validate fail for column auth");
  56. $this->controller->__hook('index', $this->fakeRequest('/',
  57. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1]
  58. ), $response);
  59. $this->fail("test index auth error fail");
  60. }
  61. public function testIndexParamError()
  62. {
  63. $response = $this->fakeResponse();
  64. $this->expectException(ParamValidateError::class);
  65. $this->expectExceptionMessage("validate fail for column param");
  66. $this->controller->__hook('index', $this->fakeRequest('/',
  67. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1]
  68. ), $response);
  69. $this->fail("test index param error fail");
  70. }
  71. public function testSuccess()
  72. {
  73. $response = $this->fakeResponse();
  74. $this->controller->__hook('index', $this->fakeRequest('/',
  75. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1]
  76. ), $response);
  77. $this->assertEquals(
  78. json_encode([
  79. 'groupAuth' => 1,
  80. 'groupParam' => 1,
  81. 'onRequestAuth' => 1,
  82. 'onRequestParam' => 1,
  83. 'auth' => 1,
  84. 'param' => 1,
  85. 'groupParamA' => 'groupParamA',
  86. 'groupParamB' => 'groupParamB'
  87. ]),
  88. $response->getBody()->__toString());
  89. }
  90. public function testLessThanWithColumn()
  91. {
  92. $response = $this->fakeResponse();
  93. $this->controller->__hook('lessThanWithColumn', $this->fakeRequest('/',
  94. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1, 'foo' => 1, 'bar' => 2]
  95. ), $response);
  96. $this->assertTrue(true);
  97. $this->expectException(\Exception::class);
  98. $this->expectExceptionMessage("foo必须小于bar的值");
  99. $this->controller->__hook('lessThanWithColumn', $this->fakeRequest('/',
  100. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1, 'foo' => 3, 'bar' => 2]
  101. ), $response);
  102. $this->fail('test lessThanWithColumn fail');
  103. }
  104. public function testMbLengthWithColumn()
  105. {
  106. $response = $this->fakeResponse();
  107. $this->controller->__hook('mbLengthWithColumn', $this->fakeRequest('/',
  108. [
  109. 'groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1,
  110. 'columnA'=>'仙士可',
  111. 'columnB'=>'仙士3',
  112. 'columnC'=>'先12',
  113. 'columnD'=>'仙士4',
  114. ]
  115. ), $response);
  116. $this->assertTrue(true);
  117. }
  118. public function testGreaterThanWithColumn()
  119. {
  120. $response = $this->fakeResponse();
  121. $this->controller->__hook('greaterThanWithColumn', $this->fakeRequest('/',
  122. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1, 'foo' => 3, 'bar' => 2]
  123. ), $response);
  124. $this->assertTrue(true);
  125. $this->expectException(\Exception::class);
  126. $this->expectExceptionMessage("foo必须大于bar的值");
  127. $this->controller->__hook('greaterThanWithColumn', $this->fakeRequest('/',
  128. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1, 'foo' => 1, 'bar' => 2]
  129. ), $response);
  130. $this->fail('test lessThanWithColumn fail');
  131. }
  132. public function testDeprecated()
  133. {
  134. $response = $this->fakeResponse();
  135. $this->controller->__hook('deprecated', $this->fakeRequest('/',
  136. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1]
  137. ), $response);
  138. $response = $this->fakeResponse();
  139. $this->expectException(ParamValidateError::class);
  140. $this->expectExceptionMessage('validate fail for column foo');
  141. $this->controller->__hook('notDeprecated', $this->fakeRequest('/',
  142. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1]
  143. ), $response);
  144. $this->fail('test deprecated fail');
  145. }
  146. public function testParamType()
  147. {
  148. $response = $this->fakeResponse();
  149. $this->controller->__hook('paramType', $this->fakeRequest('/',
  150. ['groupAuth' => 1, 'groupParam' => 1, 'onRequestAuth' => 1, 'onRequestParam' => 1, 'auth' => 1, 'param' => 1,
  151. 'string' => 1,
  152. 'int' => '1',
  153. 'float' => '1',
  154. 'bool' => 1,
  155. 'json' => json_encode(['a' => 1, 'b' => 2]),
  156. 'array' => []
  157. ]
  158. ), $response);
  159. $this->assertEquals('success', $response->getBody()->__toString());
  160. }
  161. }