| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Config;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Config\Config;
- class ConfigTest extends TestCase
- {
- public function testConstructorWithValidParameters(): void
- {
- $config = new Config(
- sid: 'test_sid',
- appKey: 'test_app_key',
- appSecret: 'test_app_secret',
- baseUrl: 'https://api.example.com',
- timeout: 30,
- debug: true,
- logFile: '/tmp/test.log'
- );
- $this->assertEquals('test_sid', $config->sid);
- $this->assertEquals('test_app_key', $config->appKey);
- $this->assertEquals('test_app_secret', $config->appSecret);
- $this->assertEquals('https://api.example.com', $config->baseUrl);
- $this->assertEquals(30, $config->timeout);
- $this->assertTrue($config->debug);
- $this->assertEquals('/tmp/test.log', $config->logFile);
- }
- public function testConstructorWithDefaults(): void
- {
- $config = new Config(
- sid: 'test_sid',
- appKey: 'test_app_key',
- appSecret: 'test_app_secret'
- );
- $this->assertEquals('test_sid', $config->sid);
- $this->assertEquals('test_app_key', $config->appKey);
- $this->assertEquals('test_app_secret', $config->appSecret);
- $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
- $this->assertEquals(30, $config->timeout);
- $this->assertFalse($config->debug);
- $this->assertNull($config->logFile);
- }
- public function testSandboxFactory(): void
- {
- $config = Config::sandbox('test_sid', 'test_app_key', 'test_app_secret');
- $this->assertEquals('test_sid', $config->sid);
- $this->assertEquals('test_app_key', $config->appKey);
- $this->assertEquals('test_app_secret', $config->appSecret);
- $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
- $this->assertTrue($config->debug);
- $this->assertTrue($config->isSandbox());
- }
- public function testProductionFactory(): void
- {
- $config = Config::production('test_sid', 'test_app_key', 'test_app_secret');
- $this->assertEquals('test_sid', $config->sid);
- $this->assertEquals('test_app_key', $config->appKey);
- $this->assertEquals('test_app_secret', $config->appSecret);
- $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
- $this->assertFalse($config->debug);
- $this->assertFalse($config->isSandbox());
- }
- public function testGetEndpoint(): void
- {
- $config = new Config('test_sid', 'test_app_key', 'test_app_secret', 'https://api.example.com');
- $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('test.php'));
- $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('/test.php'));
- }
- public function testGetEndpointWithTrailingSlash(): void
- {
- $config = new Config('test_sid', 'test_app_key', 'test_app_secret', 'https://api.example.com/');
- $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('test.php'));
- $this->assertEquals('https://api.example.com/test.php', $config->getEndpoint('/test.php'));
- }
- public function testIsSandbox(): void
- {
- $sandboxConfig = new Config('sid', 'key', 'secret', Config::SANDBOX_BASE_URL);
- $productionConfig = new Config('sid', 'key', 'secret', Config::PRODUCTION_BASE_URL);
- $customSandboxConfig = new Config('sid', 'key', 'secret', 'https://sandbox.test.com');
- $this->assertTrue($sandboxConfig->isSandbox());
- $this->assertFalse($productionConfig->isSandbox());
- $this->assertTrue($customSandboxConfig->isSandbox());
- }
- public function testValidationWithEmptySid(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('SID cannot be empty');
- new Config('', 'test_app_key', 'test_app_secret');
- }
- public function testValidationWithEmptyAppKey(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('App Key cannot be empty');
- new Config('test_sid', '', 'test_app_secret');
- }
- public function testValidationWithEmptyAppSecret(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('App Secret cannot be empty');
- new Config('test_sid', 'test_app_key', '');
- }
- public function testValidationWithInvalidTimeout(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Timeout must be greater than 0');
- new Config('test_sid', 'test_app_key', 'test_app_secret', Config::SANDBOX_BASE_URL, 0);
- }
- public function testValidationWithNegativeTimeout(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Timeout must be greater than 0');
- new Config('test_sid', 'test_app_key', 'test_app_secret', Config::SANDBOX_BASE_URL, -1);
- }
- public function testValidationWithInvalidBaseUrl(): void
- {
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage('Invalid base URL');
- new Config('test_sid', 'test_app_key', 'test_app_secret', 'invalid-url');
- }
- public function testConstants(): void
- {
- $this->assertEquals('https://sandbox.wangdian.cn/openapi2', Config::SANDBOX_BASE_URL);
- $this->assertEquals('https://www.wangdian.cn/openapi2', Config::PRODUCTION_BASE_URL);
- }
- }
|