AnnotationControllerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests;
  3. use EasySwoole\Component\Context\ContextManager;
  4. use EasySwoole\Component\Di;
  5. use EasySwoole\Http\Dispatcher;
  6. use EasySwoole\Http\Request;
  7. use EasySwoole\Http\Response;
  8. use EasySwoole\HttpAnnotation\Exception\Annotation\MethodNotAllow;
  9. use EasySwoole\HttpAnnotation\Tests\TestController\Annotation;
  10. use EasySwoole\HttpAnnotation\Tests\TestController\RouterPath;
  11. use PHPUnit\Framework\TestCase;
  12. class AnnotationControllerTest extends TestCase
  13. {
  14. use ControllerBase;
  15. protected $controller;
  16. protected function setUp(): void
  17. {
  18. parent::setUp();
  19. $this->controller = new Annotation();
  20. ContextManager::getInstance()->set("context", 'context data');
  21. Di::getInstance()->set('di', 'di data');
  22. }
  23. function testDi()
  24. {
  25. $this->controller->__hook('index', $this->fakeRequest(), $this->fakeResponse());
  26. $this->assertEquals('di data', $this->controller->di);
  27. $this->controller->di = null;
  28. }
  29. function testContext()
  30. {
  31. $this->controller->__hook('index', $this->fakeRequest(), $this->fakeResponse());
  32. $this->assertEquals('context data', $this->controller->context);
  33. $this->controller->context = null;
  34. }
  35. function testGroupAuth()
  36. {
  37. $response = $this->fakeResponse();
  38. $this->controller->__hook('index', $this->fakeRequest('/', []), $response);
  39. $this->assertEquals('PE-groupParamA', $response->getBody()->__tostring());
  40. $response = $this->fakeResponse();
  41. $this->controller->__hook('index', $this->fakeRequest('/', null), $response);
  42. $this->assertEquals('index', $response->getBody()->__tostring());
  43. }
  44. function testParam1()
  45. {
  46. $response = $this->fakeResponse();
  47. $this->controller->__hook('param1', $this->fakeRequest('/', null), $response);
  48. $this->assertEquals('PE-param1', $response->getBody()->__tostring());
  49. $response = $this->fakeResponse();
  50. $this->controller->__hook('param1', $this->fakeRequest('/', null, ['param1' => 520]), $response);
  51. $this->assertEquals(520, $response->getBody()->__tostring());
  52. }
  53. function testParam2()
  54. {
  55. $response = $this->fakeResponse();
  56. $this->controller->__hook('param2', $this->fakeRequest('/', null), $response);
  57. $this->assertEquals('PE-param1', $response->getBody()->__tostring());
  58. $response = $this->fakeResponse();
  59. $this->controller->__hook('param2', $this->fakeRequest('/', null, ['param1' => 520, 'param2' => 520]), $response);
  60. $this->assertEquals(1040, $response->getBody()->__tostring());
  61. }
  62. function testParam3()
  63. {
  64. $response = $this->fakeResponse();
  65. $this->controller->__hook('param3', $this->fakeRequest('/', null), $response);
  66. $this->assertEquals('PE-groupParamA', $response->getBody()->__tostring());
  67. $response = $this->fakeResponse();
  68. $this->controller->__hook('param3', $this->fakeRequest('/', null, ['param1' => 520, 'groupParamA' => 520]), $response);
  69. $this->assertEquals(1040, $response->getBody()->__tostring());
  70. }
  71. function testParamExport1()
  72. {
  73. $response = $this->fakeResponse();
  74. $this->controller->__hook('paramExport1', $this->fakeRequest('/', null), $response);
  75. $this->assertEquals('groupParamA', $response->getBody()->__tostring());
  76. }
  77. function testParamExport2()
  78. {
  79. $response = $this->fakeResponse();
  80. $this->controller->__hook('paramExport2', $this->fakeRequest('/', null, ['exp' => "exp"]), $response);
  81. $this->assertEquals('exp', $response->getBody()->__tostring());
  82. }
  83. function testInjectParam1()
  84. {
  85. $response = $this->fakeResponse();
  86. $this->controller->__hook('injectParam1', $this->fakeRequest('/', null, ['param1' => "param1"]), $response);
  87. $this->assertEquals('groupParamA|groupParamB|param1', $response->getBody()->__tostring());
  88. }
  89. function testInjectParam2()
  90. {
  91. $response = $this->fakeResponse();
  92. $this->controller->__hook('injectParam2', $this->fakeRequest('/', null, ['param1' => "param1"]), $response);
  93. $this->assertEquals('param1', $response->getBody()->__tostring());
  94. }
  95. function testAllowPostMethod()
  96. {
  97. try {
  98. $this->controller->__hook('allowPostMethod', $this->fakeRequest(), $this->fakeResponse());
  99. } catch (\Throwable $throwable) {
  100. $this->assertInstanceOf(MethodNotAllow::class, $throwable);
  101. }
  102. $response = $this->fakeResponse();
  103. $request = $this->fakeRequest('/allowPostMethod', null, ['data' => 1]);
  104. $this->controller->__hook('allowPostMethod', $request, $response);
  105. $this->assertEquals('allowPostMethod', $response->getBody()->__tostring());
  106. }
  107. function testInject()
  108. {
  109. $response = $this->fakeResponse();
  110. $this->controller->__hook('inject', $this->fakeRequest(), $response);
  111. $this->assertEquals('inject test class -> index', $response->getBody()->__toString());
  112. $response = $this->fakeResponse();
  113. $this->controller->__hook('injectGetString', $this->fakeRequest(), $response);
  114. $this->assertEquals(1, $response->getBody()->__tostring());
  115. $response = $this->fakeResponse();
  116. $this->controller->__hook('injectGetArray', $this->fakeRequest(), $response);
  117. $this->assertEquals('[1,2]', $response->getBody()->__tostring());
  118. }
  119. function testRoutePath()
  120. {
  121. $response = $this->fakeResponse();
  122. $dispatcher = new Dispatcher('EasySwoole\HttpAnnotation\Tests\TestController');
  123. $dispatcher->dispatch($this->fakeRequest('/Router/test'), $response);
  124. $this->assertEquals('EasySwoole\HttpAnnotation\Tests\TestController\RouterPath::test', $response->getBody()->__toString());
  125. $response = $this->fakeResponse();
  126. $dispatcher = new Dispatcher('EasySwoole\HttpAnnotation\Tests\TestController');
  127. $dispatcher->dispatch($this->fakeRequest('/Router'), $response);
  128. $this->assertEquals('none', $response->getBody()->__toString());
  129. $response = $this->fakeResponse();
  130. $dispatcher = new Dispatcher('EasySwoole\HttpAnnotation\Tests\TestController');
  131. $dispatcher->dispatch($this->fakeRequest('/Router/aa'), $response);
  132. $this->assertEquals('not found!', $response->getBody()->__toString());
  133. $response = $this->fakeResponse();
  134. $dispatcher = new Dispatcher('EasySwoole\HttpAnnotation\Tests\TestController');
  135. $dispatcher->dispatch($this->fakeRequest('/ignore'), $response);
  136. $this->assertEquals('ignorePrefix', $response->getBody()->__toString());
  137. }
  138. }