CaiWuXiTong/tests/Feature/Console/ArchiveStoreInformationDailyTest.php

90 lines
4.0 KiB
PHP

<?php
namespace Tests\Feature\Console;
use App\Business\StoreInformationDailyArchive;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Modules\EcommerceFinance\Repositories\BusinessInformationMirrorRepository;
use Modules\EcommerceFinance\Repositories\StoreAccountMirrorRepository;
use Mockery;
use Tests\TestCase;
class ArchiveStoreInformationDailyTest 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');
if (! Schema::connection('business_mirror')->hasTable(StoreInformationDailyArchive::TABLE)) {
Schema::connection('business_mirror')->create(StoreInformationDailyArchive::TABLE, function (Blueprint $table): void {
$table->bigIncrements('id');
$table->date('snapshot_date');
$table->string('store_record_id', 64);
$table->string('erp_store_name', 191);
$table->string('store_name', 191)->nullable();
$table->string('company_name', 255)->nullable();
$table->string('store_manager', 191)->nullable();
$table->string('department_manager', 191)->nullable();
$table->string('profit_center', 191)->nullable();
$table->string('store_status', 64)->nullable();
$table->json('payload');
$table->dateTime('captured_at', 6);
$table->timestamps(6);
$table->unique(['snapshot_date', 'store_record_id']);
});
}
}
public function test_it_archives_daily_store_information_with_account_ownership_idempotently(): void
{
$mirror = Mockery::mock(BusinessInformationMirrorRepository::class);
$mirror->shouldReceive('ready')->twice()->with('store-information.read')->andReturnTrue();
$mirror->shouldReceive('allStores')->twice()->andReturn([[
'id' => 8,
'erp_store_name' => '京东_测试店',
'store_name' => '测试店',
'company_name' => '测试主体',
'store_manager' => '旧负责人',
'department_manager' => '旧部门负责人',
'profit_center' => '运营中心A',
'store_status' => '正常',
'platform' => '京东',
]]);
$accounts = Mockery::mock(StoreAccountMirrorRepository::class);
$accounts->shouldReceive('ownershipByStoreName')->twice()->andReturn([
StoreAccountMirrorRepository::storeNameKey('京东-测试店') => [
'operation_owner' => '账号店铺负责人',
'operation_department_head' => '账号部门负责人',
'operation_director' => '运营总监',
'profit_center' => '运营中心A',
],
]);
$this->app->instance(BusinessInformationMirrorRepository::class, $mirror);
$this->app->instance(StoreAccountMirrorRepository::class, $accounts);
$this->artisan('business-mirror:archive-store-information', ['--date' => '2026-10-26'])
->expectsOutput('已保存 2026-10-26 店铺基础信息快照,共 1 条。')
->assertSuccessful();
$this->artisan('business-mirror:archive-store-information', ['--date' => '2026-10-26'])
->assertSuccessful();
$rows = DB::connection('business_mirror')->table(StoreInformationDailyArchive::TABLE)->get();
$this->assertCount(1, $rows);
$this->assertSame('账号店铺负责人', $rows[0]->store_manager);
$this->assertSame('账号部门负责人', $rows[0]->department_manager);
$payload = json_decode($rows[0]->payload, true, flags: JSON_THROW_ON_ERROR);
$this->assertSame('京东', $payload['platform']);
}
}