Files
portal/app/tests/Feature/Autopodbor/StudyBatchApiTest.php
T

71 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\AutopodborCompetitor;
use App\Models\AutopodborRun;
use App\Models\PricingTier;
use App\Models\Project;
use App\Models\SystemSetting;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Str;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class, SharesSupplierPdo::class);
beforeEach(fn () => Queue::fake());
function studyBatchApiCompetitor(int $tenantId, string $name): AutopodborCompetitor
{
return AutopodborCompetitor::create([
'tenant_id' => $tenantId,
'name' => $name,
'origin' => 'manual',
'dedup_key' => Str::slug($name).'-'.Str::random(6),
'box' => 'field',
]);
}
it('POST /api/autopodbor/study/batch — достаточный баланс → 202 с batch_id и queued', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '300', 'type' => 'decimal']);
$c1 = studyBatchApiCompetitor($tenant->id, 'Окна А');
$c2 = studyBatchApiCompetitor($tenant->id, 'Окна Б');
$response = $this->actingAs($user)->postJson('/api/autopodbor/study/batch', [
'competitor_ids' => [$c1->id, $c2->id],
])->assertStatus(202)
->assertJsonStructure(['batch_id', 'queued'])
->assertJsonPath('queued', 2);
expect(AutopodborRun::where('batch_id', $response->json('batch_id'))->count())->toBe(2);
});
it('POST /api/autopodbor/study/batch — маленький баланс + committed проекты → 422 с topup_rub и max_affordable', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '100.00', 'delivered_in_month' => 0]);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement('SET app.current_tenant_id = '.$tenant->id);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '300', 'type' => 'decimal']);
// Активный проект-обязательство + сетка тарифов — гейт учитывает резерв проектов (паритет со StudyBatchEnqueueTest).
Project::factory()->create(['tenant_id' => $tenant->id, 'is_active' => true, 'daily_limit_target' => 10]);
PricingTier::factory()->create([
'tier_no' => 1, 'leads_in_tier' => null, 'price_per_lead_kopecks' => 3000,
'is_active' => true, 'effective_from' => now()->toDateString(),
]);
$c1 = studyBatchApiCompetitor($tenant->id, 'Окна А');
$c2 = studyBatchApiCompetitor($tenant->id, 'Окна Б');
$this->actingAs($user)->postJson('/api/autopodbor/study/batch', [
'competitor_ids' => [$c1->id, $c2->id],
])->assertStatus(422)
->assertJsonStructure(['message', 'topup_rub', 'max_affordable']);
});