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

51 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\AdCampaign;
use App\Models\Deal;
use App\Models\Tenant;
use App\Services\Advertising\CampaignAudienceBuilder;
it('collects unique deal phones in window plus uploaded list', function () {
$tenant = Tenant::factory()->create();
$campaign = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'C', 'audience_days' => 10,
'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', 'audience_days' => 10,
'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);
});