TestConfig.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests;
  4. /**
  5. * Test configuration helper
  6. * Safely loads test configuration from environment or fallback to mock values
  7. */
  8. class TestConfig
  9. {
  10. private static ?array $config = null;
  11. /**
  12. * Get test configuration
  13. * Loads from .env.testing if available, otherwise uses mock values
  14. */
  15. public static function get(): array
  16. {
  17. if (self::$config !== null) {
  18. return self::$config;
  19. }
  20. // Try to load environment configuration
  21. $envFile = __DIR__ . '/../.env.testing';
  22. $useRealCredentials = false;
  23. if (file_exists($envFile)) {
  24. $envContent = file_get_contents($envFile);
  25. if ($envContent !== false) {
  26. $lines = explode("\n", $envContent);
  27. $envVars = [];
  28. foreach ($lines as $line) {
  29. $line = trim($line);
  30. if (empty($line) || str_starts_with($line, '#')) {
  31. continue;
  32. }
  33. $parts = explode('=', $line, 2);
  34. if (count($parts) === 2) {
  35. $envVars[$parts[0]] = $parts[1];
  36. }
  37. }
  38. if (isset($envVars['WANGDIAN_TEST_SID']) &&
  39. isset($envVars['WANGDIAN_TEST_APP_KEY']) &&
  40. isset($envVars['WANGDIAN_TEST_APP_SECRET'])) {
  41. $useRealCredentials = true;
  42. self::$config = [
  43. 'credentials' => [
  44. 'sid' => $envVars['WANGDIAN_TEST_SID'],
  45. 'app_key' => $envVars['WANGDIAN_TEST_APP_KEY'],
  46. 'app_secret' => $envVars['WANGDIAN_TEST_APP_SECRET'],
  47. ],
  48. 'endpoints' => [
  49. 'sandbox_base_url' => $envVars['WANGDIAN_TEST_BASE_URL'] ?? 'https://sandbox.wangdian.cn/openapi2',
  50. ],
  51. 'settings' => [
  52. 'timeout' => (int)($envVars['WANGDIAN_TEST_TIMEOUT'] ?? 30),
  53. 'debug' => ($envVars['WANGDIAN_TEST_DEBUG'] ?? 'true') === 'true',
  54. 'log_file' => null,
  55. ],
  56. 'test_mode' => [
  57. 'use_real_credentials' => true,
  58. 'run_integration_tests' => ($envVars['RUN_INTEGRATION_TESTS'] ?? 'false') === 'true',
  59. 'run_real_api_tests' => ($envVars['RUN_REAL_API_TESTS'] ?? 'false') === 'true',
  60. ],
  61. ];
  62. }
  63. }
  64. }
  65. // Fallback to mock configuration if real credentials not available
  66. if (!$useRealCredentials) {
  67. self::$config = [
  68. 'credentials' => [
  69. 'sid' => 'test_sid_12345',
  70. 'app_key' => 'test_app_key_67890',
  71. 'app_secret' => 'test_app_secret_abcdef',
  72. ],
  73. 'endpoints' => [
  74. 'sandbox_base_url' => 'https://mock-api.example.com/openapi2',
  75. ],
  76. 'settings' => [
  77. 'timeout' => 5,
  78. 'debug' => true,
  79. 'log_file' => null,
  80. ],
  81. 'test_mode' => [
  82. 'use_real_credentials' => false,
  83. 'run_integration_tests' => false,
  84. 'run_real_api_tests' => false,
  85. ],
  86. ];
  87. }
  88. return self::$config;
  89. }
  90. /**
  91. * Check if using real credentials
  92. */
  93. public static function isUsingRealCredentials(): bool
  94. {
  95. $config = self::get();
  96. return $config['test_mode']['use_real_credentials'];
  97. }
  98. /**
  99. * Check if integration tests should run
  100. */
  101. public static function shouldRunIntegrationTests(): bool
  102. {
  103. $config = self::get();
  104. return $config['test_mode']['run_integration_tests'];
  105. }
  106. /**
  107. * Check if real API tests should run
  108. */
  109. public static function shouldRunRealApiTests(): bool
  110. {
  111. $config = self::get();
  112. return $config['test_mode']['run_real_api_tests'];
  113. }
  114. /**
  115. * Get mock response data
  116. */
  117. public static function getMockResponses(): array
  118. {
  119. return [
  120. 'success' => [
  121. 'code' => 0,
  122. 'message' => 'Success',
  123. 'data' => ['test' => 'result']
  124. ],
  125. 'error' => [
  126. 'code' => 1001,
  127. 'message' => 'Test error',
  128. 'data' => null
  129. ],
  130. ];
  131. }
  132. }