Files
portal/app/tests/Feature/ClientSms/ModelTest.php
T

110 lines
3.4 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\ClientSmsCampaign;
use App\Models\ClientSmsContact;
use App\Models\ClientSmsMessage;
use App\Models\ClientSmsSettings;
use App\Models\ClientSmsTariff;
use App\Models\ClientSmsTemplate;
use App\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('creates a campaign with a related message and reads it back via hasMany', function () {
$tenant = Tenant::factory()->create();
$campaign = ClientSmsCampaign::create([
'tenant_id' => $tenant->id,
'title' => 'Тестовая кампания',
'body' => 'Здравствуйте! У вас акция.',
'sender_name' => 'LIDERRA',
'source' => ClientSmsCampaign::SOURCE_MANUAL,
'audience_days' => 30,
'status' => ClientSmsCampaign::STATUS_DRAFT,
'segments' => 1,
'planned_count' => 10,
'sent_count' => 0,
'total_sms' => 10,
'price_rub_per_sms' => '8.50',
'estimated_cost_rub' => '85.00',
'actual_cost_rub' => null,
'created_by' => null,
]);
$message = ClientSmsMessage::create([
'tenant_id' => $tenant->id,
'campaign_id' => $campaign->id,
'deal_id' => null,
'phone' => '79990000001',
'operator' => 'МТС',
'provider_key' => 'mts',
'status' => ClientSmsMessage::STATUS_PENDING,
'cost_rub' => '8.50',
'provider_message_id' => null,
'error' => null,
]);
expect($campaign->messages)->toHaveCount(1)
->and($campaign->messages->first()->id)->toBe($message->id)
->and($message->campaign->id)->toBe($campaign->id);
});
it('creates and reads back a contact and a template', function () {
$tenant = Tenant::factory()->create();
$contact = ClientSmsContact::create([
'tenant_id' => $tenant->id,
'phone' => '79990000002',
'name' => 'Иван Иванов',
'operator' => 'Билайн',
]);
$template = ClientSmsTemplate::create([
'tenant_id' => $tenant->id,
'title' => 'Приветствие',
'body' => 'Добрый день, {name}!',
]);
$contact->refresh();
$template->refresh();
expect($contact->phone)->toBe('79990000002')
->and($contact->name)->toBe('Иван Иванов')
->and($template->title)->toBe('Приветствие')
->and($template->body)->toBe('Добрый день, {name}!');
});
it('reads seeded tariffs and settings', function () {
expect(ClientSmsTariff::count())->toBe(5)
->and(ClientSmsSettings::first()->name_debt_grace_days)->toBe(29);
});
it('round-trips the decimal cast on price_rub_per_sms', function () {
$tenant = Tenant::factory()->create();
$campaign = ClientSmsCampaign::create([
'tenant_id' => $tenant->id,
'title' => 'Decimal test',
'body' => 'body',
'sender_name' => 'LIDERRA',
'source' => ClientSmsCampaign::SOURCE_DEALS,
'audience_days' => null,
'status' => ClientSmsCampaign::STATUS_DRAFT,
'segments' => 1,
'planned_count' => 0,
'sent_count' => 0,
'total_sms' => 0,
'price_rub_per_sms' => '8.5',
'estimated_cost_rub' => '0.00',
'actual_cost_rub' => null,
'created_by' => null,
]);
$campaign->refresh();
expect((string) $campaign->price_rub_per_sms)->toBe('8.50');
});