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>
69 lines
2.5 KiB
PHP
69 lines
2.5 KiB
PHP
<?php
|
||
|
||
declare(strict_types=1);
|
||
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||
|
||
uses(DatabaseTransactions::class);
|
||
|
||
beforeEach(function () {
|
||
$this->tenant = Tenant::factory()->create(['balance_rub' => '500.00', 'balance_leads' => 12]);
|
||
$this->user = User::factory()->create(['tenant_id' => $this->tenant->id]);
|
||
$this->actingAs($this->user);
|
||
});
|
||
|
||
test('POST /api/billing/topup кредитует баланс и возвращает 201', function () {
|
||
$response = $this->postJson('/api/billing/topup', ['amount_rub' => 250]);
|
||
|
||
$response->assertStatus(201)
|
||
->assertJsonPath('balance_rub', '750.00')
|
||
->assertJsonPath('transaction.type', 'topup')
|
||
->assertJsonPath('transaction.amount_rub', '250.00');
|
||
|
||
expect((string) $this->tenant->fresh()->balance_rub)->toBe('750.00');
|
||
});
|
||
|
||
test('POST /api/billing/topup пишет строку balance_transactions с user_id', function () {
|
||
$this->postJson('/api/billing/topup', ['amount_rub' => 100])->assertStatus(201);
|
||
|
||
$this->assertDatabaseHas('balance_transactions', [
|
||
'tenant_id' => $this->tenant->id,
|
||
'user_id' => $this->user->id,
|
||
'type' => 'topup',
|
||
'amount_rub' => '100.00',
|
||
]);
|
||
});
|
||
|
||
test('POST /api/billing/topup использует bcmath-точность', function () {
|
||
$this->tenant->update(['balance_rub' => '0.10']);
|
||
|
||
$this->postJson('/api/billing/topup', ['amount_rub' => 100.20])->assertStatus(201);
|
||
|
||
expect((string) $this->tenant->fresh()->balance_rub)->toBe('100.30');
|
||
});
|
||
|
||
test('POST /api/billing/topup отклоняет сумму ниже минимума 100 ₽', function () {
|
||
$this->postJson('/api/billing/topup', ['amount_rub' => 50])
|
||
->assertStatus(422)
|
||
->assertJsonValidationErrors('amount_rub');
|
||
});
|
||
|
||
test('POST /api/billing/topup отклоняет отсутствующую сумму', function () {
|
||
$this->postJson('/api/billing/topup', [])
|
||
->assertStatus(422)
|
||
->assertJsonValidationErrors('amount_rub');
|
||
});
|
||
|
||
test('POST /api/billing/topup отклоняет более 2 знаков после запятой', function () {
|
||
$this->postJson('/api/billing/topup', ['amount_rub' => 100.123])
|
||
->assertStatus(422)
|
||
->assertJsonValidationErrors('amount_rub');
|
||
});
|
||
|
||
test('POST /api/billing/topup без auth: 401', function () {
|
||
auth()->logout();
|
||
$this->postJson('/api/billing/topup', ['amount_rub' => 100])->assertStatus(401);
|
||
});
|