WangdianIntegrationTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Integration;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Config\Config;
  6. use SixShop\Wangdian\WangdianFactory;
  7. use SixShop\Wangdian\Tests\TestConfig;
  8. /**
  9. * Integration tests for real API functionality
  10. * These tests will only run if real credentials are configured
  11. */
  12. class WangdianIntegrationTest extends TestCase
  13. {
  14. private ?Config $config = null;
  15. protected function setUp(): void
  16. {
  17. if (!TestConfig::shouldRunIntegrationTests()) {
  18. $this->markTestSkipped('Integration tests are disabled. Set RUN_INTEGRATION_TESTS=true in .env.testing to enable.');
  19. }
  20. if (!TestConfig::isUsingRealCredentials()) {
  21. $this->markTestSkipped('Real credentials not configured. Please check .env.testing file.');
  22. }
  23. $testConfig = TestConfig::get();
  24. $this->config = new Config(
  25. sid: $testConfig['credentials']['sid'],
  26. appKey: $testConfig['credentials']['app_key'],
  27. appSecret: $testConfig['credentials']['app_secret'],
  28. baseUrl: $testConfig['endpoints']['sandbox_base_url'],
  29. timeout: $testConfig['settings']['timeout'],
  30. debug: $testConfig['settings']['debug']
  31. );
  32. }
  33. public function testFactoryCreatesSandboxClient(): void
  34. {
  35. $testConfig = TestConfig::get();
  36. $client = WangdianFactory::createSandboxClient(
  37. sid: $testConfig['credentials']['sid'],
  38. appKey: $testConfig['credentials']['app_key'],
  39. appSecret: $testConfig['credentials']['app_secret']
  40. );
  41. $this->assertInstanceOf(\SixShop\Wangdian\Client::class, $client);
  42. $this->assertTrue($client->getConfig()->isSandbox());
  43. }
  44. public function testBasicAuthenticationWorkflow(): void
  45. {
  46. $testConfig = TestConfig::get();
  47. $client = WangdianFactory::createSandboxClient(
  48. sid: $testConfig['credentials']['sid'],
  49. appKey: $testConfig['credentials']['app_key'],
  50. appSecret: $testConfig['credentials']['app_secret']
  51. );
  52. // Test that authenticator properly adds auth params
  53. $authenticator = $client->getAuthenticator();
  54. $params = $authenticator->addAuthParams(['test' => 'value']);
  55. $this->assertArrayHasKey('sid', $params);
  56. $this->assertArrayHasKey('appkey', $params);
  57. $this->assertArrayHasKey('timestamp', $params);
  58. $this->assertArrayHasKey('sign', $params);
  59. $this->assertEquals($testConfig['credentials']['sid'], $params['sid']);
  60. $this->assertEquals($testConfig['credentials']['app_key'], $params['appkey']);
  61. }
  62. public function testSignatureGeneration(): void
  63. {
  64. $testConfig = TestConfig::get();
  65. $client = WangdianFactory::createSandboxClient(
  66. sid: $testConfig['credentials']['sid'],
  67. appKey: $testConfig['credentials']['app_key'],
  68. appSecret: $testConfig['credentials']['app_secret']
  69. );
  70. $authenticator = $client->getAuthenticator();
  71. $signature1 = $authenticator->generateSignature(['test' => 'value']);
  72. $signature2 = $authenticator->generateSignature(['test' => 'value']);
  73. // Signatures should be valid MD5 hashes
  74. $this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $signature1);
  75. $this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $signature2);
  76. // Note: They may be different due to timestamp differences
  77. $this->assertIsString($signature1);
  78. $this->assertIsString($signature2);
  79. }
  80. /**
  81. * This test makes a real API call - only run if explicitly enabled
  82. * @group realapi
  83. */
  84. public function testRealApiCall(): void
  85. {
  86. if (!TestConfig::shouldRunRealApiTests()) {
  87. $this->markTestSkipped('Real API tests are disabled. Set RUN_REAL_API_TESTS=true in .env.testing to enable.');
  88. }
  89. $testConfig = TestConfig::get();
  90. $client = WangdianFactory::createSandboxClient(
  91. sid: $testConfig['credentials']['sid'],
  92. appKey: $testConfig['credentials']['app_key'],
  93. appSecret: $testConfig['credentials']['app_secret']
  94. );
  95. try {
  96. // Try to query warehouses - this should work with valid credentials
  97. $response = $client->basic()->queryWarehouse();
  98. $this->assertInstanceOf(\SixShop\Wangdian\Response\ApiResponse::class, $response);
  99. // Don't assert success as it depends on actual API status
  100. $this->assertIsInt($response->getCode());
  101. $this->assertIsString($response->getMessage());
  102. } catch (\SixShop\Wangdian\Exception\ApiException $e) {
  103. // API exceptions are expected with test credentials
  104. $this->addToAssertionCount(1); // Count as assertion
  105. } catch (\SixShop\Wangdian\Exception\HttpException $e) {
  106. // HTTP exceptions might indicate network issues
  107. $this->addToAssertionCount(1); // Count as assertion
  108. }
  109. }
  110. public function testConfigurationSecurity(): void
  111. {
  112. // Ensure real credentials are not accidentally logged or exposed
  113. $testConfig = TestConfig::get();
  114. $this->assertTrue(TestConfig::isUsingRealCredentials());
  115. $this->assertNotEquals('mock_sid_12345', $testConfig['credentials']['sid']);
  116. $this->assertNotEquals('mock_app_key_67890', $testConfig['credentials']['app_key']);
  117. $this->assertNotEquals('mock_app_secret_abcdef', $testConfig['credentials']['app_secret']);
  118. // Verify the real credentials match what was provided
  119. $this->assertEquals('apidevnew2', $testConfig['credentials']['sid']);
  120. $this->assertEquals('rhsw02-test', $testConfig['credentials']['app_key']);
  121. $this->assertEquals('03da28e20', $testConfig['credentials']['app_secret']);
  122. }
  123. }