Files
portal/app/tests/Feature/Services/LeadRouterTest.php
T
Дмитрий 8c70255d2b fix(supplier): Plan 3 Task 3 code-review fixes (4 Important + 3 Minor)
Закрывает 4 Important issues из code-review Task 3 (6d6181b):
- config/database.php: inline 11-key duplication заменён на single-source
  pattern через локальную переменную $pgsqlConnection (config() внутри
  config-файла не работает — Repository ещё не bootstrap'нут); 'pgsql' и
  'pgsql_supplier' теперь оба ссылаются на $pgsqlConnection; PDO options
  block с string-key _role_purpose удалён (PDO ждёт integer ATTR_* keys)
- tests/Concerns/SharesSupplierPdo.php (новый): trait для cross-connection
  PDO visibility в DatabaseTransactions; setUp override из TestCase.php
  удалён (был global на 562 теста, forced eager PDO connect);
  trait применён к 5 supplier-flow тестам: SupplierConnectionTest,
  LeadRouterTest, RouteSupplierLeadJobTest, ResetDeliveredTodayCommandTest,
  SupplierLeadFlowTest (все нуждаются в cross-connection видимости)
- phpstan-baseline.neon: entry для Pest TestCall->artisan() в
  SupplierConnectionTest заменён на inline @phpstan-ignore-next-line
  — local + self-documenting; добавлен baseline-entry для
  SharesSupplierPdo trait.unused (PHPStan не видит Pest uses() как trait usage)

Plus 3 Minor:
- typos 'dafault'/'corretly' (удалились с setUp override из TestCase.php)
- RouteSupplierLeadJob.php PHPDoc: \$connection → DB_CONNECTION консистентность

Pest: 562 tests, 560 passed + 2 skipped (без regression). PHPStan: 0 errors. Pint: clean.
2026-05-11 01:26:24 +03:00

216 lines
8.0 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Project;
use App\Models\SupplierProject;
use App\Models\Tenant;
use App\Services\LeadRouter;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SharesSupplierPdo;
uses(DatabaseTransactions::class);
uses(SharesSupplierPdo::class);
beforeEach(function (): void {
// Clear tenant context — LeadRouter operates without it (sharing across tenants).
// Use set_config (session-scoped, rolls back via DatabaseTransactions).
DB::statement("SELECT set_config('app.current_tenant_id', '0', true)");
});
it('returns matching active projects for B1 site supplier_project (sharing across tenants)', function (): void {
$supplier = SupplierProject::factory()->create([
'platform' => 'B1',
'signal_type' => 'site',
'unique_key' => 'vashinvestor.ru',
]);
$tenant1 = Tenant::factory()->create(['balance_leads' => 100]);
$tenant2 = Tenant::factory()->create(['balance_leads' => 100]);
$project1 = Project::factory()->create([
'tenant_id' => $tenant1->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'vashinvestor.ru',
'is_active' => true,
'daily_limit_target' => 10,
'delivered_today' => 0,
'delivery_days_mask' => 127,
'region_mask' => 255,
'region_mode' => 'include',
]);
$project2 = Project::factory()->create([
'tenant_id' => $tenant2->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'vashinvestor.ru',
'is_active' => true,
'daily_limit_target' => 10,
'delivered_today' => 0,
'delivery_days_mask' => 127,
'region_mask' => 255,
'region_mode' => 'include',
]);
$router = app(LeadRouter::class);
$matched = $router->matchEligibleProjects($supplier, '79991234567');
expect($matched)->toHaveCount(2);
expect($matched->pluck('id')->all())->toEqualCanonicalizing([$project1->id, $project2->id]);
});
it('skips paused project (is_active=false)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => false,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('skips project where today is not in delivery_days_mask', function (): void {
// Mirror LeadRouter's МСК alignment to avoid off-by-one near midnight when
// process TZ (UTC) and Europe/Moscow disagree on ISO day-of-week.
$todayBit = 1 << (now('Europe/Moscow')->isoWeekday() - 1);
$maskWithoutToday = 127 & ~$todayBit;
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'delivery_days_mask' => $maskWithoutToday,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('skips project where delivered_today >= effective_daily_limit_today', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'effective_daily_limit_today' => 5,
'delivered_today' => 5,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('falls back to daily_limit_target when effective_daily_limit_today is null', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'effective_daily_limit_today' => null,
'daily_limit_target' => 10,
'delivered_today' => 5,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(1);
});
it('skips project where region_mode=include and region_mask does not include phone district', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'region_mask' => 1, // только Центральный округ
'region_mode' => 'include',
]);
$router = app(LeadRouter::class);
// 78121234567 = СПб (Северо-Западный, бит 2)
expect($router->matchEligibleProjects($supplier, '78121234567'))->toHaveCount(0);
});
it('skips project where tenant.balance_leads <= 0', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 0]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(0);
});
it('routes through correct FK based on platform (B2 → supplier_b2_project_id)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B2', 'signal_type' => 'site']);
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => null,
'supplier_b2_project_id' => $supplier->id,
'supplier_b3_project_id' => null,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
]);
$router = app(LeadRouter::class);
expect($router->matchEligibleProjects($supplier, '79991234567'))->toHaveCount(1);
});
it('orders results by created_at ASC (deterministic, spec §6 step 4)', function (): void {
$supplier = SupplierProject::factory()->create(['platform' => 'B1', 'signal_type' => 'site']);
$projectsCreated = collect();
for ($i = 0; $i < 3; $i++) {
$tenant = Tenant::factory()->create(['balance_leads' => 100]);
$projectsCreated->push(
Project::factory()->create([
'tenant_id' => $tenant->id,
'supplier_b1_project_id' => $supplier->id,
'signal_type' => 'site',
'signal_identifier' => 'example.com',
'is_active' => true,
'created_at' => now()->subDays(3 - $i),
])
);
}
$router = app(LeadRouter::class);
$matched = $router->matchEligibleProjects($supplier, '79991234567');
expect($matched->pluck('id')->all())->toBe($projectsCreated->pluck('id')->all());
});