| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests;
- /**
- * Test configuration helper
- * Safely loads test configuration from environment or fallback to mock values
- */
- class TestConfig
- {
- private static ?array $config = null;
- /**
- * Get test configuration
- * Loads from .env.testing if available, otherwise uses mock values
- */
- public static function get(): array
- {
- if (self::$config !== null) {
- return self::$config;
- }
- // Try to load environment configuration
- $envFile = __DIR__ . '/../.env.testing';
- $useRealCredentials = false;
- if (file_exists($envFile)) {
- $envContent = file_get_contents($envFile);
- if ($envContent !== false) {
- $lines = explode("\n", $envContent);
- $envVars = [];
-
- foreach ($lines as $line) {
- $line = trim($line);
- if (empty($line) || str_starts_with($line, '#')) {
- continue;
- }
-
- $parts = explode('=', $line, 2);
- if (count($parts) === 2) {
- $envVars[$parts[0]] = $parts[1];
- }
- }
-
- if (isset($envVars['WANGDIAN_TEST_SID']) &&
- isset($envVars['WANGDIAN_TEST_APP_KEY']) &&
- isset($envVars['WANGDIAN_TEST_APP_SECRET'])) {
- $useRealCredentials = true;
-
- self::$config = [
- 'credentials' => [
- 'sid' => $envVars['WANGDIAN_TEST_SID'],
- 'app_key' => $envVars['WANGDIAN_TEST_APP_KEY'],
- 'app_secret' => $envVars['WANGDIAN_TEST_APP_SECRET'],
- ],
- 'endpoints' => [
- 'sandbox_base_url' => $envVars['WANGDIAN_TEST_BASE_URL'] ?? 'https://sandbox.wangdian.cn/openapi2',
- ],
- 'settings' => [
- 'timeout' => (int)($envVars['WANGDIAN_TEST_TIMEOUT'] ?? 30),
- 'debug' => ($envVars['WANGDIAN_TEST_DEBUG'] ?? 'true') === 'true',
- 'log_file' => null,
- ],
- 'test_mode' => [
- 'use_real_credentials' => true,
- 'run_integration_tests' => ($envVars['RUN_INTEGRATION_TESTS'] ?? 'false') === 'true',
- 'run_real_api_tests' => ($envVars['RUN_REAL_API_TESTS'] ?? 'false') === 'true',
- ],
- ];
- }
- }
- }
- // Fallback to mock configuration if real credentials not available
- if (!$useRealCredentials) {
- self::$config = [
- 'credentials' => [
- 'sid' => 'test_sid_12345',
- 'app_key' => 'test_app_key_67890',
- 'app_secret' => 'test_app_secret_abcdef',
- ],
- 'endpoints' => [
- 'sandbox_base_url' => 'https://mock-api.example.com/openapi2',
- ],
- 'settings' => [
- 'timeout' => 5,
- 'debug' => true,
- 'log_file' => null,
- ],
- 'test_mode' => [
- 'use_real_credentials' => false,
- 'run_integration_tests' => false,
- 'run_real_api_tests' => false,
- ],
- ];
- }
- return self::$config;
- }
- /**
- * Check if using real credentials
- */
- public static function isUsingRealCredentials(): bool
- {
- $config = self::get();
- return $config['test_mode']['use_real_credentials'];
- }
- /**
- * Check if integration tests should run
- */
- public static function shouldRunIntegrationTests(): bool
- {
- $config = self::get();
- return $config['test_mode']['run_integration_tests'];
- }
- /**
- * Check if real API tests should run
- */
- public static function shouldRunRealApiTests(): bool
- {
- $config = self::get();
- return $config['test_mode']['run_real_api_tests'];
- }
- /**
- * Get mock response data
- */
- public static function getMockResponses(): array
- {
- return [
- 'success' => [
- 'code' => 0,
- 'message' => 'Success',
- 'data' => ['test' => 'result']
- ],
- 'error' => [
- 'code' => 1001,
- 'message' => 'Test error',
- 'data' => null
- ],
- ];
- }
- }
|