WangdianFactoryTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Client;
  6. use SixShop\Wangdian\Config\Config;
  7. use SixShop\Wangdian\WangdianFactory;
  8. class WangdianFactoryTest extends TestCase
  9. {
  10. private string $testSid = 'test_sid';
  11. private string $testAppKey = 'test_app_key';
  12. private string $testAppSecret = 'test_app_secret';
  13. public function testCreateSandboxClient(): void
  14. {
  15. $client = WangdianFactory::createSandboxClient(
  16. $this->testSid,
  17. $this->testAppKey,
  18. $this->testAppSecret
  19. );
  20. $this->assertInstanceOf(Client::class, $client);
  21. $config = $client->getConfig();
  22. $this->assertEquals($this->testSid, $config->sid);
  23. $this->assertEquals($this->testAppKey, $config->appKey);
  24. $this->assertEquals($this->testAppSecret, $config->appSecret);
  25. $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
  26. $this->assertTrue($config->debug);
  27. $this->assertTrue($config->isSandbox());
  28. }
  29. public function testCreateSandboxClientWithCustomDependencies(): void
  30. {
  31. $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
  32. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  33. $client = WangdianFactory::createSandboxClient(
  34. $this->testSid,
  35. $this->testAppKey,
  36. $this->testAppSecret,
  37. $httpClient,
  38. $logger
  39. );
  40. $this->assertInstanceOf(Client::class, $client);
  41. $this->assertTrue($client->getConfig()->isSandbox());
  42. }
  43. public function testCreateProductionClient(): void
  44. {
  45. $client = WangdianFactory::createProductionClient(
  46. $this->testSid,
  47. $this->testAppKey,
  48. $this->testAppSecret
  49. );
  50. $this->assertInstanceOf(Client::class, $client);
  51. $config = $client->getConfig();
  52. $this->assertEquals($this->testSid, $config->sid);
  53. $this->assertEquals($this->testAppKey, $config->appKey);
  54. $this->assertEquals($this->testAppSecret, $config->appSecret);
  55. $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
  56. $this->assertFalse($config->debug);
  57. $this->assertFalse($config->isSandbox());
  58. }
  59. public function testCreateProductionClientWithCustomDependencies(): void
  60. {
  61. $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
  62. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  63. $client = WangdianFactory::createProductionClient(
  64. $this->testSid,
  65. $this->testAppKey,
  66. $this->testAppSecret,
  67. $httpClient,
  68. $logger
  69. );
  70. $this->assertInstanceOf(Client::class, $client);
  71. $this->assertFalse($client->getConfig()->isSandbox());
  72. }
  73. public function testCreateClientWithCustomConfig(): void
  74. {
  75. $customConfig = new Config(
  76. sid: $this->testSid,
  77. appKey: $this->testAppKey,
  78. appSecret: $this->testAppSecret,
  79. baseUrl: 'https://custom.api.com/v1',
  80. timeout: 60,
  81. debug: true,
  82. logFile: '/tmp/custom.log'
  83. );
  84. $client = WangdianFactory::createClient($customConfig);
  85. $this->assertInstanceOf(Client::class, $client);
  86. $this->assertSame($customConfig, $client->getConfig());
  87. }
  88. public function testCreateClientWithCustomConfigAndDependencies(): void
  89. {
  90. $customConfig = new Config(
  91. sid: $this->testSid,
  92. appKey: $this->testAppKey,
  93. appSecret: $this->testAppSecret
  94. );
  95. $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
  96. $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
  97. $client = WangdianFactory::createClient($customConfig, $httpClient, $logger);
  98. $this->assertInstanceOf(Client::class, $client);
  99. $this->assertSame($customConfig, $client->getConfig());
  100. }
  101. public function testCreateConfig(): void
  102. {
  103. $config = WangdianFactory::createConfig(
  104. sid: $this->testSid,
  105. appKey: $this->testAppKey,
  106. appSecret: $this->testAppSecret
  107. );
  108. $this->assertInstanceOf(Config::class, $config);
  109. $this->assertEquals($this->testSid, $config->sid);
  110. $this->assertEquals($this->testAppKey, $config->appKey);
  111. $this->assertEquals($this->testAppSecret, $config->appSecret);
  112. $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl); // Default
  113. $this->assertEquals(30, $config->timeout); // Default
  114. $this->assertFalse($config->debug); // Default
  115. $this->assertNull($config->logFile); // Default
  116. }
  117. public function testCreateConfigWithCustomParameters(): void
  118. {
  119. $customBaseUrl = 'https://custom.wangdian.com/api';
  120. $customTimeout = 45;
  121. $customLogFile = '/var/log/wangdian.log';
  122. $config = WangdianFactory::createConfig(
  123. sid: $this->testSid,
  124. appKey: $this->testAppKey,
  125. appSecret: $this->testAppSecret,
  126. baseUrl: $customBaseUrl,
  127. timeout: $customTimeout,
  128. debug: true,
  129. logFile: $customLogFile
  130. );
  131. $this->assertInstanceOf(Config::class, $config);
  132. $this->assertEquals($this->testSid, $config->sid);
  133. $this->assertEquals($this->testAppKey, $config->appKey);
  134. $this->assertEquals($this->testAppSecret, $config->appSecret);
  135. $this->assertEquals($customBaseUrl, $config->baseUrl);
  136. $this->assertEquals($customTimeout, $config->timeout);
  137. $this->assertTrue($config->debug);
  138. $this->assertEquals($customLogFile, $config->logFile);
  139. }
  140. public function testCreateConfigWithProductionUrl(): void
  141. {
  142. $config = WangdianFactory::createConfig(
  143. sid: $this->testSid,
  144. appKey: $this->testAppKey,
  145. appSecret: $this->testAppSecret,
  146. baseUrl: Config::PRODUCTION_BASE_URL
  147. );
  148. $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
  149. $this->assertFalse($config->isSandbox());
  150. }
  151. public function testFactoryMethodsCreateIndependentInstances(): void
  152. {
  153. $client1 = WangdianFactory::createSandboxClient('sid1', 'key1', 'secret1');
  154. $client2 = WangdianFactory::createSandboxClient('sid2', 'key2', 'secret2');
  155. $this->assertNotSame($client1, $client2);
  156. $this->assertNotSame($client1->getConfig(), $client2->getConfig());
  157. $this->assertEquals('sid1', $client1->getConfig()->sid);
  158. $this->assertEquals('sid2', $client2->getConfig()->sid);
  159. }
  160. public function testSandboxAndProductionClientsHaveDifferentConfigs(): void
  161. {
  162. $sandboxClient = WangdianFactory::createSandboxClient(
  163. $this->testSid,
  164. $this->testAppKey,
  165. $this->testAppSecret
  166. );
  167. $productionClient = WangdianFactory::createProductionClient(
  168. $this->testSid,
  169. $this->testAppKey,
  170. $this->testAppSecret
  171. );
  172. $sandboxConfig = $sandboxClient->getConfig();
  173. $productionConfig = $productionClient->getConfig();
  174. $this->assertTrue($sandboxConfig->isSandbox());
  175. $this->assertFalse($productionConfig->isSandbox());
  176. $this->assertTrue($sandboxConfig->debug);
  177. $this->assertFalse($productionConfig->debug);
  178. $this->assertEquals(Config::SANDBOX_BASE_URL, $sandboxConfig->baseUrl);
  179. $this->assertEquals(Config::PRODUCTION_BASE_URL, $productionConfig->baseUrl);
  180. }
  181. public function testFactoryMethodsAreStatic(): void
  182. {
  183. $reflection = new \ReflectionClass(WangdianFactory::class);
  184. $methods = [
  185. 'createSandboxClient',
  186. 'createProductionClient',
  187. 'createClient',
  188. 'createConfig'
  189. ];
  190. foreach ($methods as $methodName) {
  191. $method = $reflection->getMethod($methodName);
  192. $this->assertTrue($method->isStatic(), "Method {$methodName} should be static");
  193. $this->assertTrue($method->isPublic(), "Method {$methodName} should be public");
  194. }
  195. }
  196. }