92 lines
5.5 KiB
PHP
92 lines
5.5 KiB
PHP
<?php
|
||
|
||
namespace Tests\Unit\EcommerceFinance;
|
||
|
||
use Modules\EcommerceFinance\Services\PlantingDingTalkForm as Form;
|
||
use PHPUnit\Framework\TestCase;
|
||
|
||
class PlantingDingTalkFormTest extends TestCase
|
||
{
|
||
/** 结构仿照钉钉表单接口返回(schemaContent.items)。 */
|
||
public static function schema(): array
|
||
{
|
||
$field = static fn (string $type, string $label, bool $required = true, array $extra = []): array => ['componentName' => $type, 'props' => ['label' => $label, 'required' => $required] + $extra];
|
||
|
||
return [
|
||
$field('TextNote', '说明'),
|
||
$field('DDSelectField', '报销类型', true, ['options' => [['key' => 'a', 'value' => '种菜核销'], ['key' => 'b', 'value' => '日常报销']]]),
|
||
['componentName' => 'TableField', 'props' => ['label' => '收款费用明细', 'required' => true], 'children' => [
|
||
$field('NumberField', '序号'),
|
||
$field('DepartmentField', '费用承担部门(利润中心)'),
|
||
$field('TextField', '店铺'),
|
||
$field('TextField', '用途'),
|
||
$field('MoneyField', '收款金额'),
|
||
$field('TextField', '备注', false),
|
||
]],
|
||
$field('MoneyField', '收款合计'),
|
||
$field('TextField', '收款人'),
|
||
$field('TextField', '开户行'),
|
||
$field('TextField', '账号'),
|
||
['componentName' => 'TableField', 'props' => ['label' => '付款明细', 'required' => false], 'children' => [
|
||
$field('TextField', '对应序号'),
|
||
$field('MoneyField', '付款金额'),
|
||
$field('DDSelectField', '付款方式', true, ['options' => ['{"key":"1","value":"银行卡"}', '{"key":"2","value":"支付宝"}']]),
|
||
$field('DDDateField', '打款时间', false),
|
||
]],
|
||
$field('DDAttachment', '附件'),
|
||
];
|
||
}
|
||
|
||
public static function summary(): array
|
||
{
|
||
return [
|
||
'bill_date' => '2026-11-11', 'purpose' => '11.11种菜报销', 'total' => '130.50',
|
||
'ding_lines' => [
|
||
['department' => '运营中心A', 'shop' => '欧意朗', 'purpose' => '11.11种菜报销', 'payment_method' => '银行卡', 'cents' => 10050],
|
||
['department' => '运营中心G', 'shop' => '其他', 'purpose' => '11.11种菜报销', 'payment_method' => '银行卡', 'cents' => 3000],
|
||
],
|
||
];
|
||
}
|
||
|
||
public function test_fills_form_by_labels_and_attachment(): void
|
||
{
|
||
$attachment = ['spaceId' => 'S1', 'fileName' => '种菜报销明细11.11.xlsx', 'fileSize' => '123', 'fileType' => 'xlsx', 'fileId' => 'F1'];
|
||
$result = (new Form())->build(self::schema(), self::summary(), $attachment, '种菜报销明细11.11.xlsx');
|
||
|
||
$this->assertSame([], $result['missing']);
|
||
$this->assertSame([], $result['invalid']);
|
||
$values = array_column($result['values'], 'value', 'name');
|
||
$this->assertSame('种菜核销', $values['报销类型']);
|
||
$this->assertSame('130.50', $values['收款合计']);
|
||
$this->assertSame('账号详见附件', $values['收款人']);
|
||
$this->assertSame('账号详见附件', $values['账号']);
|
||
$this->assertSame([
|
||
[['name' => '序号', 'value' => '1'], ['name' => '费用承担部门(利润中心)', 'value' => '运营中心A'], ['name' => '店铺', 'value' => '欧意朗'], ['name' => '用途', 'value' => '11.11种菜报销'], ['name' => '收款金额', 'value' => '100.50']],
|
||
[['name' => '序号', 'value' => '2'], ['name' => '费用承担部门(利润中心)', 'value' => '运营中心G'], ['name' => '店铺', 'value' => '其他'], ['name' => '用途', 'value' => '11.11种菜报销'], ['name' => '收款金额', 'value' => '30.00']],
|
||
], json_decode($values['收款费用明细'], true));
|
||
$this->assertSame([[['name' => '对应序号', 'value' => '1-2'], ['name' => '付款金额', 'value' => '130.50'], ['name' => '付款方式', 'value' => '银行卡']]], json_decode($values['付款明细'], true));
|
||
$this->assertSame([$attachment], json_decode($values['附件'], true));
|
||
$this->assertArrayNotHasKey('说明', $values);
|
||
|
||
// 预览时不带附件:不出附件值,但不算缺字段。
|
||
$preview = (new Form())->build(self::schema(), self::summary(), null, '种菜报销明细11.11.xlsx');
|
||
$this->assertSame([], $preview['missing']);
|
||
$this->assertArrayNotHasKey('附件', array_column($preview['values'], 'value', 'name'));
|
||
}
|
||
|
||
public function test_reports_required_fields_it_cannot_fill_and_bad_options(): void
|
||
{
|
||
$schema = self::schema();
|
||
$schema[] = ['componentName' => 'DDDateField', 'props' => ['label' => '报销日期', 'required' => true]];
|
||
$schema[1]['props']['options'] = [['key' => 'b', 'value' => '日常报销']];
|
||
array_pop($schema[2]['children']);
|
||
$schema[2]['children'][] = ['componentName' => 'TextField', 'props' => ['label' => '经办人', 'required' => true]];
|
||
$withoutAttachment = array_values(array_filter($schema, static fn (array $item): bool => $item['componentName'] !== 'DDAttachment'));
|
||
|
||
$result = (new Form())->build($withoutAttachment, self::summary(), null, 'x.xlsx');
|
||
|
||
$this->assertSame(['收款费用明细 → 经办人', '报销日期', '附件(表单里没有附件字段,无法附上打款明细)'], $result['missing']);
|
||
$this->assertSame(['报销类型(选项里没有「种菜核销」)'], $result['invalid']);
|
||
}
|
||
}
|