8c70255d2b
Закрывает 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.
117 lines
3.8 KiB
PHP
117 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Deal;
|
|
use App\Models\Project;
|
|
use App\Models\SupplierLead;
|
|
use App\Models\SupplierProject;
|
|
use App\Models\SystemSetting;
|
|
use App\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\Concerns\SharesSupplierPdo;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
uses(SharesSupplierPdo::class);
|
|
|
|
beforeEach(function (): void {
|
|
SystemSetting::query()->where('key', 'supplier_webhook_secret')
|
|
->update(['value' => 'test-secret-32chars-aaaaaaaaaaaaaa']);
|
|
});
|
|
|
|
it('end-to-end: 1 webhook → 3 deal copies for 3 active tenants', function (): void {
|
|
$supplier = SupplierProject::factory()->create([
|
|
'platform' => 'B1',
|
|
'signal_type' => 'site',
|
|
'unique_key' => 'vashinvestor.ru',
|
|
]);
|
|
|
|
$tenants = collect();
|
|
$projects = collect();
|
|
for ($i = 0; $i < 3; $i++) {
|
|
$t = Tenant::factory()->create(['balance_leads' => 100]);
|
|
$tenants->push($t);
|
|
$projects->push(Project::factory()->create([
|
|
'tenant_id' => $t->id,
|
|
'supplier_b1_project_id' => $supplier->id,
|
|
'signal_type' => 'site',
|
|
'signal_identifier' => 'vashinvestor.ru',
|
|
'is_active' => true,
|
|
'delivered_today' => 0,
|
|
'delivered_in_month' => 0,
|
|
'delivery_days_mask' => 127,
|
|
'region_mask' => 255,
|
|
'region_mode' => 'include',
|
|
'daily_limit_target' => 10,
|
|
'effective_daily_limit_today' => null,
|
|
]));
|
|
}
|
|
|
|
// 4-й tenant — paused
|
|
$pausedTenant = Tenant::factory()->create(['balance_leads' => 100]);
|
|
Project::factory()->create([
|
|
'tenant_id' => $pausedTenant->id,
|
|
'supplier_b1_project_id' => $supplier->id,
|
|
'signal_type' => 'site',
|
|
'signal_identifier' => 'vashinvestor.ru',
|
|
'is_active' => false,
|
|
]);
|
|
|
|
$vid = 432176649;
|
|
$response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
|
|
'vid' => $vid,
|
|
'project' => 'B1_vashinvestor.ru',
|
|
'tag' => 'Ваш инвестор',
|
|
'phone' => '79991234567',
|
|
'phones' => ['79991234567'],
|
|
'time' => time(),
|
|
]);
|
|
|
|
$response->assertStatus(202);
|
|
|
|
foreach ($projects as $i => $project) {
|
|
$tenant = $tenants[$i];
|
|
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
|
|
|
|
$deals = Deal::query()
|
|
->where('tenant_id', $tenant->id)
|
|
->where('source_crm_id', $vid)
|
|
->get();
|
|
expect($deals)->toHaveCount(1, "tenant {$tenant->id} expected 1 deal copy");
|
|
|
|
$deal = $deals->first();
|
|
expect($deal->phone)->toBe('79991234567');
|
|
expect($deal->project_id)->toBe($project->id);
|
|
expect($deal->duplicate_of_id)->toBeNull();
|
|
|
|
expect($tenant->fresh()->balance_leads)->toBe(99);
|
|
expect($project->fresh()->delivered_today)->toBe(1);
|
|
expect($project->fresh()->delivered_in_month)->toBe(1);
|
|
}
|
|
|
|
DB::statement("SET LOCAL app.current_tenant_id = '{$pausedTenant->id}'");
|
|
expect(Deal::query()
|
|
->where('tenant_id', $pausedTenant->id)
|
|
->where('source_crm_id', $vid)
|
|
->count())->toBe(0);
|
|
});
|
|
|
|
it('end-to-end: lead orphan (no matching projects) — 0 deals, lead stored', function (): void {
|
|
$vid = 999_888_777;
|
|
|
|
$response = $this->postJson('/api/webhook/supplier/test-secret-32chars-aaaaaaaaaaaaaa', [
|
|
'vid' => $vid,
|
|
'project' => 'B1_orphan.ru',
|
|
'phone' => '79991234567',
|
|
'time' => time(),
|
|
]);
|
|
|
|
$response->assertStatus(202);
|
|
|
|
$lead = SupplierLead::where('vid', $vid)->firstOrFail();
|
|
expect($lead->processed_at)->not->toBeNull();
|
|
expect($lead->deals_created_count)->toBe(0);
|
|
expect($lead->supplier_project_id)->not->toBeNull(); // resolveOrStub stub
|
|
});
|