33 lines
1.3 KiB
PHP
33 lines
1.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Exceptions\Billing\InsufficientBalanceException;
|
||
|
|
use App\Models\AdWallet;
|
||
|
|
use App\Models\Tenant;
|
||
|
|
use App\Services\Advertising\AdWalletService;
|
||
|
|
|
||
|
|
it('freezes amount into frozen_rub and blocks over-freeze', function () {
|
||
|
|
$tenant = Tenant::factory()->create();
|
||
|
|
AdWallet::create(['tenant_id' => $tenant->id, 'balance_rub' => '3000.00', 'frozen_rub' => '0.00']);
|
||
|
|
$svc = app(AdWalletService::class);
|
||
|
|
|
||
|
|
$svc->freeze($tenant->id, channel: 'yandex', sourceType: 'campaign', sourceId: 1, amountRub: '2500.00');
|
||
|
|
$wallet = AdWallet::where('tenant_id', $tenant->id)->first();
|
||
|
|
expect($wallet->frozen_rub)->toBe('2500.00');
|
||
|
|
|
||
|
|
// свободно 500 < 2500 → нельзя заморозить второй раз
|
||
|
|
expect(fn () => $svc->freeze($tenant->id, 'yandex', 'campaign', 2, '2500.00'))
|
||
|
|
->toThrow(InsufficientBalanceException::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('releases a hold back to free balance', function () {
|
||
|
|
$tenant = Tenant::factory()->create();
|
||
|
|
AdWallet::create(['tenant_id' => $tenant->id, 'balance_rub' => '3000.00', 'frozen_rub' => '0.00']);
|
||
|
|
$svc = app(AdWalletService::class);
|
||
|
|
$svc->freeze($tenant->id, 'yandex', 'campaign', 1, '2500.00');
|
||
|
|
|
||
|
|
$svc->release($tenant->id, 'yandex', 'campaign', 1);
|
||
|
|
expect(AdWallet::where('tenant_id', $tenant->id)->first()->frozen_rub)->toBe('0.00');
|
||
|
|
});
|