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); });