CaiWuXiTong/tests/Unit/EcommerceFinance/JushuitanShopPromotionFeeClientTest.php

128 lines
4.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Tests\Unit\EcommerceFinance;
use Illuminate\Http\Client\Request;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Http;
use Modules\EcommerceFinance\Services\JushuitanShopPromotionFeeClient;
use RuntimeException;
use Tests\TestCase;
class JushuitanShopPromotionFeeClientTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Carbon::setTestNow('2026-12-01 10:00:00');
config()->set('services.jushuitan', [
'open_api_base_url' => 'https://jushuitan.example.test',
'app_key' => 'test-app-key',
'app_secret' => 'test-app-secret',
'access_token' => 'test-access-token',
'timeout' => 20,
'shop_promotion_fee_path' => '/open/webapi/pfopen/pfopenalifee/getopenalipaypromotionfee',
'shop_promotion_fee_page_size' => 100,
]);
Http::preventStrayRequests();
}
protected function tearDown(): void
{
Carbon::setTestNow();
parent::tearDown();
}
public function test_it_posts_the_new_open_platform_signature_and_reads_cursor_page(): void
{
Http::fake([
'https://jushuitan.example.test/*' => Http::response([
'code' => 0,
'msg' => '执行成功',
'data' => [
'cursor' => 314,
'has_next' => true,
'data' => [[
'auto_id' => 8101,
'shop_id' => 18526685,
'fee_time' => '2026-11-30 09:15:00',
'amount' => 123.45,
'fee_name' => '万相台无界-现金收支',
'channel' => 'TAOBAO',
'csg_name' => '推广费',
'primary_key' => 'PK-1',
]],
],
]),
]);
$result = (new JushuitanShopPromotionFeeClient())->getPage(
Carbon::parse('2026-11-01'),
Carbon::parse('2026-12-01'),
'fee_time',
0,
200,
);
$this->assertSame(1, $result['source_rows']);
$this->assertSame(314, $result['cursor']);
$this->assertTrue($result['has_next']);
$this->assertSame('PK-1', $result['records'][0]['primary_key']);
Http::assertSent(function (Request $request): bool {
$data = $request->data();
$biz = '{"cursor":0,"start_time":"2026-11-01","time_type":"fee_time",'
. '"end_time":"2026-12-01","page_size":100}';
$expected = [
'app_key' => 'test-app-key',
'access_token' => 'test-access-token',
'timestamp' => (string) Carbon::now()->timestamp,
'charset' => 'utf-8',
'version' => '2',
'biz' => $biz,
];
ksort($expected);
$signingValue = 'test-app-secret';
foreach ($expected as $key => $value) {
$signingValue .= $key . $value;
}
$expected['sign'] = md5($signingValue);
return $request->method() === 'POST'
&& $request->url() === 'https://jushuitan.example.test/open/webapi/pfopen/pfopenalifee/getopenalipaypromotionfee'
&& str_starts_with((string) $request->header('Content-Type')[0], 'application/x-www-form-urlencoded')
&& $data === $expected;
});
}
public function test_it_rejects_missing_credentials_and_invalid_app_key_without_exposing_upstream_text(): void
{
config()->set('services.jushuitan.app_secret', null);
try {
(new JushuitanShopPromotionFeeClient())->getPage(
Carbon::parse('2026-11-01'),
Carbon::parse('2026-12-01'),
'fee_time',
);
$this->fail('Expected missing credential exception.');
} catch (RuntimeException $exception) {
$this->assertSame('jushuitan_open_api_credentials_missing', $exception->getMessage());
}
config()->set('services.jushuitan.app_secret', 'test-app-secret');
Http::fake([
'*' => Http::response([
'code' => 140,
'msg' => '验证失败!app_key无效',
]),
]);
$this->expectExceptionMessage('jushuitan_open_api_app_key_invalid');
(new JushuitanShopPromotionFeeClient())->getPage(
Carbon::parse('2026-11-01'),
Carbon::parse('2026-12-01'),
'fee_time',
);
}
}