TableTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace EasySwoole\Config\Test;
  3. use EasySwoole\Config\TableConfig;
  4. use PHPUnit\Framework\TestCase;
  5. class TableTest extends TestCase
  6. {
  7. protected $config;
  8. public function setUp()/* The :void return type declaration that should be here would cause a BC issue */
  9. {
  10. $this->config = new \EasySwoole\Config\TableConfig();
  11. }
  12. private function getConfigObj(): TableConfig
  13. {
  14. return $this->config;
  15. }
  16. private function loadConfig()
  17. {
  18. $config = [
  19. 'SERVER_NAME' => "EasySwoole",//服务名
  20. 'MAIN_SERVER' => [
  21. 'LISTEN_ADDRESS' => '0.0.0.0',//监听地址
  22. 'PORT' => 9901,//监听端口
  23. 'SERVER_TYPE' => 'EASYSWOOLE_WEB_SERVER', //可选为 EASYSWOOLE_SERVER EASYSWOOLE_WEB_SERVER EASYSWOOLE_WEB_SOCKET_SERVER
  24. 'SOCK_TYPE' => 'SWOOLE_TCP',//该配置项当为SERVER_TYPE值为TYPE_SERVER时有效
  25. 'RUN_MODEL' => 'SWOOLE_PROCESS',// 默认Server的运行模式
  26. 'SETTING' => [// Swoole Server的运行配置( 完整配置可见[Swoole文档](https://wiki.swoole.com/wiki/page/274.html) )
  27. 'worker_num' => 8,//运行的 worker进程数量
  28. 'max_request' => 5000,// worker 完成该数量的请求后将退出,防止内存溢出
  29. 'task_worker_num' => 8,//运行的 task_worker 进程数量
  30. 'task_max_request' => 1000// task_worker 完成该数量的请求后将退出,防止内存溢出
  31. ]
  32. ],
  33. 'TEMP_DIR' => null,//临时文件存放的目录
  34. 'LOG_DIR' => null,//日志文件存放的目录;
  35. ];
  36. $this->getConfigObj()->load($config);
  37. }
  38. public function testSetConf()
  39. {
  40. $this->assertNull($this->getConfigObj()->getConf('SERVER_NAME'));
  41. $this->getConfigObj()->setConf('SERVER_NAME', 'EaswSwoole');
  42. $this->getConfigObj()->setConf('MAIN_SERVER.PORT', 9999);
  43. $this->assertEquals('EaswSwoole', $this->getConfigObj()->getConf('SERVER_NAME'));
  44. $this->assertEquals(9999, $this->getConfigObj()->getConf('MAIN_SERVER.PORT'));
  45. }
  46. public function testGetConf()
  47. {
  48. $this->loadConfig();
  49. $this->assertEquals('EasySwoole', $this->getConfigObj()->getConf('SERVER_NAME'));
  50. $this->assertEquals(9901, $this->getConfigObj()->getConf('MAIN_SERVER.PORT'));
  51. $this->assertEquals(8, $this->getConfigObj()->getConf('MAIN_SERVER.SETTING.worker_num'));
  52. }
  53. public function testMerge()
  54. {
  55. $this->loadConfig();
  56. $this->assertEquals('EasySwoole', $this->getConfigObj()->getConf('SERVER_NAME'));
  57. $arr = ['SERVER_NAME' => 'bb', 'MAIN_SERVER' => ['MAIN_SERVER' => 9903]];
  58. $this->getConfigObj()->merge($arr);
  59. $this->assertEquals('bb', $this->getConfigObj()->getConf('SERVER_NAME'));
  60. $this->assertEquals('9903', $this->getConfigObj()->getConf('MAIN_SERVER.MAIN_SERVER'));
  61. }
  62. public function testClear()
  63. {
  64. $this->loadConfig();
  65. $this->getConfigObj()->clear();
  66. $arr = $this->getConfigObj()->getConf();
  67. $this->assertEmpty($arr);
  68. }
  69. }