| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Auth\Authenticator;
- use SixShop\Wangdian\Client;
- use SixShop\Wangdian\Config\Config;
- use SixShop\Wangdian\Http\HttpClient;
- use SixShop\Wangdian\Response\ApiResponse;
- use SixShop\Wangdian\Response\ResponseHandler;
- use SixShop\Wangdian\Services\BasicService;
- use SixShop\Wangdian\Services\GoodsService;
- use SixShop\Wangdian\Services\PurchaseService;
- use SixShop\Wangdian\Services\RefundService;
- use SixShop\Wangdian\Services\StockService;
- use SixShop\Wangdian\Services\TradeService;
- use SixShop\Wangdian\Tests\TestConfig;
- class ClientTest extends TestCase
- {
- private Config $config;
- private Client $client;
- protected function setUp(): void
- {
- $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']
- );
-
- $this->client = new Client($this->config);
- }
- public function testConstructorWithDefaultDependencies(): void
- {
- $client = new Client($this->config);
-
- $this->assertInstanceOf(Client::class, $client);
- $this->assertInstanceOf(Config::class, $client->getConfig());
- $this->assertInstanceOf(HttpClient::class, $client->getHttpClient());
- $this->assertInstanceOf(Authenticator::class, $client->getAuthenticator());
- $this->assertInstanceOf(ResponseHandler::class, $client->getResponseHandler());
- }
- public function testConstructorWithCustomDependencies(): void
- {
- $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
- $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
-
- $client = new Client($this->config, $httpClient, $logger);
-
- $this->assertInstanceOf(Client::class, $client);
- $this->assertSame($this->config, $client->getConfig());
- }
- public function testGetConfig(): void
- {
- $config = $this->client->getConfig();
-
- $this->assertSame($this->config, $config);
- }
- public function testGetHttpClient(): void
- {
- $httpClient = $this->client->getHttpClient();
-
- $this->assertInstanceOf(HttpClient::class, $httpClient);
- }
- public function testGetAuthenticator(): void
- {
- $authenticator = $this->client->getAuthenticator();
-
- $this->assertInstanceOf(Authenticator::class, $authenticator);
- }
- public function testGetResponseHandler(): void
- {
- $responseHandler = $this->client->getResponseHandler();
-
- $this->assertInstanceOf(ResponseHandler::class, $responseHandler);
- }
- public function testBasicServiceSingleton(): void
- {
- $service1 = $this->client->basic();
- $service2 = $this->client->basic();
-
- $this->assertInstanceOf(BasicService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testGoodsServiceSingleton(): void
- {
- $service1 = $this->client->goods();
- $service2 = $this->client->goods();
-
- $this->assertInstanceOf(GoodsService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testPurchaseServiceSingleton(): void
- {
- $service1 = $this->client->purchase();
- $service2 = $this->client->purchase();
-
- $this->assertInstanceOf(PurchaseService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testRefundServiceSingleton(): void
- {
- $service1 = $this->client->refund();
- $service2 = $this->client->refund();
-
- $this->assertInstanceOf(RefundService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testStockServiceSingleton(): void
- {
- $service1 = $this->client->stock();
- $service2 = $this->client->stock();
-
- $this->assertInstanceOf(StockService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testTradeServiceSingleton(): void
- {
- $service1 = $this->client->trade();
- $service2 = $this->client->trade();
-
- $this->assertInstanceOf(TradeService::class, $service1);
- $this->assertSame($service1, $service2); // Should return same instance
- }
- public function testAllServicesAreDifferentInstances(): void
- {
- $basic = $this->client->basic();
- $goods = $this->client->goods();
- $purchase = $this->client->purchase();
- $refund = $this->client->refund();
- $stock = $this->client->stock();
- $trade = $this->client->trade();
-
- // All services should be different instances
- $this->assertNotSame($basic, $goods);
- $this->assertNotSame($basic, $purchase);
- $this->assertNotSame($goods, $refund);
- $this->assertNotSame($stock, $trade);
- }
- public function testCallMethodIntegration(): void
- {
- // Since call method requires working HTTP client and authentication,
- // we'll test that the method exists and has the right signature
- $this->assertTrue(method_exists($this->client, 'call'));
-
- $reflection = new \ReflectionMethod($this->client, 'call');
- $this->assertTrue($reflection->isPublic());
-
- $parameters = $reflection->getParameters();
- $this->assertCount(2, $parameters);
- $this->assertEquals('endpoint', $parameters[0]->getName());
- $this->assertEquals('params', $parameters[1]->getName());
- $this->assertTrue($parameters[1]->isDefaultValueAvailable());
- $this->assertEquals([], $parameters[1]->getDefaultValue());
- }
- public function testServiceFactoryMethods(): void
- {
- // Test that all service factory methods exist
- $this->assertTrue(method_exists($this->client, 'basic'));
- $this->assertTrue(method_exists($this->client, 'goods'));
- $this->assertTrue(method_exists($this->client, 'purchase'));
- $this->assertTrue(method_exists($this->client, 'refund'));
- $this->assertTrue(method_exists($this->client, 'stock'));
- $this->assertTrue(method_exists($this->client, 'trade'));
- }
- public function testClientWithDifferentConfigs(): void
- {
- $config1 = Config::sandbox('sid1', 'key1', 'secret1');
- $config2 = Config::production('sid2', 'key2', 'secret2');
-
- $client1 = new Client($config1);
- $client2 = new Client($config2);
-
- $this->assertNotSame($client1->getConfig(), $client2->getConfig());
- $this->assertTrue($client1->getConfig()->isSandbox());
- $this->assertFalse($client2->getConfig()->isSandbox());
- }
- }
|