180 lines
6.6 KiB
PHP
180 lines
6.6 KiB
PHP
<?php
|
|
|
|
namespace Tests\Unit\EcommerceFinance;
|
|
|
|
use Carbon\CarbonImmutable;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Http\Client\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Modules\EcommerceFinance\Exceptions\DingTalkProcessInstanceQuotaException;
|
|
use Modules\EcommerceFinance\Services\DingTalkProcessInstanceDailyBudget;
|
|
use Modules\EcommerceFinance\Services\DingTalkProcessInstanceClient;
|
|
use Modules\EcommerceFinance\Support\OaProcessTime;
|
|
use Tests\TestCase;
|
|
|
|
class DingTalkProcessInstanceClientTest extends TestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
Schema::connection('business_mirror')->create(
|
|
DingTalkProcessInstanceDailyBudget::TABLE,
|
|
function (Blueprint $table): void {
|
|
$table->date('usage_date')->primary();
|
|
$table->unsignedBigInteger('reserved_calls')->default(0);
|
|
$table->timestamps();
|
|
},
|
|
);
|
|
}
|
|
|
|
public function test_process_snapshots_fetches_and_parses_a_batch(): void
|
|
{
|
|
config([
|
|
'dingtalk.client_id' => 'test-client',
|
|
'dingtalk.client_secret' => 'test-secret',
|
|
'dingtalk.app_token_url' => 'https://example.test/token',
|
|
'dingtalk.timeout' => 3,
|
|
'dingtalk.process_concurrency' => 2,
|
|
]);
|
|
|
|
Http::fake([
|
|
'https://example.test/token*' => Http::response([
|
|
'errcode' => 0,
|
|
'access_token' => 'test-token',
|
|
'expires_in' => 7200,
|
|
]),
|
|
'https://api.dingtalk.com/v1.0/workflow/processInstances*' => function (Request $request) {
|
|
$processInstanceId = (string) ($request->data()['processInstanceId'] ?? '');
|
|
|
|
return Http::response([
|
|
'result' => [
|
|
'status' => 'RUNNING',
|
|
'result' => 'NONE',
|
|
'formComponentValues' => [[
|
|
'name' => '用途',
|
|
'value' => '批量读取-' . $processInstanceId,
|
|
]],
|
|
'tasks' => [[
|
|
'taskGroupName' => '出纳打款',
|
|
'status' => 'RUNNING',
|
|
'result' => 'NONE',
|
|
]],
|
|
'operationRecords' => [],
|
|
],
|
|
]);
|
|
},
|
|
]);
|
|
|
|
$result = app(DingTalkProcessInstanceClient::class)
|
|
->processSnapshots(['process-a', 'process-b']);
|
|
|
|
$this->assertFalse($result['fatal']);
|
|
$this->assertSame(2, $result['attempted']);
|
|
$this->assertSame([], $result['errors']);
|
|
$this->assertSame(
|
|
'批量读取-process-a',
|
|
$result['snapshots']['process-a']['form_values']['用途'],
|
|
);
|
|
$this->assertSame(
|
|
'批量读取-process-b',
|
|
$result['snapshots']['process-b']['form_values']['用途'],
|
|
);
|
|
Http::assertSentCount(3);
|
|
}
|
|
|
|
public function test_daily_budget_blocks_requests_after_configured_limit(): void
|
|
{
|
|
config([
|
|
'dingtalk.client_id' => 'test-client',
|
|
'dingtalk.client_secret' => 'test-secret',
|
|
'dingtalk.app_token_url' => 'https://example.test/token',
|
|
'dingtalk.timeout' => 3,
|
|
'dingtalk.process_daily_call_limit' => 2,
|
|
]);
|
|
|
|
Http::fake([
|
|
'https://example.test/token*' => Http::response([
|
|
'errcode' => 0,
|
|
'access_token' => 'test-token',
|
|
'expires_in' => 7200,
|
|
]),
|
|
'https://api.dingtalk.com/v1.0/workflow/processInstances*' => function (Request $request) {
|
|
$processInstanceId = (string) ($request->data()['processInstanceId'] ?? '');
|
|
|
|
return Http::response([
|
|
'result' => [
|
|
'status' => 'RUNNING',
|
|
'result' => 'NONE',
|
|
'formComponentValues' => [],
|
|
'tasks' => [],
|
|
'operationRecords' => [],
|
|
'processInstanceId' => $processInstanceId,
|
|
],
|
|
]);
|
|
},
|
|
]);
|
|
|
|
$client = app(DingTalkProcessInstanceClient::class);
|
|
$first = $client->processSnapshots(['process-a', 'process-b']);
|
|
$this->assertSame(2, $first['attempted']);
|
|
$this->assertFalse($first['budget_exhausted']);
|
|
$this->assertSame(2, (int) DB::connection('business_mirror')
|
|
->table(DingTalkProcessInstanceDailyBudget::TABLE)
|
|
->value('reserved_calls'));
|
|
|
|
try {
|
|
$client->processSnapshot('process-c');
|
|
$this->fail('超过 OA 流程日限额后不应继续发起接口请求。');
|
|
} catch (DingTalkProcessInstanceQuotaException $exception) {
|
|
$this->assertTrue($exception->stopsRun());
|
|
}
|
|
|
|
Http::assertSentCount(3);
|
|
}
|
|
|
|
public function test_daily_budget_cannot_exceed_twenty_thousand_and_isolates_each_date(): void
|
|
{
|
|
config(['dingtalk.process_daily_call_limit' => 30000]);
|
|
$budget = app(DingTalkProcessInstanceDailyBudget::class);
|
|
$this->assertSame(20000, $budget->limit());
|
|
|
|
config(['dingtalk.process_daily_call_limit' => 2]);
|
|
CarbonImmutable::setTestNow(
|
|
CarbonImmutable::parse('2026-11-23 10:00:00', 'Asia/Shanghai'),
|
|
);
|
|
try {
|
|
$this->assertSame('2026-11-23', OaProcessTime::now()->toDateString());
|
|
DB::connection('business_mirror')
|
|
->table(DingTalkProcessInstanceDailyBudget::TABLE)
|
|
->insert([
|
|
'usage_date' => '2026-11-22',
|
|
'reserved_calls' => 999,
|
|
'created_at' => '2026-11-22 00:00:00',
|
|
'updated_at' => '2026-11-22 23:59:59',
|
|
]);
|
|
$this->assertSame(2, $budget->reserve(3));
|
|
$this->assertSame(0, $budget->reserve(1));
|
|
$this->assertSame([
|
|
'date' => '2026-11-23',
|
|
'used' => 2,
|
|
'limit' => 2,
|
|
'remaining' => 0,
|
|
], $budget->usage());
|
|
} finally {
|
|
CarbonImmutable::setTestNow();
|
|
}
|
|
}
|
|
}
|