AmountValidatorTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace App\Module\Transfer\Tests\Unit;
  3. use App\Module\Transfer\Validators\AmountValidator;
  4. use Tests\TestCase;
  5. /**
  6. * 金额验证器单元测试
  7. */
  8. class AmountValidatorTest extends TestCase
  9. {
  10. /**
  11. * 测试有效金额
  12. */
  13. public function test_valid_amounts()
  14. {
  15. $validAmounts = [
  16. '1.0000000000',
  17. '100.5000000000',
  18. '0.0000000001',
  19. '999999999.9999999999',
  20. '123.456789',
  21. '1000',
  22. '0.1',
  23. ];
  24. foreach ($validAmounts as $amount) {
  25. $validator = new AmountValidator($amount);
  26. $this->assertTrue($validator->validate(), "金额 {$amount} 应该是有效的");
  27. }
  28. }
  29. /**
  30. * 测试无效金额
  31. */
  32. public function test_invalid_amounts()
  33. {
  34. $invalidAmounts = [
  35. '', // 空值
  36. '0', // 零值
  37. '-100', // 负数
  38. 'abc', // 非数字
  39. '1.23.45', // 多个小数点
  40. '1e10', // 科学计数法
  41. '1000000000000', // 超过最大值
  42. ];
  43. foreach ($invalidAmounts as $amount) {
  44. $validator = new AmountValidator($amount);
  45. $this->assertFalse($validator->validate(), "金额 {$amount} 应该是无效的");
  46. }
  47. }
  48. /**
  49. * 测试小数位数限制
  50. */
  51. public function test_decimal_places_limit()
  52. {
  53. // 测试超过10位小数的金额
  54. $validator = new AmountValidator('1.12345678901', 10);
  55. $this->assertFalse($validator->validate());
  56. // 测试正好10位小数的金额
  57. $validator = new AmountValidator('1.1234567890', 10);
  58. $this->assertTrue($validator->validate());
  59. // 测试少于10位小数的金额
  60. $validator = new AmountValidator('1.123456789', 10);
  61. $this->assertTrue($validator->validate());
  62. }
  63. /**
  64. * 测试自定义范围
  65. */
  66. public function test_custom_range()
  67. {
  68. $minAmount = '10.0000000000';
  69. $maxAmount = '1000.0000000000';
  70. // 测试范围内的金额
  71. $validator = new AmountValidator('100.0000000000', 10, $minAmount, $maxAmount);
  72. $this->assertTrue($validator->validate());
  73. // 测试小于最小值的金额
  74. $validator = new AmountValidator('5.0000000000', 10, $minAmount, $maxAmount);
  75. $this->assertFalse($validator->validate());
  76. // 测试大于最大值的金额
  77. $validator = new AmountValidator('2000.0000000000', 10, $minAmount, $maxAmount);
  78. $this->assertFalse($validator->validate());
  79. }
  80. /**
  81. * 测试金额格式化
  82. */
  83. public function test_amount_formatting()
  84. {
  85. $validator = new AmountValidator('100.1000000000');
  86. $this->assertEquals('100.1', $validator->format());
  87. $validator = new AmountValidator('100.0000000000');
  88. $this->assertEquals('100', $validator->format());
  89. $validator = new AmountValidator('0.1000000000');
  90. $this->assertEquals('0.1', $validator->format());
  91. }
  92. /**
  93. * 测试标准格式转换
  94. */
  95. public function test_standard_format()
  96. {
  97. $validator = new AmountValidator('100.1');
  98. $this->assertEquals('100.1000000000', $validator->toStandardFormat());
  99. $validator = new AmountValidator('100');
  100. $this->assertEquals('100.0000000000', $validator->toStandardFormat());
  101. }
  102. /**
  103. * 测试静态验证方法
  104. */
  105. public function test_static_validation()
  106. {
  107. $this->assertTrue(AmountValidator::isValid('100.0000000000'));
  108. $this->assertFalse(AmountValidator::isValid('0'));
  109. $this->assertFalse(AmountValidator::isValid('-100'));
  110. }
  111. /**
  112. * 测试金额比较
  113. */
  114. public function test_amount_comparison()
  115. {
  116. $this->assertEquals(0, AmountValidator::compare('100.0000000000', '100.0000000000'));
  117. $this->assertEquals(1, AmountValidator::compare('200.0000000000', '100.0000000000'));
  118. $this->assertEquals(-1, AmountValidator::compare('100.0000000000', '200.0000000000'));
  119. }
  120. /**
  121. * 测试金额运算
  122. */
  123. public function test_amount_calculations()
  124. {
  125. // 加法
  126. $result = AmountValidator::add('100.0000000000', '50.0000000000');
  127. $this->assertEquals('150.0000000000', $result);
  128. // 减法
  129. $result = AmountValidator::subtract('100.0000000000', '30.0000000000');
  130. $this->assertEquals('70.0000000000', $result);
  131. // 乘法
  132. $result = AmountValidator::multiply('100.0000000000', '2.0000');
  133. $this->assertEquals('200.0000000000', $result);
  134. // 除法
  135. $result = AmountValidator::divide('100.0000000000', '2.0000');
  136. $this->assertEquals('50.0000000000', $result);
  137. }
  138. /**
  139. * 测试除零异常
  140. */
  141. public function test_division_by_zero()
  142. {
  143. $this->expectException(\InvalidArgumentException::class);
  144. AmountValidator::divide('100.0000000000', '0');
  145. }
  146. /**
  147. * 测试手续费验证
  148. */
  149. public function test_fee_validation()
  150. {
  151. $validator = new AmountValidator('100.0000000000');
  152. // 金额大于手续费,应该通过
  153. $this->assertTrue($validator->validateWithFee('10.0000000000'));
  154. // 金额等于手续费,应该失败
  155. $this->assertFalse($validator->validateWithFee('100.0000000000'));
  156. // 金额小于手续费,应该失败
  157. $this->assertFalse($validator->validateWithFee('200.0000000000'));
  158. }
  159. /**
  160. * 测试边界值
  161. */
  162. public function test_boundary_values()
  163. {
  164. // 测试最小有效值
  165. $validator = new AmountValidator('0.0000000001');
  166. $this->assertTrue($validator->validate());
  167. // 测试最大有效值
  168. $validator = new AmountValidator('999999999.9999999999');
  169. $this->assertTrue($validator->validate());
  170. // 测试刚好超出范围的值
  171. $validator = new AmountValidator('1000000000.0000000000');
  172. $this->assertFalse($validator->validate());
  173. }
  174. }