CaiWuXiTong/tests/Unit/EsignConfigurationTest.php

76 lines
2.5 KiB
PHP

<?php
namespace Tests\Unit;
use App\Services\Esign\EsignConfiguration;
use PHPUnit\Framework\TestCase;
use RuntimeException;
class EsignConfigurationTest extends TestCase
{
public function test_it_normalizes_private_environment_values_without_exposing_the_secret(): void
{
$configuration = new EsignConfiguration(
environment: 'production',
baseUrl: 'https://openapi.esign.cn/',
appId: 'production-app-id',
appSecret: 'production-app-secret',
notifyUrl: 'https://example.test/integrations/esign/callback',
callbackToleranceSeconds: 900,
timeout: 20,
);
$configuration->assertReadyForApi();
$this->assertTrue($configuration->isConfigured());
$this->assertSame('https://openapi.esign.cn', $configuration->baseUrl());
$this->assertSame('production-app-id', $configuration->appId());
}
public function test_it_rejects_production_credentials_with_the_sandbox_endpoint(): void
{
$configuration = $this->configuration(baseUrl: 'https://smlopenapi.esign.cn');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('正式环境不能使用沙箱 API 地址');
$configuration->assertReadyForApi();
}
public function test_it_rejects_sandbox_credentials_with_the_production_endpoint(): void
{
$configuration = $this->configuration(environment: 'sandbox', baseUrl: 'https://openapi.esign.cn');
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('沙箱环境不能使用正式 API 地址');
$configuration->assertReadyForApi();
}
public function test_it_requires_both_credentials_before_an_api_request(): void
{
$configuration = $this->configuration(appSecret: '');
$this->assertFalse($configuration->isConfigured());
$this->expectExceptionMessage('尚未配置 App ID 或 AppSecret');
$configuration->assertReadyForApi();
}
private function configuration(
string $environment = 'production',
string $baseUrl = 'https://openapi.esign.cn',
string $appId = 'production-app-id',
string $appSecret = 'production-app-secret',
): EsignConfiguration {
return new EsignConfiguration(
environment: $environment,
baseUrl: $baseUrl,
appId: $appId,
appSecret: $appSecret,
notifyUrl: '',
callbackToleranceSeconds: 900,
timeout: 20,
);
}
}