82 lines
3.1 KiB
PHP
82 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit;
|
|
|
|
use Illuminate\Container\Container;
|
|
use Illuminate\Support\Facades\Facade;
|
|
use Illuminate\Translation\ArrayLoader;
|
|
use Illuminate\Translation\Translator;
|
|
use Illuminate\Validation\Factory;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Modules\EcommerceFinance\Services\PayablesFieldNormalizer;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class PayablesFieldNormalizerTest extends TestCase
|
|
{
|
|
private $previousApp;
|
|
private const FIELDS = ['amount' => ['label' => '金额', 'type' => 'number']];
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->previousApp = Facade::getFacadeApplication();
|
|
$app = new Container;
|
|
$app->instance('validator', new Factory(new Translator(new ArrayLoader, 'zh_CN'), $app));
|
|
Facade::clearResolvedInstances();
|
|
Facade::setFacadeApplication($app);
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Facade::clearResolvedInstances();
|
|
Facade::setFacadeApplication($this->previousApp);
|
|
parent::tearDown();
|
|
}
|
|
|
|
public static function tailValues(): array
|
|
{
|
|
return [
|
|
['9.220000000000001', '9.2200'],
|
|
['9.699999999999999', '9.7000'],
|
|
['-75.70999999999999', '-75.7100'],
|
|
['1.234500000000001', '1.2345'],
|
|
['9.220000000001', '9.2200'],
|
|
['.09999999999999999', '0.1000'],
|
|
['0.000000000000001', '0.0000'],
|
|
['-0.000000000000001', '0.0000'],
|
|
['9999999999999999.999900000000001', '9999999999999999.9999'],
|
|
['12.34000', '12.3400'],
|
|
];
|
|
}
|
|
|
|
#[DataProvider('tailValues')]
|
|
public function test_import_removes_only_micro_decimal_tails(string $input, string $expected): void
|
|
{
|
|
$this->assertSame(['amount' => $expected], (new PayablesFieldNormalizer)->normalizeImport(self::FIELDS, ['amount' => $input]));
|
|
}
|
|
|
|
public static function invalidValues(): array
|
|
{
|
|
return array_map(fn ($value) => [$value], ['9.22001', '9.220000000002', '0.000000001', '1.23456',
|
|
'9999999999999999.99999', '10000000000000000.00000', 'NaN', 'INF', '1e-5', '=1+2']);
|
|
}
|
|
|
|
#[DataProvider('invalidValues')]
|
|
public function test_import_still_rejects_real_excess_precision_and_invalid_amounts(string $value): void
|
|
{
|
|
$this->expectException(ValidationException::class);
|
|
(new PayablesFieldNormalizer)->normalizeImport(self::FIELDS, ['amount' => $value]);
|
|
}
|
|
|
|
public function test_manual_edit_stays_strict_and_import_preserves_text_and_blank_zero(): void
|
|
{
|
|
$normalizer = new PayablesFieldNormalizer;
|
|
$fields = self::FIELDS + ['code' => ['label' => '编码', 'type' => 'text']];
|
|
$this->assertSame(['amount' => '0', 'code' => '00123456789012345678'],
|
|
$normalizer->normalizeImport($fields, ['amount' => '0', 'code' => '00123456789012345678']));
|
|
$this->assertSame(['amount' => null], $normalizer->normalizeImport(self::FIELDS, ['amount' => '']));
|
|
$this->expectException(ValidationException::class);
|
|
$normalizer->normalize(self::FIELDS, ['amount' => '9.220000000000001']);
|
|
}
|
|
}
|