SplArrayTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace EasySwoole\Config\Test;
  3. use EasySwoole\Config\SplArrayConfig;
  4. use PHPUnit\Framework\TestCase;
  5. class SplArrayTest 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\SplArrayConfig();
  11. }
  12. private function getConfigObj(): SplArrayConfig
  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->assertEquals(null, $this->getConfigObj()->getConf('TEMP_DIR'));
  41. $this->getConfigObj()->setConf('TEMP_DIR', '/Temp');
  42. $this->assertEquals('/Temp', $this->getConfigObj()->getConf('TEMP_DIR'));
  43. }
  44. public function testGetConf()
  45. {
  46. $this->loadConfig();
  47. $this->assertEquals('EasySwoole', $this->getConfigObj()->getConf('SERVER_NAME'));
  48. $this->assertEquals(9901, $this->getConfigObj()->getConf('MAIN_SERVER.PORT'));
  49. $this->assertEquals(8, $this->getConfigObj()->getConf('MAIN_SERVER.SETTING.worker_num'));
  50. }
  51. public function testMerge()
  52. {
  53. $this->loadConfig();
  54. $this->assertEquals('EasySwoole', $this->getConfigObj()->getConf('SERVER_NAME'));
  55. $arr = ['SERVER_NAME' => 'bb'];
  56. $this->getConfigObj()->merge($arr);
  57. $this->assertEquals('bb', $this->getConfigObj()->getConf('SERVER_NAME'));
  58. }
  59. public function testClear()
  60. {
  61. $this->loadConfig();
  62. $this->getConfigObj()->clear();
  63. $arr = $this->getConfigObj()->getConf();
  64. $this->assertEmpty($arr);
  65. }
  66. }