Files
portal/app/tests/Feature/Console/BillingMigrateLeadsToRubTest.php
T
Дмитрий 19ab348c58 test: капча-драйвер null в тестах плюс изоляция migrate и billing-summary от состояния БД
Капча: добавлен тестовый драйвер CAPTCHA_DRIVER=null и CAPTCHA_FAKE_PASSES
в phpunit.xml, иначе тесты наследовали боевой yandex из .env и регистрация
падала на проверке я не робот. Плюс изоляция двух тестов от состояния
учебной базы: BillingMigrateLeadsToRubTest считает операции своего тенанта,
BillingSummaryProviderTest берёт дату вне периода из следующего месяца.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-25 05:20:14 +03:00

83 lines
3.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
use App\Models\BalanceTransaction;
use App\Models\PricingTier;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
uses(DatabaseTransactions::class);
beforeEach(function () {
// Custom tier 1 with effective_from in the past — overrides PricingTierSeeder's '1970-01-01' default.
PricingTier::query()->delete();
PricingTier::create([
'tier_no' => 1,
'leads_in_tier' => 50,
'price_per_lead_kopecks' => 12000,
'is_active' => true,
'effective_from' => now()->subDay()->toDateString(),
]);
});
it('migrates balance_leads to balance_rub at tier 1 price', function () {
$tenant = Tenant::factory()->create([
'balance_leads' => 5,
'balance_rub' => '100.00',
]);
$this->artisan('billing:migrate-leads-to-rub')->assertOk();
$tenant->refresh();
expect($tenant->balance_leads)->toBe(0);
expect((string) $tenant->balance_rub)->toBe('700.00'); // 100 + 5×120 = 700
$tx = BalanceTransaction::where('tenant_id', $tenant->id)
->where('type', BalanceTransaction::TYPE_MIGRATION)
->first();
expect($tx)->not->toBeNull();
expect((int) $tx->amount_leads)->toBe(-5);
expect((string) $tx->amount_rub)->toBe('600.00');
expect((string) $tx->balance_rub_after)->toBe('700.00');
});
it('is idempotent — second run is no-op', function () {
$tenant = Tenant::factory()->create([
'balance_leads' => 5,
'balance_rub' => '100.00',
]);
$this->artisan('billing:migrate-leads-to-rub')->assertOk();
$balanceAfterFirst = $tenant->fresh()->balance_rub;
$this->artisan('billing:migrate-leads-to-rub')->assertOk();
expect($tenant->fresh()->balance_rub)->toBe($balanceAfterFirst);
// Считаем операции миграции ТОЛЬКО своего тенанта — команда работает по всей базе,
// а в тест-БД могут быть закоммиченные тенанты от других прогонов (изоляция теста).
expect(BalanceTransaction::where('tenant_id', $tenant->id)
->where('type', BalanceTransaction::TYPE_MIGRATION)->count())->toBe(1);
});
it('skips tenants with balance_leads = 0', function () {
$tenant = Tenant::factory()->create([
'balance_leads' => 0,
'balance_rub' => '500.00',
]);
$this->artisan('billing:migrate-leads-to-rub')->assertOk();
// Только свой тенант: balance_leads=0 → не мигрируется → 0 операций (не считаем чужих).
expect(BalanceTransaction::where('tenant_id', $tenant->id)
->where('type', BalanceTransaction::TYPE_MIGRATION)->count())->toBe(0);
});
it('aborts if no active tier 1 configured', function () {
PricingTier::query()->update(['is_active' => false]);
Tenant::factory()->create(['balance_leads' => 5]);
$this->artisan('billing:migrate-leads-to-rub')->assertFailed();
});