ConfigTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit\Config;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Config\Config;
  6. class ConfigTest extends TestCase
  7. {
  8. public function testConstructorWithValidParameters(): void
  9. {
  10. $config = new Config(
  11. sid: 'test_sid',
  12. appKey: 'test_app_key',
  13. appSecret: 'test_app_secret',
  14. baseUrl: 'https://api.example.com',
  15. timeout: 30,
  16. debug: true,
  17. logFile: '/tmp/test.log'
  18. );
  19. $this->assertEquals('test_sid', $config->sid);
  20. $this->assertEquals('test_app_key', $config->appKey);
  21. $this->assertEquals('test_app_secret', $config->appSecret);
  22. $this->assertEquals('https://api.example.com', $config->baseUrl);
  23. $this->assertEquals(30, $config->timeout);
  24. $this->assertTrue($config->debug);
  25. $this->assertEquals('/tmp/test.log', $config->logFile);
  26. }
  27. public function testConstructorWithDefaults(): void
  28. {
  29. $config = new Config(
  30. sid: 'test_sid',
  31. appKey: 'test_app_key',
  32. appSecret: 'test_app_secret'
  33. );
  34. $this->assertEquals('test_sid', $config->sid);
  35. $this->assertEquals('test_app_key', $config->appKey);
  36. $this->assertEquals('test_app_secret', $config->appSecret);
  37. $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
  38. $this->assertEquals(30, $config->timeout);
  39. $this->assertFalse($config->debug);
  40. $this->assertNull($config->logFile);
  41. }
  42. public function testSandboxFactory(): void
  43. {
  44. $config = Config::sandbox('test_sid', 'test_app_key', 'test_app_secret');
  45. $this->assertEquals('test_sid', $config->sid);
  46. $this->assertEquals('test_app_key', $config->appKey);
  47. $this->assertEquals('test_app_secret', $config->appSecret);
  48. $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
  49. $this->assertTrue($config->debug);
  50. $this->assertTrue($config->isSandbox());
  51. }
  52. public function testProductionFactory(): void
  53. {
  54. $config = Config::production('test_sid', 'test_app_key', 'test_app_secret');
  55. $this->assertEquals('test_sid', $config->sid);
  56. $this->assertEquals('test_app_key', $config->appKey);
  57. $this->assertEquals('test_app_secret', $config->appSecret);
  58. $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
  59. $this->assertFalse($config->debug);
  60. $this->assertFalse($config->isSandbox());
  61. }
  62. public function testGetEndpoint(): void
  63. {
  64. $config = new Config('test_sid', 'test_app_key', 'test_app_secret', 'https://api.example.com');
  65. $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('test.php'));
  66. $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('/test.php'));
  67. }
  68. public function testGetEndpointWithTrailingSlash(): void
  69. {
  70. $config = new Config('test_sid', 'test_app_key', 'test_app_secret', 'https://api.example.com/');
  71. $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('test.php'));
  72. $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('/test.php'));
  73. }
  74. public function testIsSandbox(): void
  75. {
  76. $sandboxConfig = new Config('sid', 'key', 'secret', Config::SANDBOX_BASE_URL);
  77. $productionConfig = new Config('sid', 'key', 'secret', Config::PRODUCTION_BASE_URL);
  78. $customSandboxConfig = new Config('sid', 'key', 'secret', 'https://sandbox.test.com');
  79. $this->assertTrue($sandboxConfig->isSandbox());
  80. $this->assertFalse($productionConfig->isSandbox());
  81. $this->assertTrue($customSandboxConfig->isSandbox());
  82. }
  83. public function testValidationWithEmptySid(): void
  84. {
  85. $this->expectException(\InvalidArgumentException::class);
  86. $this->expectExceptionMessage('SID cannot be empty');
  87. new Config('', 'test_app_key', 'test_app_secret');
  88. }
  89. public function testValidationWithEmptyAppKey(): void
  90. {
  91. $this->expectException(\InvalidArgumentException::class);
  92. $this->expectExceptionMessage('App Key cannot be empty');
  93. new Config('test_sid', '', 'test_app_secret');
  94. }
  95. public function testValidationWithEmptyAppSecret(): void
  96. {
  97. $this->expectException(\InvalidArgumentException::class);
  98. $this->expectExceptionMessage('App Secret cannot be empty');
  99. new Config('test_sid', 'test_app_key', '');
  100. }
  101. public function testValidationWithInvalidTimeout(): void
  102. {
  103. $this->expectException(\InvalidArgumentException::class);
  104. $this->expectExceptionMessage('Timeout must be greater than 0');
  105. new Config('test_sid', 'test_app_key', 'test_app_secret', Config::SANDBOX_BASE_URL, 0);
  106. }
  107. public function testValidationWithNegativeTimeout(): void
  108. {
  109. $this->expectException(\InvalidArgumentException::class);
  110. $this->expectExceptionMessage('Timeout must be greater than 0');
  111. new Config('test_sid', 'test_app_key', 'test_app_secret', Config::SANDBOX_BASE_URL, -1);
  112. }
  113. public function testValidationWithInvalidBaseUrl(): void
  114. {
  115. $this->expectException(\InvalidArgumentException::class);
  116. $this->expectExceptionMessage('Invalid base URL');
  117. new Config('test_sid', 'test_app_key', 'test_app_secret', 'invalid-url');
  118. }
  119. public function testConstants(): void
  120. {
  121. $this->assertEquals('https://sandbox.wangdian.cn/openapi2', Config::SANDBOX_BASE_URL);
  122. $this->assertEquals('https://www.wangdian.cn/openapi2', Config::PRODUCTION_BASE_URL);
  123. }
  124. }