77 lines
2.8 KiB
PHP
77 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Modules\EcommerceFinance\Repositories\AlibabaBillRepository;
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQuery;
|
|
use Modules\EcommerceFinance\Services\UpstreamReadOnlyQueryFactory;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class AlibabaBillRepositoryTest extends TestCase
|
|
{
|
|
public function test_it_maps_template_columns_and_keeps_yuan_amounts(): void
|
|
{
|
|
$query = new class('jiansun') extends UpstreamReadOnlyQuery {
|
|
public array $calls = [];
|
|
|
|
public function __construct()
|
|
{
|
|
}
|
|
|
|
public function scalar(string $sql, array $bindings = []): mixed
|
|
{
|
|
$this->calls[] = ['sql' => $sql, 'bindings' => $bindings];
|
|
|
|
return 1;
|
|
}
|
|
|
|
public function select(string $sql, array $bindings = []): array
|
|
{
|
|
$this->calls[] = ['sql' => $sql, 'bindings' => $bindings];
|
|
|
|
return [[
|
|
'id' => 9,
|
|
'shopName' => '测试店铺',
|
|
'tradeDay' => '2026-06-30 12:00:00',
|
|
'tradeTypeName' => '全站投放',
|
|
'tradeAMTIn' => 12.345,
|
|
'tradeAMTOut' => 67.891,
|
|
]];
|
|
}
|
|
|
|
public function first(string $sql, array $bindings = []): ?array
|
|
{
|
|
$this->calls[] = ['sql' => $sql, 'bindings' => $bindings];
|
|
|
|
return ['income_total' => 12.345, 'expense_total' => 67.891, 'cnt' => 1];
|
|
}
|
|
};
|
|
$factory = new class($query) extends UpstreamReadOnlyQueryFactory {
|
|
public function __construct(private readonly UpstreamReadOnlyQuery $query)
|
|
{
|
|
}
|
|
|
|
public function forSource(string $source): UpstreamReadOnlyQuery
|
|
{
|
|
TestCase::assertSame('jiansun', $source);
|
|
|
|
return $this->query;
|
|
}
|
|
};
|
|
|
|
$repository = new AlibabaBillRepository($factory);
|
|
$result = $repository->paginate('测试店铺', '全站投放', '2026-06-30', '2026-06-30', 2, 250);
|
|
$summary = $repository->summary('测试店铺', '全站投放', '2026-06-30', '2026-06-30');
|
|
|
|
$this->assertSame(1, $result['total']);
|
|
$this->assertSame('测试店铺', $result['rows'][0]['shop_name']);
|
|
$this->assertSame(12.35, $result['rows'][0]['income_amount']);
|
|
$this->assertSame(67.89, $result['rows'][0]['expense_amount']);
|
|
$this->assertSame(-55.54, $summary['net']);
|
|
$this->assertStringContainsString('tradeTypeName = ?', $query->calls[0]['sql']);
|
|
$this->assertStringContainsString('CAST(src.tradeDay AS date)', $query->calls[0]['sql']);
|
|
$this->assertSame(101, $query->calls[1]['bindings'][4]);
|
|
$this->assertSame(200, $query->calls[1]['bindings'][5]);
|
|
}
|
|
}
|