| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- <?php
- declare(strict_types=1);
- namespace SixShop\Wangdian\Tests\Unit\Services;
- use PHPUnit\Framework\TestCase;
- use SixShop\Wangdian\Client;
- use SixShop\Wangdian\Services\BaseService;
- use SixShop\Wangdian\Tests\TestConfig;
- /**
- * Mock service for testing BaseService functionality
- */
- class MockService extends BaseService
- {
- public function testValidateRequired(array $params, array $required): void
- {
- $this->validateRequired($params, $required);
- }
- public function testFilterParams(array $params): array
- {
- return $this->filterParams($params);
- }
- public function testEncodeIfArray(mixed $value): mixed
- {
- return $this->encodeIfArray($value);
- }
- }
- class BaseServiceTest extends TestCase
- {
- private MockService $service;
- protected function setUp(): void
- {
- $testConfig = TestConfig::get();
-
- // Create a minimal client mock for constructor
- $client = $this->getMockBuilder(Client::class)
- ->disableOriginalConstructor()
- ->getMock();
-
- $this->service = new MockService($client);
- }
- public function testConstructor(): void
- {
- $this->assertInstanceOf(BaseService::class, $this->service);
- }
- public function testValidateRequiredSuccess(): void
- {
- $params = [
- 'required_field1' => 'value1',
- 'required_field2' => 'value2',
- 'optional_field' => 'optional_value'
- ];
- // Should not throw exception
- $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
- $this->assertTrue(true); // Assert that we reach this point
- }
- public function testValidateRequiredMissingField(): void
- {
- $params = [
- 'required_field1' => 'value1',
- 'optional_field' => 'optional_value'
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
- $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
- }
- public function testValidateRequiredEmptyString(): void
- {
- $params = [
- 'required_field1' => 'value1',
- 'required_field2' => '',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
- $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
- }
- public function testValidateRequiredWhitespaceString(): void
- {
- $params = [
- 'required_field1' => 'value1',
- 'required_field2' => ' ',
- ];
- $this->expectException(\InvalidArgumentException::class);
- $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
- $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
- }
- public function testFilterParams(): void
- {
- $params = [
- 'valid_param1' => 'value1',
- 'valid_param2' => 'value2',
- 'null_param' => null,
- 'empty_string_param' => '',
- 'zero_param' => 0,
- 'false_param' => false,
- 'valid_param3' => 'value3'
- ];
- $filtered = $this->service->testFilterParams($params);
- $expected = [
- 'valid_param1' => 'value1',
- 'valid_param2' => 'value2',
- 'zero_param' => 0,
- 'false_param' => false,
- 'valid_param3' => 'value3'
- ];
- $this->assertEquals($expected, $filtered);
- }
- public function testEncodeIfArrayWithArray(): void
- {
- $array = ['key1' => 'value1', 'key2' => 'value2'];
- $result = $this->service->testEncodeIfArray($array);
-
- $this->assertIsString($result);
- $this->assertEquals('{"key1":"value1","key2":"value2"}', $result);
- }
- public function testEncodeIfArrayWithNestedArray(): void
- {
- $array = [
- 'level1' => [
- 'level2' => ['level3' => 'value']
- ]
- ];
- $result = $this->service->testEncodeIfArray($array);
-
- $this->assertIsString($result);
- $decoded = json_decode($result, true);
- $this->assertEquals($array, $decoded);
- }
- public function testEncodeIfArrayWithString(): void
- {
- $string = 'test_string';
- $result = $this->service->testEncodeIfArray($string);
-
- $this->assertEquals($string, $result);
- $this->assertIsString($result);
- }
- public function testEncodeIfArrayWithNumber(): void
- {
- $number = 12345;
- $result = $this->service->testEncodeIfArray($number);
-
- $this->assertEquals($number, $result);
- $this->assertIsInt($result);
- }
- public function testEncodeIfArrayWithNull(): void
- {
- $result = $this->service->testEncodeIfArray(null);
-
- $this->assertNull($result);
- }
- public function testEncodeIfArrayWithBoolean(): void
- {
- $result = $this->service->testEncodeIfArray(true);
- $this->assertTrue($result);
-
- $result = $this->service->testEncodeIfArray(false);
- $this->assertFalse($result);
- }
- public function testEncodeIfArrayWithUtf8(): void
- {
- $array = ['中文' => '测试', '数据' => ['嵌套' => '值']];
- $result = $this->service->testEncodeIfArray($array);
-
- $this->assertIsString($result);
- $this->assertStringContainsString('中文', $result);
- $this->assertStringContainsString('测试', $result);
- $decoded = json_decode($result, true);
- $this->assertEquals($array, $decoded);
- }
- }
|