209 lines
8.0 KiB
PHP
209 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use Mockery;
|
|
use Modules\EcommerceFinance\Repositories\StoreReceivingAlipayBillMirrorRepository;
|
|
use Modules\EcommerceFinance\Repositories\StoreReceivingDewuAlipayBillMirrorRepository;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class StoreReceivingBillsApiTest extends FeatureTestCase
|
|
{
|
|
public function test_account_directory_lists_all_receiving_bill_tabs(): void
|
|
{
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.accounts'))
|
|
->assertOk()
|
|
->assertJsonCount(12, 'data')
|
|
->assertJsonFragment(['slug' => 'taobao-alipay', 'title' => '全部支付宝账户'])
|
|
->assertJsonFragment(['slug' => 'xiaohongshu', 'title' => '小红书账户']);
|
|
}
|
|
|
|
public function test_taobao_alipay_api_uses_existing_query_and_returns_account_metadata(): void
|
|
{
|
|
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
|
$repository->shouldReceive('paginate')
|
|
->once()
|
|
->with(
|
|
Mockery::on(fn (array $filters): bool => $filters['store_name'] === '天猫-亿家达旗舰店'
|
|
&& $filters['start'] === '2026-11-01'
|
|
&& $filters['end'] === '2026-11-30'),
|
|
'occurred_at',
|
|
'desc',
|
|
2,
|
|
20,
|
|
)
|
|
->andReturn($this->emptyAlipayResult(2, 20));
|
|
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([
|
|
'store_name' => '天猫-亿家达旗舰店',
|
|
'start' => '2026-11-01',
|
|
'end' => '2026-11-30',
|
|
'page' => 2,
|
|
'limit' => 20,
|
|
]))
|
|
->assertOk()
|
|
->assertJsonPath('account.slug', 'taobao-alipay')
|
|
->assertJsonPath('account.title', '全部支付宝账户')
|
|
->assertJsonPath('current_page', 2)
|
|
->assertJsonPath('per_page', 20);
|
|
}
|
|
|
|
public function test_api_defaults_to_current_month_when_dates_are_omitted(): void
|
|
{
|
|
$this->travelTo('2026-12-02 10:00:00');
|
|
|
|
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
|
$repository->shouldReceive('paginate')
|
|
->once()
|
|
->with(
|
|
Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-12-01'
|
|
&& $filters['end'] === '2026-12-31'),
|
|
'occurred_at',
|
|
'desc',
|
|
1,
|
|
10,
|
|
)
|
|
->andReturn($this->emptyAlipayResult());
|
|
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']))
|
|
->assertOk();
|
|
}
|
|
|
|
public function test_generic_account_tab_uses_the_shared_account_bills_query(): void
|
|
{
|
|
$repository = Mockery::mock(StoreReceivingDewuAlipayBillMirrorRepository::class);
|
|
$repository->shouldReceive('paginate')
|
|
->once()
|
|
->with(
|
|
Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-11-01'
|
|
&& $filters['end'] === '2026-11-30'),
|
|
'occurred_at',
|
|
'desc',
|
|
1,
|
|
5,
|
|
)
|
|
->andReturn($this->emptyAlipayResult(1, 5));
|
|
$this->app->instance(StoreReceivingDewuAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'dewu-alipay']) . '?' . http_build_query([
|
|
'start' => '2026-11-01',
|
|
'end' => '2026-11-30',
|
|
'limit' => 5,
|
|
]))
|
|
->assertOk()
|
|
->assertJsonPath('account.slug', 'dewu-alipay')
|
|
->assertJsonPath('account.title', '得物支付宝账户')
|
|
->assertJsonPath('columns.title', '得物支付宝账户');
|
|
}
|
|
|
|
public function test_api_rejects_an_incomplete_date_range(): void
|
|
{
|
|
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
|
$repository->shouldNotReceive('paginate');
|
|
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withExceptionHandling()
|
|
->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?start=2026-11-01')
|
|
->assertStatus(422)
|
|
->assertJsonValidationErrors(['start', 'end']);
|
|
}
|
|
|
|
public function test_filter_options_endpoint_uses_the_same_page_options(): void
|
|
{
|
|
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
|
$repository->shouldReceive('filterOptionsForChannel')
|
|
->once()
|
|
->with('alipayCSV', StoreReceivingAlipayBillMirrorRepository::DATASET, true)
|
|
->andReturn([
|
|
'shops' => ['天猫-亿家达旗舰店'],
|
|
'bill_items' => ['订单收入'],
|
|
]);
|
|
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.filter-options', ['account' => 'taobao-alipay']))
|
|
->assertOk()
|
|
->assertJsonPath('account.slug', 'taobao-alipay')
|
|
->assertJsonPath('shops.0', '天猫-亿家达旗舰店')
|
|
->assertJsonPath('bill_items.0', '订单收入');
|
|
}
|
|
|
|
public function test_export_parameter_still_returns_json_data(): void
|
|
{
|
|
$repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class);
|
|
$repository->shouldReceive('paginate')
|
|
->once()
|
|
->andReturn($this->emptyAlipayResult());
|
|
$this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository);
|
|
|
|
$this->loginAs();
|
|
|
|
$this->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([
|
|
'start' => '2026-11-01',
|
|
'end' => '2026-11-30',
|
|
'export' => 1,
|
|
]))
|
|
->assertOk()
|
|
->assertHeader('content-type', 'application/json')
|
|
->assertJsonPath('account.slug', 'taobao-alipay');
|
|
}
|
|
|
|
public function test_unknown_account_returns_not_found(): void
|
|
{
|
|
$this->loginAs();
|
|
|
|
$this->withExceptionHandling()
|
|
->withHeaders($this->apiHeaders())
|
|
->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'unknown-account']))
|
|
->assertNotFound();
|
|
}
|
|
|
|
/** @return array<string,mixed> */
|
|
private function emptyAlipayResult(int $page = 1, int $perPage = 10): array
|
|
{
|
|
return [
|
|
'data' => [],
|
|
'total' => 0,
|
|
'current_page' => $page,
|
|
'last_page' => 1,
|
|
'per_page' => $perPage,
|
|
'summary' => [
|
|
'income' => 0,
|
|
'expense' => 0,
|
|
'opening_balance' => 0,
|
|
'opening_balance_date' => null,
|
|
'opening_balance_source' => null,
|
|
'balance' => 0,
|
|
'count' => 0,
|
|
],
|
|
];
|
|
}
|
|
|
|
private function apiHeaders(): array
|
|
{
|
|
return [
|
|
'Authorization' => 'Basic ' . base64_encode('test@company.com:123456'),
|
|
];
|
|
}
|
|
}
|