42 lines
1.9 KiB
PHP
42 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use App\Services\WeightEstimator;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
/**
|
|
* 参考重量估算:重量单位直算、规格解析折算、无法解析兜底(单位:斤)
|
|
*/
|
|
class WeightEstimatorTest extends TestCase
|
|
{
|
|
/** 计价单位本身是重量单位:订货量即重量(忽略规格) */
|
|
public function test_weight_unit_quantity_is_weight(): void
|
|
{
|
|
$this->assertSame('5.000', WeightEstimator::estimate('10斤/箱', '斤', '5'));
|
|
$this->assertSame('6.000', WeightEstimator::estimate('', '公斤', '3'));
|
|
$this->assertSame('6.000', WeightEstimator::estimate('', 'KG', '3'));
|
|
$this->assertSame('1.000', WeightEstimator::estimate('', '克', '500'));
|
|
}
|
|
|
|
/** 规格解析每件重量 × 订货量 */
|
|
public function test_spec_per_unit_weight(): void
|
|
{
|
|
$this->assertSame('30.000', WeightEstimator::estimate('10斤/箱', '箱', '3'));
|
|
$this->assertSame('75.000', WeightEstimator::estimate('25斤/袋', '袋', '3'));
|
|
$this->assertSame('3.000', WeightEstimator::estimate('500g/袋', '袋', '3'));
|
|
$this->assertSame('3.000', WeightEstimator::estimate('500克/袋', '袋', '3'));
|
|
$this->assertSame('9.000', WeightEstimator::estimate('1.5kg/箱', '箱', '3'));
|
|
$this->assertSame('12.000', WeightEstimator::estimate('2公斤/筐', '筐', '3'));
|
|
$this->assertSame('15.000', WeightEstimator::estimate('10斤/箱', '箱', '1.5'), '订货量可为小数');
|
|
}
|
|
|
|
/** 规格无法解析:裸数字按斤计,无数字计 0 */
|
|
public function test_unparsable_spec_fallback(): void
|
|
{
|
|
$this->assertSame('20.000', WeightEstimator::estimate('10/箱', '箱', '2'), '裸数字按斤计');
|
|
$this->assertSame('0.000', WeightEstimator::estimate('散装', '箱', '3'));
|
|
$this->assertSame('0.000', WeightEstimator::estimate('', '箱', '3'));
|
|
}
|
|
}
|