44dc1025ec
BillingTopupService кредитует tenants.balance_rub (bcmath) и пишет append-only строку balance_transactions(type='topup'). BillingController + route POST /api/billing/topup под [auth:sanctum, tenant]. MVP-stub: без платёжного шлюза (ЮKassa — post-Б-1). Sprint 2 Plan C, audit E1 (backend). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
55 lines
1.9 KiB
PHP
55 lines
1.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Tenant;
|
|
use App\Models\User;
|
|
use App\Services\Billing\BillingTopupService;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
|
|
test('topup кредитует balance_rub и пишет append-only строку topup', function () {
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'balance_leads' => 7]);
|
|
|
|
$tx = app(BillingTopupService::class)->topup($tenant->id, '250.00', null);
|
|
|
|
expect($tx->type)->toBe('topup')
|
|
->and($tx->amount_rub)->toBe('250.00')
|
|
->and($tx->amount_leads)->toBe(0)
|
|
->and($tx->balance_rub_after)->toBe('750.00')
|
|
->and($tx->balance_leads_after)->toBe(7);
|
|
|
|
expect((string) $tenant->fresh()->balance_rub)->toBe('750.00');
|
|
});
|
|
|
|
test('topup использует bcmath — нет float-дрейфа на 0.1 + 0.2', function () {
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '0.10']);
|
|
|
|
app(BillingTopupService::class)->topup($tenant->id, '0.20', null);
|
|
|
|
expect((string) $tenant->fresh()->balance_rub)->toBe('0.30');
|
|
});
|
|
|
|
test('topup фиксирует user_id инициатора', function () {
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '0.00']);
|
|
$user = User::factory()->create(['tenant_id' => $tenant->id]);
|
|
|
|
$tx = app(BillingTopupService::class)->topup($tenant->id, '100.00', $user->id);
|
|
|
|
expect($tx->user_id)->toBe($user->id);
|
|
});
|
|
|
|
test('topup-строка получает log_hash через append-only hash-chain триггер', function () {
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '0.00']);
|
|
|
|
$tx = app(BillingTopupService::class)->topup($tenant->id, '100.00', null);
|
|
|
|
$hasHash = DB::table('balance_transactions')
|
|
->where('id', $tx->id)
|
|
->whereNotNull('log_hash')
|
|
->exists();
|
|
expect($hasHash)->toBeTrue();
|
|
});
|