| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- <?php
- namespace App\Module\Transfer\Tests\Unit;
- use App\Module\Transfer\Validators\AmountValidator;
- use Tests\TestCase;
- /**
- * 金额验证器单元测试
- */
- class AmountValidatorTest extends TestCase
- {
- /**
- * 测试有效金额
- */
- public function test_valid_amounts()
- {
- $validAmounts = [
- '1.0000000000',
- '100.5000000000',
- '0.0000000001',
- '999999999.9999999999',
- '123.456789',
- '1000',
- '0.1',
- ];
- foreach ($validAmounts as $amount) {
- $validator = new AmountValidator($amount);
- $this->assertTrue($validator->validate(), "金额 {$amount} 应该是有效的");
- }
- }
- /**
- * 测试无效金额
- */
- public function test_invalid_amounts()
- {
- $invalidAmounts = [
- '', // 空值
- '0', // 零值
- '-100', // 负数
- 'abc', // 非数字
- '1.23.45', // 多个小数点
- '1e10', // 科学计数法
- '1000000000000', // 超过最大值
- ];
- foreach ($invalidAmounts as $amount) {
- $validator = new AmountValidator($amount);
- $this->assertFalse($validator->validate(), "金额 {$amount} 应该是无效的");
- }
- }
- /**
- * 测试小数位数限制
- */
- public function test_decimal_places_limit()
- {
- // 测试超过10位小数的金额
- $validator = new AmountValidator('1.12345678901', 10);
- $this->assertFalse($validator->validate());
- // 测试正好10位小数的金额
- $validator = new AmountValidator('1.1234567890', 10);
- $this->assertTrue($validator->validate());
- // 测试少于10位小数的金额
- $validator = new AmountValidator('1.123456789', 10);
- $this->assertTrue($validator->validate());
- }
- /**
- * 测试自定义范围
- */
- public function test_custom_range()
- {
- $minAmount = '10.0000000000';
- $maxAmount = '1000.0000000000';
- // 测试范围内的金额
- $validator = new AmountValidator('100.0000000000', 10, $minAmount, $maxAmount);
- $this->assertTrue($validator->validate());
- // 测试小于最小值的金额
- $validator = new AmountValidator('5.0000000000', 10, $minAmount, $maxAmount);
- $this->assertFalse($validator->validate());
- // 测试大于最大值的金额
- $validator = new AmountValidator('2000.0000000000', 10, $minAmount, $maxAmount);
- $this->assertFalse($validator->validate());
- }
- /**
- * 测试金额格式化
- */
- public function test_amount_formatting()
- {
- $validator = new AmountValidator('100.1000000000');
- $this->assertEquals('100.1', $validator->format());
- $validator = new AmountValidator('100.0000000000');
- $this->assertEquals('100', $validator->format());
- $validator = new AmountValidator('0.1000000000');
- $this->assertEquals('0.1', $validator->format());
- }
- /**
- * 测试标准格式转换
- */
- public function test_standard_format()
- {
- $validator = new AmountValidator('100.1');
- $this->assertEquals('100.1000000000', $validator->toStandardFormat());
- $validator = new AmountValidator('100');
- $this->assertEquals('100.0000000000', $validator->toStandardFormat());
- }
- /**
- * 测试静态验证方法
- */
- public function test_static_validation()
- {
- $this->assertTrue(AmountValidator::isValid('100.0000000000'));
- $this->assertFalse(AmountValidator::isValid('0'));
- $this->assertFalse(AmountValidator::isValid('-100'));
- }
- /**
- * 测试金额比较
- */
- public function test_amount_comparison()
- {
- $this->assertEquals(0, AmountValidator::compare('100.0000000000', '100.0000000000'));
- $this->assertEquals(1, AmountValidator::compare('200.0000000000', '100.0000000000'));
- $this->assertEquals(-1, AmountValidator::compare('100.0000000000', '200.0000000000'));
- }
- /**
- * 测试金额运算
- */
- public function test_amount_calculations()
- {
- // 加法
- $result = AmountValidator::add('100.0000000000', '50.0000000000');
- $this->assertEquals('150.0000000000', $result);
- // 减法
- $result = AmountValidator::subtract('100.0000000000', '30.0000000000');
- $this->assertEquals('70.0000000000', $result);
- // 乘法
- $result = AmountValidator::multiply('100.0000000000', '2.0000');
- $this->assertEquals('200.0000000000', $result);
- // 除法
- $result = AmountValidator::divide('100.0000000000', '2.0000');
- $this->assertEquals('50.0000000000', $result);
- }
- /**
- * 测试除零异常
- */
- public function test_division_by_zero()
- {
- $this->expectException(\InvalidArgumentException::class);
- AmountValidator::divide('100.0000000000', '0');
- }
- /**
- * 测试手续费验证
- */
- public function test_fee_validation()
- {
- $validator = new AmountValidator('100.0000000000');
-
- // 金额大于手续费,应该通过
- $this->assertTrue($validator->validateWithFee('10.0000000000'));
-
- // 金额等于手续费,应该失败
- $this->assertFalse($validator->validateWithFee('100.0000000000'));
-
- // 金额小于手续费,应该失败
- $this->assertFalse($validator->validateWithFee('200.0000000000'));
- }
- /**
- * 测试边界值
- */
- public function test_boundary_values()
- {
- // 测试最小有效值
- $validator = new AmountValidator('0.0000000001');
- $this->assertTrue($validator->validate());
- // 测试最大有效值
- $validator = new AmountValidator('999999999.9999999999');
- $this->assertTrue($validator->validate());
- // 测试刚好超出范围的值
- $validator = new AmountValidator('1000000000.0000000000');
- $this->assertFalse($validator->validate());
- }
- }
|