NoneAnnotationControllerTest.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace EasySwoole\HttpAnnotation\Tests;
  3. use EasySwoole\HttpAnnotation\AnnotationTag\Di;
  4. use EasySwoole\HttpAnnotation\Tests\TestController\NoneAnnotation;
  5. use PHPUnit\Framework\TestCase;
  6. class NoneAnnotationControllerTest extends TestCase
  7. {
  8. use ControllerBase;
  9. protected $controller;
  10. protected function setUp():void
  11. {
  12. parent::setUp();
  13. $this->controller = new NoneAnnotation();
  14. }
  15. function testIndex()
  16. {
  17. $response = $this->fakeResponse();
  18. $this->controller->__hook('index',$this->fakeRequest('/',null),$response);
  19. $this->assertEquals('index',$response->getBody()->__tostring());
  20. }
  21. function testOnRequest()
  22. {
  23. $response = $this->fakeResponse();
  24. $this->controller->__hook('testOnRequest',$this->fakeRequest('/',null),$response);
  25. $this->assertEquals('testOnRequest',$response->getBody()->__tostring());
  26. }
  27. function testProtectFunc()
  28. {
  29. $response = $this->fakeResponse();
  30. $this->controller->__hook('noneAction',$this->fakeRequest('/',null),$response);
  31. $this->assertEquals('404',$response->getBody()->__tostring());
  32. }
  33. function test404()
  34. {
  35. $response = $this->fakeResponse();
  36. $this->controller->__hook('xxxxxxx',$this->fakeRequest('/',null),$response);
  37. $this->assertEquals('404',$response->getBody()->__tostring());
  38. }
  39. function testException()
  40. {
  41. $response = $this->fakeResponse();
  42. $this->controller->__hook('exception',$this->fakeRequest('/',null),$response);
  43. $this->assertEquals('exception',$response->getBody()->__tostring());
  44. }
  45. function testGc()
  46. {
  47. $response = $this->fakeResponse();
  48. $this->controller->__hook('index',$this->fakeRequest('/',null),$response);
  49. $this->assertEquals('index',$response->getBody()->__tostring());
  50. $this->assertEquals(1,$this->controller->gc);
  51. $response = $this->fakeResponse();
  52. $this->controller->__hook('exception',$this->fakeRequest('/',null),$response);
  53. $this->assertEquals('exception',$response->getBody()->__tostring());
  54. $this->assertEquals(1,$this->controller->gc);
  55. }
  56. function testAfterAction()
  57. {
  58. \EasySwoole\Component\Di::getInstance()->set('afterAction',null);
  59. $response = $this->fakeResponse();
  60. $this->controller->__hook('index',$this->fakeRequest('/',null),$response);
  61. $this->assertEquals('afterAction',\EasySwoole\Component\Di::getInstance()->get('afterAction'));
  62. \EasySwoole\Component\Di::getInstance()->set('afterAction',null);
  63. \EasySwoole\Component\Di::getInstance()->set('afterAction',null);
  64. $response = $this->fakeResponse();
  65. $this->controller->__hook('exception',$this->fakeRequest('/',null),$response);
  66. $this->assertEquals('afterAction',\EasySwoole\Component\Di::getInstance()->get('afterAction'));
  67. \EasySwoole\Component\Di::getInstance()->set('afterAction',null);
  68. }
  69. }