| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Http;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\RequestException;
- use GuzzleHttp\Handler\MockHandler;
- use GuzzleHttp\HandlerStack;
- use GuzzleHttp\Psr7\Request;
- use GuzzleHttp\Psr7\Response;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Config\Config;
- use SixShop\Wangdian\Exception\HttpException;
- use SixShop\Wangdian\Http\HttpClient;
- use SixShop\Wangdian\Tests\TestConfig;
- class HttpClientTest extends TestCase
- {
- private Config $config;
- protected function setUp(): void
- {
- // Use secure test configuration
- $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 testConstructorWithDefaultClient(): void
- {
- $httpClient = new HttpClient($this->config);
-
- $this->assertInstanceOf(HttpClient::class, $httpClient);
- }
- public function testSuccessfulPostRequest(): void
- {
- $mockResponses = TestConfig::getMockResponses();
- $mockResponse = json_encode($mockResponses['success']);
-
- // Create a mock handler with successful response
- $mock = new MockHandler([
- new Response(200, [], $mockResponse)
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $result = $httpClient->post('test_endpoint.php', ['param' => 'value']);
-
- $this->assertEquals($mockResponses['success'], $result);
- }
- public function testPostRequestWithNon200Status(): void
- {
- $mock = new MockHandler([
- new Response(500, [], 'Internal Server Error')
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $this->expectException(HttpException::class);
- $this->expectExceptionMessageMatches('/HTTP request failed.*500/');
-
- $httpClient->post('test_endpoint.php', ['param' => 'value']);
- }
- public function testPostRequestWithInvalidJson(): void
- {
- $mock = new MockHandler([
- new Response(200, [], 'invalid json response')
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $this->expectException(HttpException::class);
- $this->expectExceptionMessage('Failed to parse JSON response');
-
- $httpClient->post('test_endpoint.php', ['param' => 'value']);
- }
- public function testPostRequestWithEmptyResponse(): void
- {
- $mock = new MockHandler([
- new Response(200, [], '')
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $this->expectException(HttpException::class);
- $this->expectExceptionMessage('Empty response body received');
-
- $httpClient->post('test_endpoint.php', ['param' => 'value']);
- }
- public function testPostRequestWithRequestException(): void
- {
- $mock = new MockHandler([
- new RequestException('Connection timeout', new Request('POST', 'test'))
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $this->expectException(HttpException::class);
- $this->expectExceptionMessage('HTTP request failed: Connection timeout');
-
- $httpClient->post('test_endpoint.php', ['param' => 'value']);
- }
- public function testEndpointUrlGeneration(): void
- {
- $mockResponses = TestConfig::getMockResponses();
- $mockResponse = json_encode($mockResponses['success']);
-
- $mock = new MockHandler([
- new Response(200, [], $mockResponse)
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- $result = $httpClient->post('trade_push.php', ['test' => 'data']);
-
- $this->assertEquals($mockResponses['success'], $result);
- }
- public function testSslVerificationDisabledForSandbox(): void
- {
- $sandboxConfig = Config::sandbox('test_sid', 'test_key', 'test_secret');
- $httpClient = new HttpClient($sandboxConfig);
-
- $this->assertInstanceOf(HttpClient::class, $httpClient);
- $this->assertTrue($sandboxConfig->isSandbox());
- }
- public function testHttpExceptionContext(): void
- {
- $mock = new MockHandler([
- new RequestException('Network error', new Request('POST', 'test'))
- ]);
- $handlerStack = HandlerStack::create($mock);
- $mockClient = new Client(['handler' => $handlerStack]);
-
- $httpClient = new HttpClient($this->config, $mockClient);
-
- try {
- $httpClient->post('test_endpoint.php', ['param' => 'value']);
- $this->fail('Expected HttpException was not thrown');
- } catch (HttpException $e) {
- $context = $e->getContext();
- $this->assertIsArray($context);
- $this->assertArrayHasKey('url', $context);
- $this->assertArrayHasKey('data', $context);
- }
- }
- }
|