57 lines
2.7 KiB
PHP
57 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use InvalidArgumentException;
|
|
use Modules\EcommerceFinance\Services\PlantingQuotaRules as Rules;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class PlantingQuotaRulesTest extends TestCase
|
|
{
|
|
public function test_seed_csv_from_maixiang_parses_all_309_rules(): void
|
|
{
|
|
$rules = (new Rules())->parseCsv((string) file_get_contents(dirname(__DIR__, 3) . '/resources/data/' . Rules::SEED));
|
|
|
|
$this->assertCount(309, $rules);
|
|
$types = array_count_values(array_column($rules, 'rule_type'));
|
|
ksort($types);
|
|
$this->assertSame(['ider' => 9, 'platform' => 83, 'shop' => 51, 'shop_level' => 166], $types);
|
|
$this->assertSame(3, count(array_filter($rules, static fn (array $rule): bool => ! $rule['is_enabled'])));
|
|
$tongxuan = array_values(array_filter($rules, static fn (array $rule): bool => $rule['ider'] === '通玄'))[0];
|
|
$this->assertSame(['80000.00', '3000.00', '500.00'], [$tongxuan['month_limit'], $tongxuan['day_limit'], $tongxuan['single_limit']]);
|
|
}
|
|
|
|
public function test_normalize_validates_by_rule_type(): void
|
|
{
|
|
$rules = new Rules();
|
|
$rule = $rules->normalize(['rule_type' => '平台+职级', 'platform' => '天猫', 'position_level' => 'p2', 'shop_name' => '多填的店铺', 'day_limit' => '1,500', 'single_limit' => '', 'is_enabled' => '1']);
|
|
$this->assertSame('platform', $rule['rule_type']);
|
|
$this->assertSame('P2', $rule['position_level']);
|
|
$this->assertSame('', $rule['shop_name']);
|
|
$this->assertSame('1500.00', $rule['day_limit']);
|
|
$this->assertNull($rule['single_limit']);
|
|
$this->assertTrue($rule['is_enabled']);
|
|
|
|
foreach ([
|
|
['rule_type' => 'ider', 'day_limit' => '1'],
|
|
['rule_type' => 'platform', 'platform' => '天猫', 'position_level' => 'P9', 'day_limit' => '1'],
|
|
['rule_type' => 'platform', 'platform' => '快手', 'position_level' => 'P2', 'day_limit' => '1'],
|
|
['rule_type' => 'shop', 'shop_name' => '某店'],
|
|
['rule_type' => 'shop', 'shop_name' => '某店', 'day_limit' => '-1'],
|
|
] as $input) {
|
|
try {
|
|
$rules->normalize($input);
|
|
$this->fail('应校验失败:' . json_encode($input, JSON_UNESCAPED_UNICODE));
|
|
} catch (InvalidArgumentException) {
|
|
$this->assertTrue(true);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_duplicate_rows_in_csv_are_rejected(): void
|
|
{
|
|
$this->expectException(InvalidArgumentException::class);
|
|
(new Rules())->parseCsv("rule_type,platform,shop_name,channel,position_level,ider,month_limit,day_limit,single_limit,is_enabled,remark\nider,,,,,通玄,,100,,1,\nider,,,,,通玄,,200,,1,\n");
|
|
}
|
|
}
|