BaseServiceTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Wangdian\Tests\Unit\Services;
  4. use PHPUnit\Framework\TestCase;
  5. use SixShop\Wangdian\Client;
  6. use SixShop\Wangdian\Services\BaseService;
  7. use SixShop\Wangdian\Tests\TestConfig;
  8. /**
  9. * Mock service for testing BaseService functionality
  10. */
  11. class MockService extends BaseService
  12. {
  13. public function testValidateRequired(array $params, array $required): void
  14. {
  15. $this->validateRequired($params, $required);
  16. }
  17. public function testFilterParams(array $params): array
  18. {
  19. return $this->filterParams($params);
  20. }
  21. public function testEncodeIfArray(mixed $value): mixed
  22. {
  23. return $this->encodeIfArray($value);
  24. }
  25. }
  26. class BaseServiceTest extends TestCase
  27. {
  28. private MockService $service;
  29. protected function setUp(): void
  30. {
  31. $testConfig = TestConfig::get();
  32. // Create a minimal client mock for constructor
  33. $client = $this->getMockBuilder(Client::class)
  34. ->disableOriginalConstructor()
  35. ->getMock();
  36. $this->service = new MockService($client);
  37. }
  38. public function testConstructor(): void
  39. {
  40. $this->assertInstanceOf(BaseService::class, $this->service);
  41. }
  42. public function testValidateRequiredSuccess(): void
  43. {
  44. $params = [
  45. 'required_field1' => 'value1',
  46. 'required_field2' => 'value2',
  47. 'optional_field' => 'optional_value'
  48. ];
  49. // Should not throw exception
  50. $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
  51. $this->assertTrue(true); // Assert that we reach this point
  52. }
  53. public function testValidateRequiredMissingField(): void
  54. {
  55. $params = [
  56. 'required_field1' => 'value1',
  57. 'optional_field' => 'optional_value'
  58. ];
  59. $this->expectException(\InvalidArgumentException::class);
  60. $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
  61. $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
  62. }
  63. public function testValidateRequiredEmptyString(): void
  64. {
  65. $params = [
  66. 'required_field1' => 'value1',
  67. 'required_field2' => '',
  68. ];
  69. $this->expectException(\InvalidArgumentException::class);
  70. $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
  71. $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
  72. }
  73. public function testValidateRequiredWhitespaceString(): void
  74. {
  75. $params = [
  76. 'required_field1' => 'value1',
  77. 'required_field2' => ' ',
  78. ];
  79. $this->expectException(\InvalidArgumentException::class);
  80. $this->expectExceptionMessage("Required parameter 'required_field2' is missing or empty");
  81. $this->service->testValidateRequired($params, ['required_field1', 'required_field2']);
  82. }
  83. public function testFilterParams(): void
  84. {
  85. $params = [
  86. 'valid_param1' => 'value1',
  87. 'valid_param2' => 'value2',
  88. 'null_param' => null,
  89. 'empty_string_param' => '',
  90. 'zero_param' => 0,
  91. 'false_param' => false,
  92. 'valid_param3' => 'value3'
  93. ];
  94. $filtered = $this->service->testFilterParams($params);
  95. $expected = [
  96. 'valid_param1' => 'value1',
  97. 'valid_param2' => 'value2',
  98. 'zero_param' => 0,
  99. 'false_param' => false,
  100. 'valid_param3' => 'value3'
  101. ];
  102. $this->assertEquals($expected, $filtered);
  103. }
  104. public function testEncodeIfArrayWithArray(): void
  105. {
  106. $array = ['key1' => 'value1', 'key2' => 'value2'];
  107. $result = $this->service->testEncodeIfArray($array);
  108. $this->assertIsString($result);
  109. $this->assertEquals('{"key1":"value1","key2":"value2"}', $result);
  110. }
  111. public function testEncodeIfArrayWithNestedArray(): void
  112. {
  113. $array = [
  114. 'level1' => [
  115. 'level2' => ['level3' => 'value']
  116. ]
  117. ];
  118. $result = $this->service->testEncodeIfArray($array);
  119. $this->assertIsString($result);
  120. $decoded = json_decode($result, true);
  121. $this->assertEquals($array, $decoded);
  122. }
  123. public function testEncodeIfArrayWithString(): void
  124. {
  125. $string = 'test_string';
  126. $result = $this->service->testEncodeIfArray($string);
  127. $this->assertEquals($string, $result);
  128. $this->assertIsString($result);
  129. }
  130. public function testEncodeIfArrayWithNumber(): void
  131. {
  132. $number = 12345;
  133. $result = $this->service->testEncodeIfArray($number);
  134. $this->assertEquals($number, $result);
  135. $this->assertIsInt($result);
  136. }
  137. public function testEncodeIfArrayWithNull(): void
  138. {
  139. $result = $this->service->testEncodeIfArray(null);
  140. $this->assertNull($result);
  141. }
  142. public function testEncodeIfArrayWithBoolean(): void
  143. {
  144. $result = $this->service->testEncodeIfArray(true);
  145. $this->assertTrue($result);
  146. $result = $this->service->testEncodeIfArray(false);
  147. $this->assertFalse($result);
  148. }
  149. public function testEncodeIfArrayWithUtf8(): void
  150. {
  151. $array = ['中文' => '测试', '数据' => ['嵌套' => '值']];
  152. $result = $this->service->testEncodeIfArray($array);
  153. $this->assertIsString($result);
  154. $this->assertStringContainsString('中文', $result);
  155. $this->assertStringContainsString('测试', $result);
  156. $decoded = json_decode($result, true);
  157. $this->assertEquals($array, $decoded);
  158. }
  159. }