ClientTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Auth\Authenticator;
  6. use SixShop\Wangdian\Client;
  7. use SixShop\Wangdian\Config\Config;
  8. use SixShop\Wangdian\Http\HttpClient;
  9. use SixShop\Wangdian\Response\ApiResponse;
  10. use SixShop\Wangdian\Response\ResponseHandler;
  11. use SixShop\Wangdian\Services\BasicService;
  12. use SixShop\Wangdian\Services\GoodsService;
  13. use SixShop\Wangdian\Services\PurchaseService;
  14. use SixShop\Wangdian\Services\RefundService;
  15. use SixShop\Wangdian\Services\StockService;
  16. use SixShop\Wangdian\Services\TradeService;
  17. use SixShop\Wangdian\Tests\TestConfig;
  18. class ClientTest extends TestCase
  19. {
  20. private Config $config;
  21. private Client $client;
  22. protected function setUp(): void
  23. {
  24. $testConfig = TestConfig::get();
  25. $this->config = new Config(
  26. sid: $testConfig['credentials']['sid'],
  27. appKey: $testConfig['credentials']['app_key'],
  28. appSecret: $testConfig['credentials']['app_secret'],
  29. baseUrl: $testConfig['endpoints']['sandbox_base_url'],
  30. timeout: $testConfig['settings']['timeout'],
  31. debug: $testConfig['settings']['debug']
  32. );
  33. $this->client = new Client($this->config);
  34. }
  35. public function testConstructorWithDefaultDependencies(): void
  36. {
  37. $client = new Client($this->config);
  38. $this->assertInstanceOf(Client::class, $client);
  39. $this->assertInstanceOf(Config::class, $client->getConfig());
  40. $this->assertInstanceOf(HttpClient::class, $client->getHttpClient());
  41. $this->assertInstanceOf(Authenticator::class, $client->getAuthenticator());
  42. $this->assertInstanceOf(ResponseHandler::class, $client->getResponseHandler());
  43. }
  44. public function testConstructorWithCustomDependencies(): void
  45. {
  46. $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
  47. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  48. $client = new Client($this->config, $httpClient, $logger);
  49. $this->assertInstanceOf(Client::class, $client);
  50. $this->assertSame($this->config, $client->getConfig());
  51. }
  52. public function testGetConfig(): void
  53. {
  54. $config = $this->client->getConfig();
  55. $this->assertSame($this->config, $config);
  56. }
  57. public function testGetHttpClient(): void
  58. {
  59. $httpClient = $this->client->getHttpClient();
  60. $this->assertInstanceOf(HttpClient::class, $httpClient);
  61. }
  62. public function testGetAuthenticator(): void
  63. {
  64. $authenticator = $this->client->getAuthenticator();
  65. $this->assertInstanceOf(Authenticator::class, $authenticator);
  66. }
  67. public function testGetResponseHandler(): void
  68. {
  69. $responseHandler = $this->client->getResponseHandler();
  70. $this->assertInstanceOf(ResponseHandler::class, $responseHandler);
  71. }
  72. public function testBasicServiceSingleton(): void
  73. {
  74. $service1 = $this->client->basic();
  75. $service2 = $this->client->basic();
  76. $this->assertInstanceOf(BasicService::class, $service1);
  77. $this->assertSame($service1, $service2); // Should return same instance
  78. }
  79. public function testGoodsServiceSingleton(): void
  80. {
  81. $service1 = $this->client->goods();
  82. $service2 = $this->client->goods();
  83. $this->assertInstanceOf(GoodsService::class, $service1);
  84. $this->assertSame($service1, $service2); // Should return same instance
  85. }
  86. public function testPurchaseServiceSingleton(): void
  87. {
  88. $service1 = $this->client->purchase();
  89. $service2 = $this->client->purchase();
  90. $this->assertInstanceOf(PurchaseService::class, $service1);
  91. $this->assertSame($service1, $service2); // Should return same instance
  92. }
  93. public function testRefundServiceSingleton(): void
  94. {
  95. $service1 = $this->client->refund();
  96. $service2 = $this->client->refund();
  97. $this->assertInstanceOf(RefundService::class, $service1);
  98. $this->assertSame($service1, $service2); // Should return same instance
  99. }
  100. public function testStockServiceSingleton(): void
  101. {
  102. $service1 = $this->client->stock();
  103. $service2 = $this->client->stock();
  104. $this->assertInstanceOf(StockService::class, $service1);
  105. $this->assertSame($service1, $service2); // Should return same instance
  106. }
  107. public function testTradeServiceSingleton(): void
  108. {
  109. $service1 = $this->client->trade();
  110. $service2 = $this->client->trade();
  111. $this->assertInstanceOf(TradeService::class, $service1);
  112. $this->assertSame($service1, $service2); // Should return same instance
  113. }
  114. public function testAllServicesAreDifferentInstances(): void
  115. {
  116. $basic = $this->client->basic();
  117. $goods = $this->client->goods();
  118. $purchase = $this->client->purchase();
  119. $refund = $this->client->refund();
  120. $stock = $this->client->stock();
  121. $trade = $this->client->trade();
  122. // All services should be different instances
  123. $this->assertNotSame($basic, $goods);
  124. $this->assertNotSame($basic, $purchase);
  125. $this->assertNotSame($goods, $refund);
  126. $this->assertNotSame($stock, $trade);
  127. }
  128. public function testCallMethodIntegration(): void
  129. {
  130. // Since call method requires working HTTP client and authentication,
  131. // we'll test that the method exists and has the right signature
  132. $this->assertTrue(method_exists($this->client, 'call'));
  133. $reflection = new \ReflectionMethod($this->client, 'call');
  134. $this->assertTrue($reflection->isPublic());
  135. $parameters = $reflection->getParameters();
  136. $this->assertCount(2, $parameters);
  137. $this->assertEquals('endpoint', $parameters[0]->getName());
  138. $this->assertEquals('params', $parameters[1]->getName());
  139. $this->assertTrue($parameters[1]->isDefaultValueAvailable());
  140. $this->assertEquals([], $parameters[1]->getDefaultValue());
  141. }
  142. public function testServiceFactoryMethods(): void
  143. {
  144. // Test that all service factory methods exist
  145. $this->assertTrue(method_exists($this->client, 'basic'));
  146. $this->assertTrue(method_exists($this->client, 'goods'));
  147. $this->assertTrue(method_exists($this->client, 'purchase'));
  148. $this->assertTrue(method_exists($this->client, 'refund'));
  149. $this->assertTrue(method_exists($this->client, 'stock'));
  150. $this->assertTrue(method_exists($this->client, 'trade'));
  151. }
  152. public function testClientWithDifferentConfigs(): void
  153. {
  154. $config1 = Config::sandbox('sid1', 'key1', 'secret1');
  155. $config2 = Config::production('sid2', 'key2', 'secret2');
  156. $client1 = new Client($config1);
  157. $client2 = new Client($config2);
  158. $this->assertNotSame($client1->getConfig(), $client2->getConfig());
  159. $this->assertTrue($client1->getConfig()->isSandbox());
  160. $this->assertFalse($client2->getConfig()->isSandbox());
  161. }
  162. }