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

67 lines
3.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\Tenant;
use App\Models\User;
use App\Models\SystemSetting;
use App\Models\AutopodborRun;
use App\Models\AutopodborCompetitor;
use App\Models\AutopodborSource;
use App\Models\Project;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Queue;
uses(DatabaseTransactions::class, \Tests\Concerns\SharesSupplierPdo::class);
beforeEach(fn () => Queue::fake());
it('GET /api/autopodbor/state — доступность, прогоны, цены', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
SystemSetting::updateOrCreate(['key' => 'autopodbor_enabled'], ['value' => '1', 'type' => 'bool']);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '500', 'type' => 'decimal']);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_study_rub'], ['value' => '300', 'type' => 'decimal']);
$this->actingAs($user)->getJson('/api/autopodbor/state')
->assertOk()
->assertJsonStructure(['enabled', 'runs', 'prices' => ['search', 'study']]);
});
it('POST /api/autopodbor/search — стартует прогон (201)', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '1', 'type' => 'decimal']);
$this->actingAs($user)->postJson('/api/autopodbor/search', [
'region_code' => 16, 'examples' => ['okna.ru'], 'about_self' => [], 'include_federal' => true,
])->assertCreated()->assertJsonPath('data.kind', 'search');
});
it('GET /api/autopodbor/competitors/{id} — источники с existing_project_id', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement("SET app.current_tenant_id = ".$tenant->id);
$run = AutopodborRun::create(['tenant_id'=>$tenant->id,'kind'=>'search','status'=>'done','region_code'=>16,'params'=>[]]);
$comp = AutopodborCompetitor::create(['tenant_id'=>$tenant->id,'search_run_id'=>$run->id,'name'=>'Окна Комфорт','dedup_key'=>'okna']);
AutopodborSource::create(['tenant_id'=>$tenant->id,'competitor_id'=>$comp->id,'study_run_id'=>$run->id,'signal_type'=>'site','identifier'=>'okna-komfort.ru','dedup_key'=>'site:okna-komfort.ru']);
$this->actingAs($user)->getJson("/api/autopodbor/competitors/{$comp->id}")
->assertOk()
->assertJsonStructure(['data'=>['id','name'], 'sources'=>[['id','signal_type','identifier','existing_project_id']]]);
});
it('POST /api/autopodbor/projects — создаёт проекты из источников (201)', function () {
$tenant = Tenant::factory()->create(['balance_rub' => '500000.00']);
$user = User::factory()->create(['tenant_id' => $tenant->id]);
DB::statement("SET app.current_tenant_id = ".$tenant->id);
$run = AutopodborRun::create(['tenant_id'=>$tenant->id,'kind'=>'study','status'=>'done','region_code'=>16,'params'=>[]]);
$comp = AutopodborCompetitor::create(['tenant_id'=>$tenant->id,'search_run_id'=>$run->id,'name'=>'Окна Комфорт','dedup_key'=>'okna']);
$s1 = AutopodborSource::create(['tenant_id'=>$tenant->id,'competitor_id'=>$comp->id,'study_run_id'=>$run->id,'signal_type'=>'site','identifier'=>'okna-komfort.ru','dedup_key'=>'site:okna-komfort.ru']);
$this->actingAs($user)->postJson('/api/autopodbor/projects', [
'source_ids'=>[$s1->id], 'regions'=>[16], 'daily_limit_target'=>20, 'delivery_days_mask'=>127, 'launch'=>false,
])->assertCreated();
expect(Project::where('tenant_id',$tenant->id)->where('signal_identifier','okna-komfort.ru')->exists())->toBeTrue();
});