ToolsTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Utils;
  4. use MaxMind\Db\Reader\InvalidDatabaseException;
  5. use PHPUnit\Framework\TestCase;
  6. use function date_default_timezone_set;
  7. class ToolsTest extends TestCase
  8. {
  9. /**
  10. * @covers App\Utils\Tools::getIpLocation
  11. * @throws InvalidDatabaseException
  12. */
  13. public function testGetIpLocation()
  14. {
  15. $_ENV['maxmind_license_key'] = '';
  16. $msg = Tools::getIpLocation('8.8.8.8');
  17. $this->assertIsString($msg);
  18. $this->assertEquals('GeoIP2 服务未配置', $msg);
  19. }
  20. /**
  21. * @covers App\Utils\Tools::autoBytes
  22. */
  23. public function testAutoBytes()
  24. {
  25. $size = 1024;
  26. $bytes = Tools::autoBytes($size);
  27. $this->assertIsString($bytes);
  28. $this->assertEquals('1KB', $bytes);
  29. }
  30. /**
  31. * @covers App\Utils\Tools::autoBytesR
  32. */
  33. public function testAutoBytesR()
  34. {
  35. $size = '1KB';
  36. $bytes = Tools::autoBytesR($size);
  37. $this->assertIsInt($bytes);
  38. $this->assertEquals(1024, $bytes);
  39. }
  40. /**
  41. * @covers App\Utils\Tools::autoMbps
  42. */
  43. public function testAutoMbps()
  44. {
  45. $bandwidth = 1;
  46. $mbps = Tools::autoMbps($bandwidth);
  47. $this->assertIsString($mbps);
  48. $this->assertEquals('1Mbps', $mbps);
  49. }
  50. /**
  51. * @covers App\Utils\Tools::toMB
  52. */
  53. public function testToMB()
  54. {
  55. $traffic = 1;
  56. $mb = 1048576;
  57. $result = Tools::toMB($traffic);
  58. $this->assertIsInt($result);
  59. $this->assertEquals($traffic * $mb, $result);
  60. }
  61. /**
  62. * @covers App\Utils\Tools::toGB
  63. */
  64. public function testToGB()
  65. {
  66. $traffic = 1;
  67. $gb = 1048576 * 1024;
  68. $result = Tools::toGB($traffic);
  69. $this->assertIsInt($result);
  70. $this->assertEquals($traffic * $gb, $result);
  71. }
  72. /**
  73. * @covers App\Utils\Tools::flowToGB
  74. */
  75. public function testFlowToGB()
  76. {
  77. $traffic = 1048576 * 1024;
  78. $gb = 1048576 * 1024;
  79. $result = Tools::flowToGB($traffic);
  80. $this->assertIsFloat($result);
  81. $this->assertEquals($traffic / $gb, $result);
  82. }
  83. /**
  84. * @covers App\Utils\Tools::flowToMB
  85. */
  86. public function testFlowToMB()
  87. {
  88. $traffic = 1048576;
  89. $mb = 1048576;
  90. $result = Tools::flowToMB($traffic);
  91. $this->assertIsFloat($result);
  92. $this->assertEquals($traffic / $mb, $result);
  93. }
  94. /**
  95. * @covers App\Utils\Tools::genRandomChar
  96. */
  97. public function testGenRandomChar()
  98. {
  99. $length = 10;
  100. $randomString = Tools::genRandomChar($length);
  101. $this->assertIsString($randomString);
  102. $this->assertEquals($length, strlen($randomString));
  103. }
  104. /**
  105. * @covers App\Utils\Tools::genSs2022UserPk
  106. */
  107. public function testGenSs2022UserPk()
  108. {
  109. $passwd = 'password';
  110. $length = 16;
  111. $pk = Tools::genSs2022UserPk($passwd, $length);
  112. $this->assertIsString($pk);
  113. $this->assertEquals('NWU4ODQ4OThkYTI4MDQ3MQ==', $pk);
  114. }
  115. /**
  116. * @covers App\Utils\Tools::toDateTime
  117. */
  118. public function testToDateTime()
  119. {
  120. date_default_timezone_set('ROC'); // Use Asia/Shanghai or PRC will cause this test to fail
  121. $time = 612907200; // 1989-06-04 04:00:00 UTC+8
  122. $expected = '1989-06-04 04:00:00';
  123. $result = Tools::toDateTime($time);
  124. $this->assertIsString($result);
  125. $this->assertEquals($expected, $result);
  126. }
  127. /**
  128. * @covers App\Utils\Tools::isParamValidate
  129. */
  130. public function testIsParamValidate()
  131. {
  132. $this->assertTrue(Tools::isParamValidate('default', 'aes-128-gcm'));
  133. $this->assertFalse(Tools::isParamValidate('default', 'rc4-md5'));
  134. }
  135. /**
  136. * @covers App\Utils\Tools::isEmail
  137. */
  138. public function testIsEmail()
  139. {
  140. $this->assertTrue(Tools::isEmail('[email protected]'));
  141. $this->assertFalse(Tools::isEmail('test@example'));
  142. }
  143. /**
  144. * @covers App\Utils\Tools::isEmailLegal
  145. */
  146. public function testIsEmailLegal()
  147. {
  148. $_ENV['mail_filter'] = 1;
  149. $_ENV['mail_filter_list'] = ['example.com'];
  150. $email1 = '[email protected]';
  151. $email2 = '[email protected]';
  152. $expected1 = ['ret' => 1];
  153. $expected2 = ['ret' => 0, 'msg' => '邮箱域名 example.org 无效,请更换邮件地址'];
  154. $this->assertEquals($expected1, Tools::isEmailLegal($email1));
  155. $this->assertEquals($expected2, Tools::isEmailLegal($email2));
  156. }
  157. /**
  158. * @covers App\Utils\Tools::isIPv4
  159. */
  160. public function testIsIPv4()
  161. {
  162. $this->assertTrue(Tools::isIPv4('192.168.0.1'));
  163. $this->assertFalse(Tools::isIPv4('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  164. }
  165. /**
  166. * @covers App\Utils\Tools::isIPv6
  167. */
  168. public function testIsIPv6()
  169. {
  170. $this->assertTrue(Tools::isIPv6('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
  171. $this->assertFalse(Tools::isIPv6('192.168.0.1'));
  172. }
  173. /**
  174. * @covers App\Utils\Tools::isInt
  175. */
  176. public function testIsInt()
  177. {
  178. $this->assertTrue(Tools::isInt(123));
  179. $this->assertFalse(Tools::isInt('abc'));
  180. }
  181. /**
  182. * @covers App\Utils\Tools::isJson
  183. */
  184. public function testIsJson()
  185. {
  186. $this->assertTrue(Tools::isJson('{}'));
  187. $this->assertFalse(Tools::isJson('[]'));
  188. }
  189. }