63 lines
2.9 KiB
PHP
63 lines
2.9 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
use App\Models\AutopodborRun;
|
||
|
|
use App\Models\AutopodborCompetitor;
|
||
|
|
use App\Models\SystemSetting;
|
||
|
|
use App\Models\Tenant;
|
||
|
|
use App\Services\Autopodbor\AutopodborRunService;
|
||
|
|
use App\Jobs\Autopodbor\RunAutopodborSearchJob;
|
||
|
|
use App\Jobs\Autopodbor\RunAutopodborResolveJob;
|
||
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
||
|
|
use Illuminate\Support\Facades\DB;
|
||
|
|
use Illuminate\Support\Facades\Queue;
|
||
|
|
|
||
|
|
uses(DatabaseTransactions::class, \Tests\Concerns\SharesSupplierPdo::class);
|
||
|
|
|
||
|
|
it('стартует search, создаёт queued-прогон и ставит джобу', function () {
|
||
|
|
Queue::fake();
|
||
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
|
||
|
|
DB::statement("SET app.current_tenant_id = ".$tenant->id);
|
||
|
|
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '500', 'type' => 'decimal']);
|
||
|
|
|
||
|
|
$run = app(AutopodborRunService::class)->startSearch($tenant->id, 16, ['okna.ru'], [], true);
|
||
|
|
|
||
|
|
expect($run->kind)->toBe('search')->and($run->status)->toBe('queued');
|
||
|
|
Queue::assertPushed(RunAutopodborSearchJob::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('не стартует второй in-flight search того же tenant', function () {
|
||
|
|
Queue::fake();
|
||
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100000.00']);
|
||
|
|
DB::statement("SET app.current_tenant_id = ".$tenant->id);
|
||
|
|
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '500', 'type' => 'decimal']);
|
||
|
|
$svc = app(AutopodborRunService::class);
|
||
|
|
$svc->startSearch($tenant->id, 16, ['okna.ru'], [], true);
|
||
|
|
|
||
|
|
expect(fn () => $svc->startSearch($tenant->id, 16, ['okna.ru'], [], true))
|
||
|
|
->toThrow(\App\Exceptions\Autopodbor\RunInFlightException::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('гейтит по балансу: нехватка на цену search → InsufficientBalanceException', function () {
|
||
|
|
Queue::fake();
|
||
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '100.00']);
|
||
|
|
DB::statement("SET app.current_tenant_id = ".$tenant->id);
|
||
|
|
SystemSetting::updateOrCreate(['key' => 'autopodbor_price_search_rub'], ['value' => '500', 'type' => 'decimal']);
|
||
|
|
|
||
|
|
expect(fn () => app(AutopodborRunService::class)->startSearch($tenant->id, 16, ['okna.ru'], [], true))
|
||
|
|
->toThrow(\App\Exceptions\Billing\InsufficientBalanceException::class);
|
||
|
|
Queue::assertNotPushed(RunAutopodborSearchJob::class);
|
||
|
|
});
|
||
|
|
|
||
|
|
it('startResolve бесплатный: стартует даже при нулевом балансе, ставит resolve-джобу', function () {
|
||
|
|
Queue::fake();
|
||
|
|
$tenant = Tenant::factory()->create(['balance_rub' => '0.00']);
|
||
|
|
DB::statement("SET app.current_tenant_id = ".$tenant->id);
|
||
|
|
|
||
|
|
$run = app(AutopodborRunService::class)->startResolve($tenant->id, 'Окна Комфорт', 16);
|
||
|
|
|
||
|
|
expect($run->kind)->toBe('resolve')->and($run->status)->toBe('queued');
|
||
|
|
Queue::assertPushed(RunAutopodborResolveJob::class);
|
||
|
|
});
|