Files
portal/app/tests/Feature/Models/LeadChargeTest.php
T
Дмитрий 31f813315d feat(models): add LeadCharge ledger model + factory + relations to Tenant/Deal
- app/Models/LeadCharge.php — fillable + casts (datetime + integer)
  + tenant() BelongsTo relation
  + deal() BelongsTo relation (по deal_id, без deal_received_at — composite PK
    на партиционированной обеспечивается БД-уровнем через FK)
  + accessor priceRubles (kopecks → float)
- database/factories/LeadChargeFactory.php — НЕ создаёт реальный Deal автоматически
  (composite FK requires (deal_id, deal_received_at) пары); тесты с FK-целостностью
  явно создают Deal::factory() и передают пару в state()
- tests/Feature/Models/LeadChargeTest.php — 4 теста: factory, tenant relation,
  deal relation, priceRubles accessor. testing_rls_user setup в beforeEach
  для проверки RLS context из не-superuser контекста.

Quirk: SET LOCAL app.current_tenant_id НЕ принимает параметрическое связывание PG —
используем string interpolation с {$tenant->id} как в RlsSmokeTest pattern.

ide-helper:models -W -M -N синхронизировал docblocks (WebhookDedupKey).

Pest: 459 / 457 passed / 2 skipped (453 + 4 новых = 457).
Larastan: 0 errors. Pint passed.

Spec: §7.4
Plan: Task 8

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-10 16:52:10 +03:00

101 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\Deal;
use App\Models\LeadCharge;
use App\Models\Project;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
uses(DatabaseTransactions::class);
beforeEach(function () {
// Создаём testing_rls_user если нет — паттерн из RlsSmokeTest для проверки
// RLS из не-superuser контекста (postgres BYPASSRLS обходит политику).
DB::unprepared(<<<'SQL'
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'testing_rls_user') THEN
CREATE ROLE testing_rls_user NOLOGIN;
GRANT USAGE ON SCHEMA public TO testing_rls_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO testing_rls_user;
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO testing_rls_user;
END IF;
END
$$;
SQL);
DB::statement('GRANT SELECT, INSERT ON TABLE lead_charges TO testing_rls_user');
DB::statement('GRANT USAGE, SELECT ON SEQUENCE lead_charges_id_seq TO testing_rls_user');
});
test('LeadCharge can be created via factory', function () {
$tenant = Tenant::factory()->create();
$project = Project::factory()->for($tenant)->create();
$deal = Deal::factory()->for($tenant)->for($project)->create();
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
$charge = LeadCharge::factory()->create([
'tenant_id' => $tenant->id,
'deal_id' => $deal->id,
'deal_received_at' => $deal->received_at,
]);
expect($charge->tier_no)->toBeInt();
expect($charge->price_per_lead_kopecks)->toBeInt();
});
test('LeadCharge belongs to tenant', function () {
$tenant = Tenant::factory()->create();
$project = Project::factory()->for($tenant)->create();
$deal = Deal::factory()->for($tenant)->for($project)->create();
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
$charge = LeadCharge::factory()->create([
'tenant_id' => $tenant->id,
'deal_id' => $deal->id,
'deal_received_at' => $deal->received_at,
]);
expect($charge->tenant)->toBeInstanceOf(Tenant::class);
expect($charge->tenant->id)->toBe($tenant->id);
});
test('LeadCharge belongs to deal via deal_id', function () {
$tenant = Tenant::factory()->create();
$project = Project::factory()->for($tenant)->create();
$deal = Deal::factory()->for($tenant)->for($project)->create();
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
$charge = LeadCharge::factory()->create([
'tenant_id' => $tenant->id,
'deal_id' => $deal->id,
'deal_received_at' => $deal->received_at,
]);
expect($charge->deal)->toBeInstanceOf(Deal::class);
expect($charge->deal->id)->toBe($deal->id);
});
test('priceRubles accessor', function () {
$tenant = Tenant::factory()->create();
$project = Project::factory()->for($tenant)->create();
$deal = Deal::factory()->for($tenant)->for($project)->create();
DB::statement("SET LOCAL app.current_tenant_id = '{$tenant->id}'");
$charge = LeadCharge::factory()->create([
'tenant_id' => $tenant->id,
'deal_id' => $deal->id,
'deal_received_at' => $deal->received_at,
'price_per_lead_kopecks' => 5500,
]);
expect($charge->price_rubles)->toBe(55.0);
});