NormalControllerTest.php 2.9 KB

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