DispatchTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace EasySwoole\Http\Tests;
  3. use EasySwoole\Http\AbstractInterface\AbstractRouter;
  4. use EasySwoole\Http\Dispatcher;
  5. use EasySwoole\Http\Message\Uri;
  6. use EasySwoole\Http\Request;
  7. use EasySwoole\Http\Response;
  8. use PHPUnit\Framework\TestCase;
  9. class DispatchTest extends TestCase
  10. {
  11. private $dispatcherWithRouter;
  12. private $dispatcher;
  13. function setUp(): void
  14. {
  15. $this->dispatcher = new Dispatcher('EasySwoole\Http\Tests\Controller');
  16. $this->dispatcherWithRouter = new Dispatcher('EasySwoole\Http\Tests\ControllerWithRouter');
  17. }
  18. function testIndex()
  19. {
  20. $response = new Response();
  21. $this->dispatcher->dispatch($this->getRequest('/'), $response);
  22. $this->assertEquals(200, $response->getStatusCode());
  23. $this->assertEquals('index', $response->getBody()->__toString());
  24. $response = new Response();
  25. $this->dispatcherWithRouter->dispatch($this->getRequest('/', 'GET'), $response);
  26. $this->assertEquals(200, $response->getStatusCode());
  27. $this->assertEquals('the router get /', $response->getBody()->__toString());
  28. $response = new Response();
  29. $this->dispatcherWithRouter->dispatch($this->getRequest('/', 'POST'), $response);
  30. $this->assertEquals(200, $response->getStatusCode());
  31. $this->assertEquals('index', $response->getBody()->__toString());
  32. $this->reset();
  33. $response = new Response();
  34. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  35. $router->setGlobalMode(true);
  36. $router->setMethodNotAllowCallBack(function (Request $request, Response $response) {
  37. $response->withStatus(405)->write('not allow');
  38. });
  39. });
  40. $this->dispatcherWithRouter->dispatch($this->getRequest('/', 'POST'), $response);
  41. $this->assertEquals(405, $response->getStatusCode());
  42. $this->assertEquals('not allow', $response->getBody()->__toString());
  43. }
  44. function testNotFound()
  45. {
  46. $response = new Response();
  47. $this->dispatcher->dispatch($this->getRequest('/test/test'), $response);
  48. $this->assertEquals(404, $response->getStatusCode());
  49. $this->assertEquals('test-404', $response->getBody()->__toString());
  50. $response = new Response();
  51. $this->dispatcher->dispatch($this->getRequest('/index/test'), $response);
  52. $this->assertEquals(404, $response->getStatusCode());
  53. $this->assertEquals('test-404', $response->getBody()->__toString());
  54. $response = new Response();
  55. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  56. $router->setPathInfoMode(false);
  57. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  58. $response->withStatus(404)->write('router not found');
  59. return false;
  60. });
  61. });
  62. $this->dispatcherWithRouter->dispatch($this->getRequest('/index', 'GET'), $response);
  63. $this->assertEquals(404, $response->getStatusCode());
  64. $this->assertEquals('router not found', $response->getBody()->__toString());
  65. }
  66. function testException()
  67. {
  68. $response = new Response();
  69. $this->dispatcher->dispatch($this->getRequest('/index/exception'), $response);
  70. $this->assertEquals(500, $response->getStatusCode());
  71. $this->assertEquals('error-the error', $response->getBody()->__toString());
  72. $this->reset();
  73. $response = new Response();
  74. $this->dispatcherWithRouter->dispatch($this->getRequest('/index/exception'), $response);
  75. $this->assertEquals(500, $response->getStatusCode());
  76. $this->assertEquals('error-the error', $response->getBody()->__toString());
  77. }
  78. function testHttpExceptionHandler()
  79. {
  80. $this->dispatcher->setHttpExceptionHandler(function (\Throwable $throwable, Request $request, Response $response) {
  81. $response->withStatus(500)->write('the exception handler');
  82. });
  83. $response = new Response();
  84. $this->dispatcher->dispatch($this->getRequest('/index/httpExceptionHandler'), $response);
  85. $this->assertEquals(500, $response->getStatusCode());
  86. $this->assertEquals('the exception handler', $response->getBody()->__toString());
  87. $this->reset();
  88. $response = new Response();
  89. $this->dispatcherWithRouter->setHttpExceptionHandler(function (\Throwable $throwable, Request $request, Response $response) {
  90. $response->withStatus(500)->write('the exception handler');
  91. return false;
  92. });
  93. $this->dispatcherWithRouter->dispatch($this->getRequest('/index/httpExceptionHandler'), $response);
  94. $this->assertEquals(500, $response->getStatusCode());
  95. $this->assertEquals('the exception handler', $response->getBody()->__toString());
  96. }
  97. public function testAddRouter()
  98. {
  99. $this->reset();
  100. $response = new Response();
  101. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  102. $router->setGlobalMode(true);
  103. $router->setPathInfoMode(false);
  104. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  105. $response->withStatus(404)->write('router not found');
  106. });
  107. });
  108. $this->dispatcherWithRouter->dispatch($this->getRequest('/index'), $response);
  109. $this->assertEquals(404, $response->getStatusCode());
  110. $this->assertEquals('router not found', $response->getBody()->__toString());
  111. $this->reset();
  112. $response = new Response();
  113. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  114. $router->setGlobalMode(true);
  115. $router->setPathInfoMode(false);
  116. $router->getRouteCollector()->get('/index', function (Request $request, Response $response) {
  117. $response->write('the route is add index');
  118. });
  119. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  120. $response->withStatus(404)->write('router not found');
  121. });
  122. });
  123. $this->dispatcherWithRouter->dispatch($this->getRequest('/index'), $response);
  124. $this->assertEquals(200, $response->getStatusCode());
  125. $this->assertEquals('the route is add index', $response->getBody()->__toString());
  126. }
  127. public function testRedirect()
  128. {
  129. $this->reset();
  130. $response = new Response();
  131. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  132. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  133. $response->withStatus(404)->write('the 404-');
  134. });
  135. });
  136. $this->dispatcherWithRouter->dispatch($this->getRequest('/index/test'), $response);
  137. $this->assertEquals(404, $response->getStatusCode());
  138. $this->assertEquals('the 404-test-404', $response->getBody()->__toString());
  139. $this->reset();
  140. $response = new Response();
  141. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  142. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  143. $response->withStatus(404)->write('the 404-');
  144. return false;
  145. });
  146. });
  147. $this->dispatcherWithRouter->dispatch($this->getRequest('/index/test'), $response);
  148. $this->assertEquals(404, $response->getStatusCode());
  149. $this->assertEquals('the 404-', $response->getBody()->__toString());
  150. $this->reset();
  151. $response = new Response();
  152. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  153. $router->setRouterNotFoundCallBack(function (Request $request, Response $response) {
  154. return '/index/index';
  155. });
  156. });
  157. $this->dispatcherWithRouter->dispatch($this->getRequest('/index/test'), $response);
  158. $this->assertEquals(200, $response->getStatusCode());
  159. $this->assertEquals('index', $response->getBody()->__toString());
  160. }
  161. public function testParseParams()
  162. {
  163. // get
  164. $this->reset();
  165. $response = new Response();
  166. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  167. $router->parseParams($router::PARSE_PARAMS_IN_GET);
  168. });
  169. $this->dispatcherWithRouter->dispatch($this->getRequest('/user/gaobinzhan'), $response);
  170. $this->assertEquals(200, $response->getStatusCode());
  171. $this->assertEquals('{"get":{"name":"gaobinzhan"},"post":[],"context":[]}', $response->getBody()->__toString());
  172. // post
  173. $this->reset();
  174. $response = new Response();
  175. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  176. $router->parseParams($router::PARSE_PARAMS_IN_POST);
  177. });
  178. $this->dispatcherWithRouter->dispatch($this->getRequest('/user/gaobinzhan'), $response);
  179. $this->assertEquals(200, $response->getStatusCode());
  180. $this->assertEquals('{"get":[],"post":{"name":"gaobinzhan"},"context":null}', $response->getBody()->__toString());
  181. // context
  182. $this->reset();
  183. $response = new Response();
  184. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  185. $router->parseParams($router::PARSE_PARAMS_IN_CONTEXT);
  186. });
  187. $this->dispatcherWithRouter->dispatch($this->getRequest('/user/gaobinzhan'), $response);
  188. $this->assertEquals(200, $response->getStatusCode());
  189. $this->assertEquals('{"get":[],"post":[],"context":{"name":"gaobinzhan"}}', $response->getBody()->__toString());
  190. // none
  191. $this->reset();
  192. $response = new Response();
  193. $this->dispatcherWithRouter->setOnRouterCreate(function (AbstractRouter $router) {
  194. $router->parseParams($router::PARSE_PARAMS_NONE);
  195. });
  196. $this->dispatcherWithRouter->dispatch($this->getRequest('/user/gaobinzhan'), $response);
  197. $this->assertEquals(200, $response->getStatusCode());
  198. $this->assertEquals('{"get":[],"post":[],"context":null}', $response->getBody()->__toString());
  199. }
  200. private function reset()
  201. {
  202. $this->dispatcher = new Dispatcher('EasySwoole\Http\Tests\Controller');
  203. $this->dispatcherWithRouter = new Dispatcher('EasySwoole\Http\Tests\ControllerWithRouter');
  204. }
  205. private function getRequest($url, $method = 'GET', array $postData = null)
  206. {
  207. $request = new Request();
  208. $request->withMethod($method);
  209. $request->withUri(new Uri($url));
  210. return $request;
  211. }
  212. }