100 lines
3.5 KiB
PHP
100 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Services\BillSyncWindowAuditor;
|
|
use Tests\TestCase;
|
|
|
|
class AuditBillSyncWindowTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
$this->createTables();
|
|
}
|
|
|
|
public function test_it_detects_missing_dates_and_late_partial_generation(): void
|
|
{
|
|
$connection = DB::connection('business_mirror');
|
|
foreach (['2026-11-17', '2026-11-18', '2026-11-19'] as $date) {
|
|
$this->insertDay($connection, 'taobao_alipay_bill_rows', 'occurred_at', $date, 10, 3, $date === '2026-11-19' ? 0 : 10);
|
|
$this->insertDay($connection, 'pdd_settlement_bill_rows', 'occurred_at', $date, 10, 3);
|
|
$this->insertDay($connection, 'dewu_settlement_bill_rows', 'bill_date', $date, 10, 3);
|
|
$this->insertDay(
|
|
$connection,
|
|
'taobao_wechat_bill_rows',
|
|
'occurred_at',
|
|
$date,
|
|
10,
|
|
4,
|
|
$date === '2026-11-18' ? 1 : 10
|
|
);
|
|
}
|
|
|
|
$result = $this->app->make(BillSyncWindowAuditor::class)->audit('2026-11-17', '2026-11-19');
|
|
|
|
$this->assertTrue($result['has_gaps']);
|
|
$this->assertTrue($result['has_anomalies']);
|
|
$this->assertSame(['2026-11-19'], $result['platforms']['taobao_alipay']['missing_dates']);
|
|
$this->assertSame(['2026-11-18'], $result['platforms']['taobao_wechat']['suspicious_dates']);
|
|
}
|
|
|
|
private function createTables(): void
|
|
{
|
|
$schema = Schema::connection('business_mirror');
|
|
foreach ([
|
|
'taobao_alipay_bill_rows' => 'occurred_at',
|
|
'pdd_settlement_bill_rows' => 'occurred_at',
|
|
'taobao_wechat_bill_rows' => 'occurred_at',
|
|
'dewu_settlement_bill_rows' => 'bill_date',
|
|
] as $tableName => $dateColumn) {
|
|
$schema->create($tableName, function (Blueprint $table) use ($dateColumn): void {
|
|
$table->increments('id');
|
|
$table->string('record_code');
|
|
$table->string($dateColumn);
|
|
$table->string('store_name')->nullable();
|
|
$table->decimal('income_amount', 18, 2)->default(0);
|
|
$table->decimal('expense_amount', 18, 2)->default(0);
|
|
});
|
|
}
|
|
}
|
|
|
|
private function insertDay(
|
|
$connection,
|
|
string $table,
|
|
string $dateColumn,
|
|
string $date,
|
|
int $stores,
|
|
int $rowCount,
|
|
?int $actualRowCount = null,
|
|
): void {
|
|
$actualRowCount ??= $rowCount;
|
|
$rows = [];
|
|
for ($index = 0; $index < $actualRowCount; $index++) {
|
|
$rows[] = [
|
|
'record_code' => $table . '-' . $date . '-' . $index,
|
|
$dateColumn => $date . ' 12:00:00',
|
|
'store_name' => 'store-' . ($index % $stores),
|
|
'income_amount' => 10,
|
|
'expense_amount' => 1,
|
|
];
|
|
}
|
|
if ($rows !== []) {
|
|
$connection->table($table)->insert($rows);
|
|
}
|
|
}
|
|
}
|