commit compras5 2711
tests / ci (push) Waiting to run

This commit is contained in:
2026-11-27 20:19:21 +02:00
parent 7486007fcf
commit d58b75c87a
57 changed files with 2803 additions and 116 deletions
@@ -2,6 +2,7 @@
use App\Models\Requisition;
use App\Models\User;
use App\Services\ExpenseAllocations;
use App\Services\RequisitionWorkflow;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Database\Connection;
@@ -21,6 +22,7 @@ if (! $app->environment('testing') || $db->getDriverName() !== 'pgsql' || $db->g
throw new LogicException('Cycle recovery requires disposable local PostgreSQL QA.');
}
config(['requisition-expiry.qa_enabled' => true]);
$expenses = ($argv[2] ?? '') === 'recover-expenses';
require_once __DIR__.'/../Support/ReceiptFixture.php';
receiptFixture();
$fingerprint = static function (Connection $connection): array {
@@ -66,6 +68,16 @@ $request = $workflow->save($owner, ['operation_key' => (string) Str::uuid(), 'pr
$submission = (string) Str::uuid();
$version = $request->process_version;
$workflow->submit($owner, $request, $submission, $version);
$expenseFixture = $expenseInput = null;
if ($expenses) {
require_once __DIR__.'/../Support/ExpenseAllocationQaFixture.php';
$expenseFixture = expenseAllocationQaFixture();
$expenseService = app(ExpenseAllocations::class);
$firstExpense = $expenseService->save($expenseFixture['f']['validator'], $expenseFixture['payment']->id, $expenseFixture['input']);
$expenseInput = [...$expenseFixture['input'], 'operation_key' => (string) Str::uuid(), 'version' => $firstExpense->version,
'allocations' => [['category' => 'Material', 'amount' => '10.00'], ['category' => 'Serviços', 'amount' => '30.00']]];
$expenseService->save($expenseFixture['f']['validator'], $expenseFixture['payment']->id, $expenseInput);
}
$after = $fingerprint($db);
$pg('pg_dump', ['--format=custom', '--file='.$postDump, '--dbname='.$database]);
$restore = 'compras_receipts_test_'.bin2hex(random_bytes(6));
@@ -93,6 +105,21 @@ try {
|| $restored->table('approval_steps')->where('requisition_id', $request->id)->where('decision', 'expired')->count() !== 1) {
throw new RuntimeException('Restored native expiry/submission replay changed history or reservation.');
}
if ($expenses) {
$expenseActor = User::query()->whereKey($expenseFixture['f']['validator']->id)->firstOrFail();
auth()->guard()->setUser($expenseActor);
$expenseService = app(ExpenseAllocations::class);
$paymentId = $expenseFixture['payment']->id;
$projection = $expenseService->projection($expenseActor, $paymentId);
$replay = $expenseService->save($expenseActor, $paymentId, $expenseInput);
if ($replay->version !== 2 || $projection['state'] !== 'allocated'
|| $projection['allocations'] !== [['category' => 'Material', 'amount_minor' => '1000'], ['category' => 'Serviços', 'amount_minor' => '3000']]
|| $fingerprint($restored) !== $after) {
throw new RuntimeException('Explicit expense versions, audited sources or replay changed after restore.');
}
echo json_encode(['expense_versions_restored' => 2, 'expense_native_sources_verified' => true,
'expense_explicit_amounts_restored' => ['Material' => '1000', 'Serviços' => '3000'], 'expense_replay_without_effects' => true], JSON_THROW_ON_ERROR).PHP_EOL;
}
DB::setDefaultConnection($originalDefault);
DB::purge('cycle_recovery_restore');
$admin->statement('DROP DATABASE "'.$restore.'" WITH (FORCE)');