Files
portal/app/tests/Feature/Advertising/CampaignAudienceBuilderTest.php
T

61 lines
3.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\AdCampaign;
use App\Models\Deal;
use App\Models\Tenant;
use App\Services\Advertising\CampaignAudienceBuilder;
// T3: свой список (ad_campaign_phones) считается только в mode=manual — auto
// его больше не подмешивает (см. AudienceBuilderModeTest.php). Оба теста ниже
// ставят mode=manual + snapshot_from/to, чтобы сохранить исходный смысл проверки
// (окно сделок + список одновременно).
it('collects unique deal phones in window plus uploaded list', function () {
$tenant = Tenant::factory()->create();
$campaign = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'C', 'mode' => AdCampaign::MODE_MANUAL,
'audience_days' => 10,
'snapshot_from' => now()->subDays(10)->format('Y-m-d'),
'snapshot_to' => now()->format('Y-m-d'),
'use_uploaded_list' => true, 'weekly_budget_rub' => '2500.00',
]);
// сделки в окне и вне окна (RLS-контекст выставит билдер)
Deal::factory()->create(['tenant_id' => $tenant->id, 'phone' => '79990000001', 'received_at' => now()->subDays(3)]);
Deal::factory()->create(['tenant_id' => $tenant->id, 'phone' => '79990000002', 'received_at' => now()->subDays(30)]);
DB::table('ad_campaign_phones')->insert([
'tenant_id' => $tenant->id, 'campaign_id' => $campaign->id, 'phone' => '79990000003',
'expires_at' => now()->addDays(5), 'created_at' => now(), 'updated_at' => now(),
]);
$phones = app(CampaignAudienceBuilder::class)->build($campaign);
expect($phones)->toContain('79990000001') // в окне
->and($phones)->not->toContain('79990000002') // вне окна
->and($phones)->toContain('79990000003'); // свой список
});
it('counts audience size', function () {
$tenant = Tenant::factory()->create();
$campaign = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'C2', 'mode' => AdCampaign::MODE_MANUAL,
'audience_days' => 10,
'snapshot_from' => now()->subDays(10)->format('Y-m-d'),
'snapshot_to' => now()->format('Y-m-d'),
'use_uploaded_list' => true, 'weekly_budget_rub' => '2500.00',
]);
Deal::factory()->create(['tenant_id' => $tenant->id, 'phone' => '79990000011', 'received_at' => now()->subDays(2)]);
Deal::factory()->create(['tenant_id' => $tenant->id, 'phone' => '79990000012', 'received_at' => now()->subDays(4)]);
// вне окна — не должна попасть в счётчик
Deal::factory()->create(['tenant_id' => $tenant->id, 'phone' => '79990000013', 'received_at' => now()->subDays(30)]);
DB::table('ad_campaign_phones')->insert([
'tenant_id' => $tenant->id, 'campaign_id' => $campaign->id, 'phone' => '79990000014',
'expires_at' => null, 'created_at' => now(), 'updated_at' => now(),
]);
$builder = app(CampaignAudienceBuilder::class);
expect($builder->size($campaign))->toBe(count($builder->build($campaign)))
->and($builder->size($campaign))->toBe(3);
});