ClientTest.php 6.9 KB

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