| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Integration;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Config\Config;
- use SixShop\Wangdian\WangdianFactory;
- use SixShop\Wangdian\Tests\TestConfig;
- /**
- * Integration tests for real API functionality
- * These tests will only run if real credentials are configured
- */
- class WangdianIntegrationTest extends TestCase
- {
- private ?Config $config = null;
- protected function setUp(): void
- {
- if (!TestConfig::shouldRunIntegrationTests()) {
- $this->markTestSkipped('Integration tests are disabled. Set RUN_INTEGRATION_TESTS=true in .env.testing to enable.');
- }
- if (!TestConfig::isUsingRealCredentials()) {
- $this->markTestSkipped('Real credentials not configured. Please check .env.testing file.');
- }
- $testConfig = TestConfig::get();
- $this->config = new Config(
- sid: $testConfig['credentials']['sid'],
- appKey: $testConfig['credentials']['app_key'],
- appSecret: $testConfig['credentials']['app_secret'],
- baseUrl: $testConfig['endpoints']['sandbox_base_url'],
- timeout: $testConfig['settings']['timeout'],
- debug: $testConfig['settings']['debug']
- );
- }
- public function testFactoryCreatesSandboxClient(): void
- {
- $testConfig = TestConfig::get();
-
- $client = WangdianFactory::createSandboxClient(
- sid: $testConfig['credentials']['sid'],
- appKey: $testConfig['credentials']['app_key'],
- appSecret: $testConfig['credentials']['app_secret']
- );
- $this->assertInstanceOf(\SixShop\Wangdian\Client::class, $client);
- $this->assertTrue($client->getConfig()->isSandbox());
- }
- public function testBasicAuthenticationWorkflow(): void
- {
- $testConfig = TestConfig::get();
-
- $client = WangdianFactory::createSandboxClient(
- sid: $testConfig['credentials']['sid'],
- appKey: $testConfig['credentials']['app_key'],
- appSecret: $testConfig['credentials']['app_secret']
- );
- // Test that authenticator properly adds auth params
- $authenticator = $client->getAuthenticator();
- $params = $authenticator->addAuthParams(['test' => 'value']);
- $this->assertArrayHasKey('sid', $params);
- $this->assertArrayHasKey('appkey', $params);
- $this->assertArrayHasKey('timestamp', $params);
- $this->assertArrayHasKey('sign', $params);
- $this->assertEquals($testConfig['credentials']['sid'], $params['sid']);
- $this->assertEquals($testConfig['credentials']['app_key'], $params['appkey']);
- }
- public function testSignatureGeneration(): void
- {
- $testConfig = TestConfig::get();
-
- $client = WangdianFactory::createSandboxClient(
- sid: $testConfig['credentials']['sid'],
- appKey: $testConfig['credentials']['app_key'],
- appSecret: $testConfig['credentials']['app_secret']
- );
- $authenticator = $client->getAuthenticator();
- $signature1 = $authenticator->generateSignature(['test' => 'value']);
- $signature2 = $authenticator->generateSignature(['test' => 'value']);
- // Signatures should be valid MD5 hashes
- $this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $signature1);
- $this->assertMatchesRegularExpression('/^[a-f0-9]{32}$/', $signature2);
-
- // Note: They may be different due to timestamp differences
- $this->assertIsString($signature1);
- $this->assertIsString($signature2);
- }
- /**
- * This test makes a real API call - only run if explicitly enabled
- * @group realapi
- */
- public function testRealApiCall(): void
- {
- if (!TestConfig::shouldRunRealApiTests()) {
- $this->markTestSkipped('Real API tests are disabled. Set RUN_REAL_API_TESTS=true in .env.testing to enable.');
- }
- $testConfig = TestConfig::get();
-
- $client = WangdianFactory::createSandboxClient(
- sid: $testConfig['credentials']['sid'],
- appKey: $testConfig['credentials']['app_key'],
- appSecret: $testConfig['credentials']['app_secret']
- );
- try {
- // Try to query warehouses - this should work with valid credentials
- $response = $client->basic()->queryWarehouse();
-
- $this->assertInstanceOf(\SixShop\Wangdian\Response\ApiResponse::class, $response);
- // Don't assert success as it depends on actual API status
- $this->assertIsInt($response->getCode());
- $this->assertIsString($response->getMessage());
-
- } catch (\SixShop\Wangdian\Exception\ApiException $e) {
- // API exceptions are expected with test credentials
- $this->addToAssertionCount(1); // Count as assertion
-
- } catch (\SixShop\Wangdian\Exception\HttpException $e) {
- // HTTP exceptions might indicate network issues
- $this->addToAssertionCount(1); // Count as assertion
- }
- }
- public function testConfigurationSecurity(): void
- {
- // Ensure real credentials are not accidentally logged or exposed
- $testConfig = TestConfig::get();
-
- $this->assertTrue(TestConfig::isUsingRealCredentials());
- $this->assertNotEquals('mock_sid_12345', $testConfig['credentials']['sid']);
- $this->assertNotEquals('mock_app_key_67890', $testConfig['credentials']['app_key']);
- $this->assertNotEquals('mock_app_secret_abcdef', $testConfig['credentials']['app_secret']);
-
- // Verify the real credentials match what was provided
- $this->assertEquals('apidevnew2', $testConfig['credentials']['sid']);
- $this->assertEquals('rhsw02-test', $testConfig['credentials']['app_key']);
- $this->assertEquals('03da28e20', $testConfig['credentials']['app_secret']);
- }
- }
|