feat(billing-v2-c): BalancePreflightService — pure-проверка платёжеспособности
Task 1.2 Спека C. evaluate(balanceRub, deliveredInMonth, requiredLeads, tiers) →
PreflightResult{passes, requiredLeads, capacityLeads, deficitLeads}. Сравнение в
лидах через BalanceToLeadsConverter::convert (7 ступеней + месячный объём).
3 unit-теста GREEN. Pint passed.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use App\Models\PricingTier;
|
||||
use App\Services\Billing\BalancePreflightService;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
|
||||
function makeTiers(): Collection
|
||||
{
|
||||
// 1 ступень: цена 50₽/лид (5000 копеек), безлимитная.
|
||||
return new Collection([
|
||||
new PricingTier(['tier_no' => 1, 'leads_in_tier' => null, 'price_per_lead_kopecks' => 5000, 'is_active' => true]),
|
||||
]);
|
||||
}
|
||||
|
||||
it('passes when balance covers full daily limit', function () {
|
||||
$service = new BalancePreflightService;
|
||||
// 2000₽ / 50₽ = 40 лидов capacity; нужно 30 → проходит.
|
||||
$result = $service->evaluate(
|
||||
balanceRub: '2000.00',
|
||||
deliveredInMonth: 0,
|
||||
requiredLeads: 30,
|
||||
tiers: makeTiers(),
|
||||
);
|
||||
|
||||
expect($result->passes)->toBeTrue();
|
||||
expect($result->capacityLeads)->toBe(40);
|
||||
expect($result->requiredLeads)->toBe(30);
|
||||
expect($result->deficitLeads)->toBe(0);
|
||||
});
|
||||
|
||||
it('fails when balance below full daily limit', function () {
|
||||
$service = new BalancePreflightService;
|
||||
// 1000₽ / 50₽ = 20 лидов capacity; нужно 30 → не проходит, дефицит 10.
|
||||
$result = $service->evaluate(
|
||||
balanceRub: '1000.00',
|
||||
deliveredInMonth: 0,
|
||||
requiredLeads: 30,
|
||||
tiers: makeTiers(),
|
||||
);
|
||||
|
||||
expect($result->passes)->toBeFalse();
|
||||
expect($result->capacityLeads)->toBe(20);
|
||||
expect($result->deficitLeads)->toBe(10);
|
||||
});
|
||||
|
||||
it('passes trivially when requiredLeads is zero', function () {
|
||||
$service = new BalancePreflightService;
|
||||
$result = $service->evaluate('0.00', 0, 0, makeTiers());
|
||||
expect($result->passes)->toBeTrue();
|
||||
});
|
||||
Reference in New Issue
Block a user