| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Client;
- use SixShop\Wangdian\Config\Config;
- use SixShop\Wangdian\WangdianFactory;
- class WangdianFactoryTest extends TestCase
- {
- private string $testSid = 'test_sid';
- private string $testAppKey = 'test_app_key';
- private string $testAppSecret = 'test_app_secret';
- public function testCreateSandboxClient(): void
- {
- $client = WangdianFactory::createSandboxClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret
- );
- $this->assertInstanceOf(Client::class, $client);
-
- $config = $client->getConfig();
- $this->assertEquals($this->testSid, $config->sid);
- $this->assertEquals($this->testAppKey, $config->appKey);
- $this->assertEquals($this->testAppSecret, $config->appSecret);
- $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl);
- $this->assertTrue($config->debug);
- $this->assertTrue($config->isSandbox());
- }
- public function testCreateSandboxClientWithCustomDependencies(): void
- {
- $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
- $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $client = WangdianFactory::createSandboxClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret,
- $httpClient,
- $logger
- );
- $this->assertInstanceOf(Client::class, $client);
- $this->assertTrue($client->getConfig()->isSandbox());
- }
- public function testCreateProductionClient(): void
- {
- $client = WangdianFactory::createProductionClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret
- );
- $this->assertInstanceOf(Client::class, $client);
-
- $config = $client->getConfig();
- $this->assertEquals($this->testSid, $config->sid);
- $this->assertEquals($this->testAppKey, $config->appKey);
- $this->assertEquals($this->testAppSecret, $config->appSecret);
- $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
- $this->assertFalse($config->debug);
- $this->assertFalse($config->isSandbox());
- }
- public function testCreateProductionClientWithCustomDependencies(): void
- {
- $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
- $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $client = WangdianFactory::createProductionClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret,
- $httpClient,
- $logger
- );
- $this->assertInstanceOf(Client::class, $client);
- $this->assertFalse($client->getConfig()->isSandbox());
- }
- public function testCreateClientWithCustomConfig(): void
- {
- $customConfig = new Config(
- sid: $this->testSid,
- appKey: $this->testAppKey,
- appSecret: $this->testAppSecret,
- baseUrl: 'https://custom.api.com/v1',
- timeout: 60,
- debug: true,
- logFile: '/tmp/custom.log'
- );
- $client = WangdianFactory::createClient($customConfig);
- $this->assertInstanceOf(Client::class, $client);
- $this->assertSame($customConfig, $client->getConfig());
- }
- public function testCreateClientWithCustomConfigAndDependencies(): void
- {
- $customConfig = new Config(
- sid: $this->testSid,
- appKey: $this->testAppKey,
- appSecret: $this->testAppSecret
- );
-
- $httpClient = $this->createMock(\Psr\Http\Client\ClientInterface::class);
- $logger = $this->createMock(\Psr\Log\LoggerInterface::class);
- $client = WangdianFactory::createClient($customConfig, $httpClient, $logger);
- $this->assertInstanceOf(Client::class, $client);
- $this->assertSame($customConfig, $client->getConfig());
- }
- public function testCreateConfig(): void
- {
- $config = WangdianFactory::createConfig(
- sid: $this->testSid,
- appKey: $this->testAppKey,
- appSecret: $this->testAppSecret
- );
- $this->assertInstanceOf(Config::class, $config);
- $this->assertEquals($this->testSid, $config->sid);
- $this->assertEquals($this->testAppKey, $config->appKey);
- $this->assertEquals($this->testAppSecret, $config->appSecret);
- $this->assertEquals(Config::SANDBOX_BASE_URL, $config->baseUrl); // Default
- $this->assertEquals(30, $config->timeout); // Default
- $this->assertFalse($config->debug); // Default
- $this->assertNull($config->logFile); // Default
- }
- public function testCreateConfigWithCustomParameters(): void
- {
- $customBaseUrl = 'https://custom.wangdian.com/api';
- $customTimeout = 45;
- $customLogFile = '/var/log/wangdian.log';
- $config = WangdianFactory::createConfig(
- sid: $this->testSid,
- appKey: $this->testAppKey,
- appSecret: $this->testAppSecret,
- baseUrl: $customBaseUrl,
- timeout: $customTimeout,
- debug: true,
- logFile: $customLogFile
- );
- $this->assertInstanceOf(Config::class, $config);
- $this->assertEquals($this->testSid, $config->sid);
- $this->assertEquals($this->testAppKey, $config->appKey);
- $this->assertEquals($this->testAppSecret, $config->appSecret);
- $this->assertEquals($customBaseUrl, $config->baseUrl);
- $this->assertEquals($customTimeout, $config->timeout);
- $this->assertTrue($config->debug);
- $this->assertEquals($customLogFile, $config->logFile);
- }
- public function testCreateConfigWithProductionUrl(): void
- {
- $config = WangdianFactory::createConfig(
- sid: $this->testSid,
- appKey: $this->testAppKey,
- appSecret: $this->testAppSecret,
- baseUrl: Config::PRODUCTION_BASE_URL
- );
- $this->assertEquals(Config::PRODUCTION_BASE_URL, $config->baseUrl);
- $this->assertFalse($config->isSandbox());
- }
- public function testFactoryMethodsCreateIndependentInstances(): void
- {
- $client1 = WangdianFactory::createSandboxClient('sid1', 'key1', 'secret1');
- $client2 = WangdianFactory::createSandboxClient('sid2', 'key2', 'secret2');
- $this->assertNotSame($client1, $client2);
- $this->assertNotSame($client1->getConfig(), $client2->getConfig());
- $this->assertEquals('sid1', $client1->getConfig()->sid);
- $this->assertEquals('sid2', $client2->getConfig()->sid);
- }
- public function testSandboxAndProductionClientsHaveDifferentConfigs(): void
- {
- $sandboxClient = WangdianFactory::createSandboxClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret
- );
-
- $productionClient = WangdianFactory::createProductionClient(
- $this->testSid,
- $this->testAppKey,
- $this->testAppSecret
- );
- $sandboxConfig = $sandboxClient->getConfig();
- $productionConfig = $productionClient->getConfig();
- $this->assertTrue($sandboxConfig->isSandbox());
- $this->assertFalse($productionConfig->isSandbox());
- $this->assertTrue($sandboxConfig->debug);
- $this->assertFalse($productionConfig->debug);
- $this->assertEquals(Config::SANDBOX_BASE_URL, $sandboxConfig->baseUrl);
- $this->assertEquals(Config::PRODUCTION_BASE_URL, $productionConfig->baseUrl);
- }
- public function testFactoryMethodsAreStatic(): void
- {
- $reflection = new \ReflectionClass(WangdianFactory::class);
-
- $methods = [
- 'createSandboxClient',
- 'createProductionClient',
- 'createClient',
- 'createConfig'
- ];
- foreach ($methods as $methodName) {
- $method = $reflection->getMethod($methodName);
- $this->assertTrue($method->isStatic(), "Method {$methodName} should be static");
- $this->assertTrue($method->isPublic(), "Method {$methodName} should be public");
- }
- }
- }
|