CaiWuXiTong/tests/Unit/EcommerceFinance/JushuitanShopPromotionFeeSynchronizerTest.php

133 lines
5.2 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Support\Carbon;
use Mockery;
use Modules\EcommerceFinance\Repositories\JushuitanShopPromotionFeeMirrorRepository;
use Modules\EcommerceFinance\Services\JushuitanShopPromotionFeeClient;
use Modules\EcommerceFinance\Services\JushuitanShopPromotionFeeSynchronizer;
use RuntimeException;
use Tests\TestCase;
class JushuitanShopPromotionFeeSynchronizerTest extends TestCase
{
protected function tearDown(): void
{
Mockery::close();
parent::tearDown();
}
public function test_it_follows_the_api_cursor_and_upserts_normalized_store_fee_rows(): void
{
config()->set('services.jushuitan.shop_promotion_fee_page_size', 100);
$client = Mockery::mock(JushuitanShopPromotionFeeClient::class);
$repository = Mockery::mock(JushuitanShopPromotionFeeMirrorRepository::class);
$from = Carbon::parse('2026-11-01', 'Asia/Shanghai');
$to = Carbon::parse('2026-11-30', 'Asia/Shanghai');
$repository->shouldReceive('startRun')
->once()
->with('full', 'fee_time', Mockery::on(
static fn (Carbon $value): bool => $value->toDateString() === '2026-11-01',
), Mockery::on(
static fn (Carbon $value): bool => $value->toDateString() === '2026-11-30',
));
$repository->shouldReceive('shopNameMap')->once()->andReturn(['101' => '天猫-甲店']);
$upserted = [];
$repository->shouldReceive('upsertRecords')
->twice()
->andReturnUsing(function (array $rows) use (&$upserted): int {
$upserted[] = $rows;
return count($rows);
});
$repository->shouldReceive('complete')
->once()
->with('full', Mockery::type(Carbon::class), Mockery::type(Carbon::class), 2, 2);
$client->shouldReceive('getPage')->once()->with(
Mockery::on(static fn (Carbon $value): bool => $value->toDateString() === '2026-11-01'),
Mockery::on(static fn (Carbon $value): bool => $value->toDateString() === '2026-12-01'),
'fee_time',
0,
100,
)->andReturn([
'records' => [$this->sourceRow('PK-1', 101, '2026-11-01', 12.30, 0)],
'source_rows' => 1,
'cursor' => 42,
'has_next' => true,
]);
$client->shouldReceive('getPage')->once()->with(
Mockery::on(static fn (Carbon $value): bool => $value->toDateString() === '2026-11-01'),
Mockery::on(static fn (Carbon $value): bool => $value->toDateString() === '2026-12-01'),
'fee_time',
42,
100,
)->andReturn([
'records' => [$this->sourceRow('PK-2', 101, '2026-11-02', -2.30, 1)],
'source_rows' => 1,
'cursor' => 43,
'has_next' => false,
]);
$result = (new JushuitanShopPromotionFeeSynchronizer($client, $repository))
->sync($from, $to, 'full', 'fee_time');
$this->assertSame(2, $result['pages']);
$this->assertSame(2, $result['source_rows']);
$this->assertSame(2, $result['stored_rows']);
$this->assertSame('PK-1', $upserted[0][0]['source_record_key']);
$this->assertSame('jushuitan_open_api', $upserted[0][0]['source_key']);
$this->assertSame('天猫-甲店', $upserted[0][0]['shop_name']);
$this->assertSame(64, strlen((string) $upserted[0][0]['row_hash']));
$this->assertSame(1, $upserted[1][0]['del_flag']);
}
public function test_it_fails_when_the_api_returns_has_next_without_advancing_the_cursor(): void
{
$client = Mockery::mock(JushuitanShopPromotionFeeClient::class);
$repository = Mockery::mock(JushuitanShopPromotionFeeMirrorRepository::class);
$repository->shouldReceive('startRun')->once();
$repository->shouldReceive('shopNameMap')->once()->andReturn([]);
$repository->shouldReceive('upsertRecords')->once()->with([])->andReturn(0);
$repository->shouldReceive('fail')->once()->with('jushuitan_shop_promotion_fee_cursor_not_advanced');
$client->shouldReceive('getPage')->once()->andReturn([
'records' => [],
'source_rows' => 0,
'cursor' => 0,
'has_next' => true,
]);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('jushuitan_shop_promotion_fee_cursor_not_advanced');
(new JushuitanShopPromotionFeeSynchronizer($client, $repository))->sync(
Carbon::parse('2026-11-01'),
Carbon::parse('2026-11-30'),
'incremental',
'modified',
);
}
/** @return array<string,mixed> */
private function sourceRow(
string $key,
int $shopId,
string $feeTime,
float $amount,
int $deleted,
): array {
return [
'auto_id' => 1,
'shop_id' => $shopId,
'fee_time' => $feeTime,
'amount' => $amount,
'fee_name' => '万相台无界-现金收支',
'channel' => 'TAOBAO',
'primary_key' => $key,
'del_flag' => $deleted,
];
}
}