CoreLibsTest.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. namespace Luolongfei\Tests\Libs;
  4. use Colors\Color;
  5. use Luolongfei\Libs\Argv;
  6. use Luolongfei\Libs\Base;
  7. use Luolongfei\Libs\Config;
  8. use Luolongfei\Libs\Env;
  9. use Luolongfei\Libs\IP;
  10. use Luolongfei\Libs\Lang;
  11. use Luolongfei\Libs\Log;
  12. use Luolongfei\Libs\PhpColor;
  13. use Luolongfei\Tests\TestCase;
  14. use Monolog\Logger;
  15. class DummySingleton extends Base
  16. {
  17. public array $params = [];
  18. public function init(...$params)
  19. {
  20. $this->params = $params;
  21. }
  22. }
  23. class TestableIP extends IP
  24. {
  25. public function __construct()
  26. {
  27. }
  28. public function parse(string $body): bool
  29. {
  30. return $this->setIpInfo($body);
  31. }
  32. }
  33. final class CoreLibsTest extends TestCase
  34. {
  35. public function testBaseSingletonInitializesOnce(): void
  36. {
  37. $first = DummySingleton::getInstance('foo', 'bar');
  38. $second = DummySingleton::getInstance('baz');
  39. $this->assertSame($first, $second);
  40. $this->assertSame(['foo', 'bar'], $first->params);
  41. }
  42. public function testEnvConfigAndLangReadFixtureValues(): void
  43. {
  44. $this->loadFixtureEnv([
  45. 'FEATURE_FLAG' => 'true',
  46. 'EMPTY_VAL' => '(empty)',
  47. 'NULL_VAL' => '(null)',
  48. 'QUOTED_VAL' => '"quoted"',
  49. ]);
  50. $this->assertTrue(Env::getInstance()->get('FEATURE_FLAG'));
  51. $this->assertSame('', Env::getInstance()->get('EMPTY_VAL'));
  52. $this->assertNull(Env::getInstance()->get('NULL_VAL'));
  53. $this->assertSame('quoted', Env::getInstance()->get('QUOTED_VAL'));
  54. $this->assertSame('api.telegram.org', Config::getInstance()->get('message.telegram.host'));
  55. $this->assertSame('fallback', Config::getInstance()->get('missing.value', 'fallback'));
  56. $this->assertNotNull(Lang::getInstance()->get('100064'));
  57. $this->assertSame(lang('100064'), Lang::getInstance()->get('100064'));
  58. }
  59. public function testArgvParsesNamedArguments(): void
  60. {
  61. global $argv;
  62. $argv = ['run', '--foo=bar', '-num=3', '--flag'];
  63. $arg = $this->makeWithoutConstructor(Argv::class);
  64. $parsed = $arg->parseAllArgs();
  65. $this->assertSame('bar', $parsed['foo']);
  66. $this->assertSame('3', $parsed['num']);
  67. $this->assertTrue($parsed['flag']);
  68. $this->assertSame('bar', $arg->get('foo'));
  69. }
  70. public function testIpParsingSupportsChineseAndEnglishSources(): void
  71. {
  72. $ip = new TestableIP();
  73. $this->loadFixtureEnv(['CUSTOM_LANGUAGE' => 'zh']);
  74. $ip->parse("当前 IP:1.2.3.4 来自于:中国 广东 深圳 ");
  75. $this->assertSame('1.2.3.4', IP::$ip);
  76. $this->assertSame('中国 广东 深圳', IP::$loc);
  77. $this->loadFixtureEnv(['CUSTOM_LANGUAGE' => 'en']);
  78. $ip->parse('{"ip":"5.6.7.8","country":"US","region":"CA","city":"San Francisco"}');
  79. $this->assertSame('5.6.7.8', IP::$ip);
  80. $this->assertSame('US CA San Francisco', IP::$loc);
  81. }
  82. public function testLogAndPhpColorExposeUsableInstances(): void
  83. {
  84. $this->assertTrue(Log::info('info message'));
  85. $this->assertTrue(Log::error('error message'));
  86. $this->assertInstanceOf(Logger::class, Log::logger());
  87. $this->assertInstanceOf(Color::class, PhpColor::getInstance()->getColorInstance());
  88. }
  89. }