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

118 lines
4.8 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\AdCampaign;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use function Pest\Laravel\actingAs;
uses(DatabaseTransactions::class);
it('создаёт черновик кампании показов без клик-полей', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$res = actingAs($user)->postJson('/api/advertising/campaigns', [
'name' => 'Показы 1', 'audience_days' => 14,
]);
$res->assertCreated();
$c = AdCampaign::firstWhere('name', 'Показы 1');
expect($c)->not->toBeNull();
expect($c->status)->toBe('draft');
});
it('патчит частоту, показы и бюджет показов', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$c = AdCampaign::create(['tenant_id' => $tenant->id, 'name' => 'X', 'audience_days' => 10]);
$res = actingAs($user)->patchJson("/api/advertising/campaigns/{$c->id}", [
'frequency' => 5, 'frequency_period_days' => 30,
'estimated_impressions' => 2500, 'budget_rub' => '300.00',
]);
$res->assertOk();
$c->refresh();
expect($c->frequency)->toBe(5);
expect($c->estimated_impressions)->toBe(2500);
expect((string) $c->budget_rub)->toBe('300.00');
});
it('создаёт кампанию в ручном режиме со своим списком, периодом и сроком показа', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$res = actingAs($user)->postJson('/api/advertising/campaigns', [
'name' => 'Ручная', 'audience_days' => 14, 'mode' => 'manual',
'use_uploaded_list' => true,
'snapshot_from' => '2026-07-01', 'snapshot_to' => '2026-07-20', 'run_days' => 14,
'client_cpm_rub' => '99.90',
]);
$res->assertCreated();
$c = AdCampaign::firstWhere('name', 'Ручная');
expect($c->mode)->toBe('manual')
->and($c->use_uploaded_list)->toBeTrue()
->and($c->snapshot_from->format('Y-m-d'))->toBe('2026-07-01')
->and($c->snapshot_to->format('Y-m-d'))->toBe('2026-07-20')
->and($c->run_days)->toBe(14)
->and($c->client_cpm_rub)->toBe('99.90');
});
it('в авто-режиме форсит use_uploaded_list=false при создании, даже если пришло true', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$res = actingAs($user)->postJson('/api/advertising/campaigns', [
'name' => 'Авто со списком', 'audience_days' => 14, 'mode' => 'auto',
'use_uploaded_list' => true,
]);
$res->assertCreated();
$c = AdCampaign::firstWhere('name', 'Авто со списком');
expect($c->mode)->toBe('auto')
->and($c->use_uploaded_list)->toBeFalse();
});
it('переключение на auto через update форсит use_uploaded_list=false', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$c = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'Была ручная', 'mode' => 'manual',
'audience_days' => 10, 'use_uploaded_list' => true,
]);
$res = actingAs($user)->patchJson("/api/advertising/campaigns/{$c->id}", ['mode' => 'auto']);
$res->assertOk();
$c->refresh();
expect($c->mode)->toBe('auto')
->and($c->use_uploaded_list)->toBeFalse();
});
it('update без смены режима у auto-кампании держит use_uploaded_list=false', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$c = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'Авто', 'mode' => 'auto',
'audience_days' => 10,
]);
$res = actingAs($user)->patchJson("/api/advertising/campaigns/{$c->id}", ['use_uploaded_list' => true]);
$res->assertOk();
$c->refresh();
expect($c->use_uploaded_list)->toBeFalse();
});
it('update ручной кампании сохраняет use_uploaded_list=true как пришло', function () {
$tenant = Tenant::factory()->create();
$user = User::factory()->create(['tenant_id' => $tenant->id]);
$c = AdCampaign::create([
'tenant_id' => $tenant->id, 'name' => 'Ручная', 'mode' => 'manual',
'audience_days' => 10,
]);
$res = actingAs($user)->patchJson("/api/advertising/campaigns/{$c->id}", ['use_uploaded_list' => true]);
$res->assertOk();
$c->refresh();
expect($c->use_uploaded_list)->toBeTrue();
});