CaiWuXiTong/tests/Unit/EcommerceFinance/PromotionExpenseMirrorSynchronizerTest.php

142 lines
6.0 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Mockery;
use Modules\EcommerceFinance\Repositories\PromotionExpenseMirrorRepository;
use Modules\EcommerceFinance\Services\PromotionExpenseMirrorSynchronizer;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
use Tests\TestCase;
class PromotionExpenseMirrorSynchronizerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$schema = Schema::connection('business_mirror');
if (! $schema->hasTable(PromotionExpenseMirrorRepository::TABLE)) {
$schema->create(PromotionExpenseMirrorRepository::TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->date('report_date');
$table->string('platform', 64);
$table->string('store_name', 255);
$table->unsignedBigInteger('source_rows')->default(0);
$table->decimal('push_fee', 20, 2)->default(0);
$table->decimal('shared_push_fee', 20, 2)->default(0);
$table->decimal('push_fee_total', 20, 2)->default(0);
$table->decimal('marketing_fee', 20, 2)->default(0);
$table->decimal('shared_marketing_fee', 20, 2)->default(0);
$table->decimal('marketing_fee_total', 20, 2)->default(0);
$table->decimal('total_fee', 20, 2)->default(0);
$table->timestamp('synced_at')->nullable();
});
}
if (! $schema->hasTable(PromotionExpenseMirrorRepository::STATE_TABLE)) {
$schema->create(PromotionExpenseMirrorRepository::STATE_TABLE, function (Blueprint $table): void {
$table->string('dataset_key', 191)->primary();
$table->string('source_key', 64);
$table->string('source_object', 191);
$table->string('target_table', 64);
$table->string('status', 24);
$table->dateTime('initial_completed_at')->nullable();
$table->dateTime('last_attempted_at')->nullable();
$table->dateTime('last_succeeded_at')->nullable();
$table->unsignedBigInteger('mirror_row_count')->default(0);
$table->string('last_error_code', 120)->nullable();
$table->timestamps();
});
}
}
public function test_it_replaces_each_day_with_the_latest_upstream_aggregate(): void
{
DB::connection('business_mirror')->table(PromotionExpenseMirrorRepository::TABLE)->insert([
'report_date' => '2026-10-01',
'platform' => '淘系',
'store_name' => '旧店',
'source_rows' => 1,
'push_fee_total' => 1,
'marketing_fee_total' => 0,
'total_fee' => 1,
]);
$factory = Mockery::mock(UpstreamReadOnlyQueryFactory::class);
$factory->shouldReceive('forSourceWithTimeout')
->once()
->andReturn(Mockery::mock(UpstreamReadOnlyQuery::class));
$synchronizer = new class($factory, new PromotionExpenseMirrorRepository()) extends PromotionExpenseMirrorSynchronizer {
protected function latestSourceDate(): ?string
{
return '2026-10-02';
}
protected function fetchAggregates(string $from, string $to): array
{
return [
[
'report_date' => '2026-10-01',
'platform' => '淘系',
'store_name' => '新店',
'source_rows' => 5,
'push_fee' => 10,
'shared_push_fee' => 20,
'push_fee_total' => 30,
'marketing_fee' => 4,
'shared_marketing_fee' => 6,
'marketing_fee_total' => 10,
],
[
'report_date' => '2026-10-02',
'platform' => '拼多多',
'store_name' => '乙店',
'source_rows' => 7,
'push_fee' => 40,
'shared_push_fee' => 0,
'push_fee_total' => 40,
'marketing_fee' => 5,
'shared_marketing_fee' => 0,
'marketing_fee_total' => 5,
],
];
}
};
$result = $synchronizer->sync(
Carbon::parse('2026-10-01', 'Asia/Shanghai'),
Carbon::parse('2026-10-03', 'Asia/Shanghai'),
);
$this->assertSame(2, $result['days']);
$this->assertSame(12, $result['source_rows']);
$this->assertSame(2, $result['stored_rows']);
$this->assertDatabaseMissing(PromotionExpenseMirrorRepository::TABLE, [
'report_date' => '2026-10-01',
'store_name' => '旧店',
], 'business_mirror');
$this->assertDatabaseHas(PromotionExpenseMirrorRepository::TABLE, [
'report_date' => '2026-10-01',
'store_name' => '新店',
'total_fee' => 40,
], 'business_mirror');
$this->assertDatabaseHas(PromotionExpenseMirrorRepository::TABLE, [
'report_date' => '2026-10-02',
'store_name' => '乙店',
'total_fee' => 45,
], 'business_mirror');
$this->assertDatabaseMissing(PromotionExpenseMirrorRepository::TABLE, [
'report_date' => '2026-10-03',
], 'business_mirror');
$this->assertDatabaseHas(PromotionExpenseMirrorRepository::STATE_TABLE, [
'dataset_key' => PromotionExpenseMirrorRepository::DATASET_KEY,
'status' => 'succeeded',
'mirror_row_count' => 2,
], 'business_mirror');
}
}