CaiWuXiTong/tests/Unit/EcommerceFinance/JianSunSnapshotRefresherTest.php

208 lines
6.9 KiB
PHP

<?php
namespace Tests\Unit\EcommerceFinance;
use Closure;
use Modules\EcommerceFinance\Repositories\EdgeBandingRepository;
use Modules\EcommerceFinance\Repositories\GoodsReconciliationRepository;
use Modules\EcommerceFinance\Repositories\InventoryRepository;
use Modules\EcommerceFinance\Repositories\PinduoduoSpreadBillRepository;
use Modules\EcommerceFinance\Repositories\RawBoardPayablesRepository;
use Modules\EcommerceFinance\Repositories\SupplierPayableRepository;
use Modules\EcommerceFinance\Repositories\TmallTaobaoSpreadBillRepository;
use Modules\EcommerceFinance\Services\JianSunQueryCache;
use Modules\EcommerceFinance\Services\JianSunSnapshotRefresher;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
class JianSunSnapshotRefresherTest extends TestCase
{
public function test_pinduoduo_flow_filter_is_preserved_during_background_refresh(): void
{
$pinduoduo = new class extends PinduoduoSpreadBillRepository {
public array $arguments = [];
public function __construct()
{
}
public function paginate(
?string $storeName = null,
?string $fromDate = null,
?string $toDate = null,
?string $flowType = null,
int $page = 1,
int $pageSize = 25
): array {
$this->arguments = func_get_args();
return ['rows' => [], 'total' => 0];
}
};
$refresher = $this->refresher(pinduoduo: $pinduoduo);
$status = $refresher->refresh([
'scope' => 'pinduoduo-bills.list',
'parameters' => [
'filters' => [
'store_name' => '测试店铺',
'from' => '2026-07-01',
'to' => '2026-07-23',
'flow_type' => 'expense',
],
'page' => 3,
'limit' => 50,
],
]);
$this->assertSame('fresh', $status);
$this->assertSame(
['测试店铺', '2026-07-01', '2026-07-23', 'expense', 3, 50],
$pinduoduo->arguments
);
}
public function test_taobao_alipay_filters_are_preserved_during_background_refresh(): void
{
$taobaoAlipay = new class extends TmallTaobaoSpreadBillRepository {
public array $arguments = [];
public function __construct()
{
}
public function paginate(
?string $storeName = null,
?string $fromDate = null,
?string $toDate = null,
?string $flowType = null,
int $page = 1,
int $pageSize = 25
): array {
$this->arguments = func_get_args();
return ['rows' => [], 'total' => 0];
}
};
$refresher = $this->refresher(taobaoAlipay: $taobaoAlipay);
$status = $refresher->refresh([
'scope' => 'taobao-alipay-bills.list',
'parameters' => [
'filters' => [
'store_name' => '测试店铺',
'from' => '2026-07-01',
'to' => '2026-07-23',
'flow_type' => 'income',
],
'page' => 2,
'limit' => 25,
],
]);
$this->assertSame('fresh', $status);
$this->assertSame(
['测试店铺', '2026-07-01', '2026-07-23', 'income', 2, 25],
$taobaoAlipay->arguments
);
}
public function test_inventory_scope_is_available_to_background_refresh(): void
{
$inventory = new class extends InventoryRepository {
public array $arguments = [];
public function __construct()
{
}
public function boardInventoryDetails(array $filters, int $page, int $limit): array
{
$this->arguments = func_get_args();
return ['data' => [], 'total' => 0, 'totals' => []];
}
};
$refresher = $this->refresher(inventory: $inventory);
$status = $refresher->refresh([
'scope' => 'inventory.board-details',
'parameters' => [
'filters' => ['manufacturer' => '测试厂'],
'page' => 2,
'limit' => 50,
],
]);
$this->assertSame('fresh', $status);
$this->assertSame([['manufacturer' => '测试厂'], 2, 50], $inventory->arguments);
}
public function test_raw_board_reconciliation_scope_is_available_to_background_refresh(): void
{
$rawBoardPayables = new class extends RawBoardPayablesRepository {
public array $arguments = [];
public function __construct()
{
}
public function reconciliations(array $filters, int $page, int $limit): array
{
$this->arguments = func_get_args();
return ['data' => [], 'total' => 0, 'totals' => []];
}
};
$refresher = $this->refresher(rawBoardPayables: $rawBoardPayables);
$status = $refresher->refresh([
'scope' => 'raw-board.reconciliations',
'parameters' => [
'filters' => ['supplier' => '测试厂'],
'page' => 2,
'limit' => 50,
],
]);
$this->assertSame('fresh', $status);
$this->assertSame([['supplier' => '测试厂'], 2, 50], $rawBoardPayables->arguments);
}
private function refresher(
?InventoryRepository $inventory = null,
?PinduoduoSpreadBillRepository $pinduoduo = null,
?RawBoardPayablesRepository $rawBoardPayables = null,
?TmallTaobaoSpreadBillRepository $taobaoAlipay = null,
): JianSunSnapshotRefresher {
return new JianSunSnapshotRefresher(
new class extends JianSunQueryCache {
public mixed $value = null;
public function __construct()
{
}
public function refresh(string $scope, array $parameters, Closure $resolver): string
{
$this->value = $resolver();
return 'fresh';
}
},
$this->placeholder(EdgeBandingRepository::class),
$this->placeholder(GoodsReconciliationRepository::class),
$inventory ?? $this->placeholder(InventoryRepository::class),
$pinduoduo ?? $this->placeholder(PinduoduoSpreadBillRepository::class),
$rawBoardPayables ?? $this->placeholder(RawBoardPayablesRepository::class),
$taobaoAlipay ?? $this->placeholder(TmallTaobaoSpreadBillRepository::class),
$this->placeholder(SupplierPayableRepository::class),
);
}
private function placeholder(string $class): object
{
return (new ReflectionClass($class))->newInstanceWithoutConstructor();
}
}