202 lines
8.6 KiB
PHP
202 lines
8.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\EcommerceFinance\Services\SupplierDingTalkNotifier;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementReminder;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementReminderTargets;
|
|
use Modules\EcommerceFinance\Services\SupplierStatementStore;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class SupplierStatementReminderTest extends FeatureTestCase
|
|
{
|
|
private const WEBHOOK = 'https://oapi.dingtalk.com/robot/send?access_token=statement-reminder-test';
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->company->makeCurrent();
|
|
}
|
|
|
|
protected function tearDown(): void
|
|
{
|
|
Carbon::setTestNow();
|
|
|
|
parent::tearDown();
|
|
}
|
|
|
|
public function test_due_soon_reminder_is_scoped_to_unuploaded_suppliers_and_never_duplicates(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierStatementReminderTargets::class)->replace(['晨丰', '鸣飞家具']);
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('晨丰', self::WEBHOOK, '', false);
|
|
$notifier->save('鸣飞家具', self::WEBHOOK . '-second', '', false);
|
|
app(SupplierStatementStore::class)->save('晨丰', [], '晨丰-202610.csv', (int) $this->company->id, null);
|
|
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->assertSuccessful();
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-04'])
|
|
->assertSuccessful();
|
|
|
|
Http::assertSentCount(1);
|
|
$request = Http::recorded()->first()[0];
|
|
$text = (string) ($request['markdown']['text'] ?? '');
|
|
$this->assertStringContainsString('statement-reminder-test-second', $request->url());
|
|
$this->assertStringContainsString('供应商月度对账单催办 · 鸣飞家具', $text);
|
|
$this->assertStringContainsString('对账月份:**2026年10月**', $text);
|
|
$this->assertStringContainsString('上传截止:**11月5日 24:00**', $text);
|
|
$this->assertStringContainsString('距离上传截止还有 **2 天**', $text);
|
|
$this->assertStringContainsString('/1/workspace/supplier-portal', $text);
|
|
$this->assertStringNotContainsString('晨丰', $text);
|
|
|
|
$state = json_decode((string) setting('supplier_reconciliation.statement_reminders'), true);
|
|
$this->assertArrayHasKey('鸣飞家具', $state['2026-10']['due_soon'] ?? []);
|
|
$this->assertArrayNotHasKey('晨丰', $state['2026-10']['due_soon'] ?? []);
|
|
}
|
|
|
|
public function test_deadline_and_overdue_stages_each_send_once(): void
|
|
{
|
|
app(SupplierStatementReminderTargets::class)->replace(['晨丰']);
|
|
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, '', false);
|
|
Http::fake(['*' => Http::response(['errcode' => 0, 'errmsg' => 'ok'])]);
|
|
|
|
foreach (['2026-11-03', '2026-11-05', '2026-11-06', '2026-11-07'] as $date) {
|
|
Carbon::setTestNow(Carbon::parse($date . ' 10:00:00', 'Asia/Shanghai'));
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => $date])
|
|
->assertSuccessful();
|
|
}
|
|
|
|
Http::assertSentCount(3);
|
|
$texts = Http::recorded()
|
|
->map(static fn (array $pair): string => (string) ($pair[0]['markdown']['text'] ?? ''))
|
|
->implode("\n");
|
|
$this->assertStringContainsString('今天是上传截止日', $texts);
|
|
$this->assertStringContainsString('上传已逾期 1 天', $texts);
|
|
}
|
|
|
|
public function test_dry_run_reports_uploaded_and_pending_suppliers_without_sending(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierStatementReminderTargets::class)->replace(['晨丰', '鸣飞家具', '未配置厂家']);
|
|
$notifier = app(SupplierDingTalkNotifier::class);
|
|
$notifier->save('晨丰', self::WEBHOOK, '', false);
|
|
$notifier->save('鸣飞家具', self::WEBHOOK . '-second', '', false);
|
|
app(SupplierStatementStore::class)->save('晨丰', [], '晨丰-202610.csv', (int) $this->company->id, null);
|
|
Http::fake();
|
|
|
|
$result = app(SupplierStatementReminder::class)->send(
|
|
Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'),
|
|
(int) $this->company->id,
|
|
true,
|
|
);
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertSame(['鸣飞家具'], $result['previewed']);
|
|
$this->assertSame(['晨丰'], $result['uploaded']);
|
|
$this->assertSame(['未配置厂家'], $result['missing_robot']);
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
public function test_failed_delivery_is_not_recorded_and_can_be_retried(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierStatementReminderTargets::class)->replace(['晨丰']);
|
|
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, '', false);
|
|
Http::fakeSequence()
|
|
->push(['errcode' => 500, 'errmsg' => 'temporary failure'], 500)
|
|
->push(['errcode' => 0, 'errmsg' => 'ok']);
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->assertFailed();
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->assertSuccessful();
|
|
Http::assertSentCount(2);
|
|
$state = json_decode((string) setting('supplier_reconciliation.statement_reminders'), true);
|
|
$this->assertArrayHasKey('晨丰', $state['2026-10']['due_soon'] ?? []);
|
|
}
|
|
|
|
public function test_dry_run_handles_122_configured_suppliers(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
$suppliers = array_map(static fn (int $index): string => '测试供应商' . str_pad(
|
|
(string) $index,
|
|
3,
|
|
'0',
|
|
STR_PAD_LEFT,
|
|
), range(1, 122));
|
|
|
|
$configs = [];
|
|
foreach ($suppliers as $supplier) {
|
|
$configs[$supplier] = [
|
|
'webhook' => \Illuminate\Support\Facades\Crypt::encryptString(self::WEBHOOK),
|
|
'secret' => '',
|
|
'at_all' => false,
|
|
'card_conversation_id' => '',
|
|
'card_robot_code' => '',
|
|
'last_pushed_at' => '',
|
|
'last_push_count' => 0,
|
|
];
|
|
}
|
|
setting()->set(
|
|
'supplier_reconciliation.dingtalk_robots',
|
|
json_encode($configs, JSON_UNESCAPED_UNICODE),
|
|
);
|
|
setting()->save();
|
|
app(SupplierStatementReminderTargets::class)->replace($suppliers);
|
|
Http::fake();
|
|
|
|
$result = app(SupplierStatementReminder::class)->send(
|
|
Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'),
|
|
(int) $this->company->id,
|
|
true,
|
|
);
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertCount(122, $result['previewed']);
|
|
$this->assertSame([], $result['failed']);
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
public function test_reminder_requires_a_current_supplier_allowlist(): void
|
|
{
|
|
Carbon::setTestNow(Carbon::parse('2026-11-03 10:00:00', 'Asia/Shanghai'));
|
|
app(SupplierDingTalkNotifier::class)->save('晨丰', self::WEBHOOK, '', false);
|
|
Http::fake();
|
|
|
|
$this->artisan('supplier-statement:send-reminders', ['--date' => '2026-11-03'])
|
|
->expectsOutputToContain('当前催办名单未配置')
|
|
->assertSuccessful();
|
|
|
|
Http::assertNothingSent();
|
|
$this->assertNull(setting('supplier_reconciliation.statement_reminders'));
|
|
}
|
|
|
|
public function test_supplier_allowlist_can_be_imported_from_a_text_file(): void
|
|
{
|
|
$path = tempnam(sys_get_temp_dir(), 'supplier-reminder-targets-');
|
|
file_put_contents($path, "\xEF\xBB\xBF供应商\n 晨丰 \n鸣飞家具\n晨丰\n");
|
|
|
|
try {
|
|
$this->artisan('supplier-statement:targets:set', [
|
|
'file' => $path,
|
|
'--company' => $this->company->id,
|
|
])->expectsOutputToContain('已保存 2 家当前供应商')
|
|
->assertSuccessful();
|
|
} finally {
|
|
@unlink($path);
|
|
}
|
|
|
|
$this->assertSame(
|
|
['晨丰', '鸣飞家具'],
|
|
app(SupplierStatementReminderTargets::class)->names(),
|
|
);
|
|
}
|
|
}
|