Files
portal/app/tests/Feature/ClientTg/ModelsTest.php
T

126 lines
4.0 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\ClientTg\Campaign;
use App\Models\ClientTg\CampaignPhone;
use App\Models\ClientTg\Contact;
use App\Models\ClientTg\Setting;
use App\Models\ClientTg\Tariff;
use App\Models\ClientTg\Template;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('creates a campaign with related phones and reads them back via hasMany', function () {
$tenant = Tenant::factory()->create();
$campaign = Campaign::create([
'tenant_id' => $tenant->id,
'status' => Campaign::STATUS_DRAFT,
'ad_text' => 'Подключите наш телеграм-бот и получайте заявки быстрее',
'ad_link' => 'https://liderra.ru',
'media_path' => null,
'ord_category' => 'Размещение рекламы',
'budget_cap_rub' => '1000.00',
'audience_kind' => Campaign::AUDIENCE_DEALS,
'audience_params' => ['days' => 30],
'planned_count' => 420,
'matched_count' => null,
'estimated_cost_rub' => '168.00',
'actual_cost_rub' => null,
'created_by' => null,
]);
$phoneA = CampaignPhone::create([
'tenant_id' => $tenant->id,
'campaign_id' => $campaign->id,
'phone' => '79990000001',
'expires_at' => null,
]);
CampaignPhone::create([
'tenant_id' => $tenant->id,
'campaign_id' => $campaign->id,
'phone' => '79990000002',
'expires_at' => null,
]);
expect($campaign->phones)->toHaveCount(2)
->and($phoneA->campaign->id)->toBe($campaign->id);
});
it('round-trips the audience_params array cast', function () {
$tenant = Tenant::factory()->create();
$campaign = Campaign::create([
'tenant_id' => $tenant->id,
'status' => Campaign::STATUS_DRAFT,
'ad_text' => 'text',
'ad_link' => null,
'media_path' => null,
'ord_category' => 'Размещение рекламы',
'budget_cap_rub' => '0.00',
'audience_kind' => Campaign::AUDIENCE_LIST,
'audience_params' => ['phones_count' => 500, 'note' => 'файл'],
'created_by' => null,
]);
$campaign->refresh();
// jsonb не сохраняет порядок ключей — сверяем по ключам, не по порядку.
expect($campaign->audience_params)->toBeArray()
->and($campaign->audience_params['phones_count'])->toBe(500)
->and($campaign->audience_params['note'])->toBe('файл');
});
it('creates and reads back a contact and a template', function () {
$tenant = Tenant::factory()->create();
$contact = Contact::create([
'tenant_id' => $tenant->id,
'phone' => '79990000003',
'name' => 'Иван Иванов',
'operator' => 'Билайн',
]);
$template = Template::create([
'tenant_id' => $tenant->id,
'title' => 'Приветствие',
'body' => 'Добрый день!',
]);
$contact->refresh();
$template->refresh();
expect($contact->phone)->toBe('79990000003')
->and($contact->name)->toBe('Иван Иванов')
->and($template->title)->toBe('Приветствие');
});
it('reads seeded tariffs and settings', function () {
expect(Tariff::count())->toBe(5)
->and(Setting::first()->name_debt_grace_days)->toBe(29);
});
it('round-trips the decimal cast on budget_cap_rub', function () {
$tenant = Tenant::factory()->create();
$campaign = Campaign::create([
'tenant_id' => $tenant->id,
'status' => Campaign::STATUS_DRAFT,
'ad_text' => 'decimal test',
'ad_link' => null,
'media_path' => null,
'ord_category' => 'Размещение рекламы',
'budget_cap_rub' => '1000.5',
'audience_kind' => Campaign::AUDIENCE_DEALS,
'audience_params' => null,
'created_by' => null,
]);
$campaign->refresh();
expect((string) $campaign->budget_cap_rub)->toBe('1000.50');
});