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

95 lines
2.9 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
use App\Models\ClientSmsAutoRule;
use App\Models\ClientSmsSender;
use App\Models\Tenant;
use Illuminate\Database\QueryException;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
uses(RefreshDatabase::class);
it('creates a client sms sender and reads back casts correctly', function () {
$tenant = Tenant::factory()->create();
$sender = ClientSmsSender::create([
'tenant_id' => $tenant->id,
'name' => 'LIDERRA',
'name_type' => ClientSmsSender::TYPE_COMPANY,
'status' => ClientSmsSender::STATUS_PENDING,
'operators' => ['mts'],
'monthly_fee_rub' => '2500.00',
'consent_at' => now(),
'note' => null,
'requested_by' => null,
'approved_at' => null,
'paid_until' => now()->toDateString(),
'debt_since' => null,
]);
$sender->refresh();
expect($sender->operators)->toBeArray()
->and($sender->operators)->toBe(['mts'])
->and((string) $sender->monthly_fee_rub)->toBe('2500.00')
->and($sender->paid_until)->toBeInstanceOf(Carbon\Carbon::class)
->and($sender->status)->toBe(ClientSmsSender::STATUS_PENDING);
});
it('creates a client sms auto rule and reads back the boolean cast', function () {
$tenant = Tenant::factory()->create();
$rule = ClientSmsAutoRule::create([
'tenant_id' => $tenant->id,
'enabled' => true,
'body' => 'текст',
'sender_name' => 'LIDERRA',
'updated_by' => null,
]);
$rule->refresh();
expect($rule->enabled)->toBeTrue()
->and($rule->enabled)->toBeBool();
});
it('enforces a unique sender per tenant', function () {
$tenant = Tenant::factory()->create();
ClientSmsSender::create([
'tenant_id' => $tenant->id,
'name' => 'LIDERRA',
'name_type' => ClientSmsSender::TYPE_COMPANY,
'status' => ClientSmsSender::STATUS_PENDING,
'operators' => ['mts'],
'monthly_fee_rub' => '0.00',
'consent_at' => null,
'note' => null,
'requested_by' => null,
'approved_at' => null,
'paid_until' => null,
'debt_since' => null,
]);
// Nested transaction (Postgres savepoint) so the aborted INSERT doesn't
// poison the outer RefreshDatabase transaction for the rest of the test.
expect(fn () => DB::transaction(fn () => ClientSmsSender::create([
'tenant_id' => $tenant->id,
'name' => 'OTHER',
'name_type' => ClientSmsSender::TYPE_WEBSITE,
'status' => ClientSmsSender::STATUS_PENDING,
'operators' => ['beeline'],
'monthly_fee_rub' => '0.00',
'consent_at' => null,
'note' => null,
'requested_by' => null,
'approved_at' => null,
'paid_until' => null,
'debt_since' => null,
])))->toThrow(QueryException::class);
expect(ClientSmsSender::where('tenant_id', $tenant->id)->count())->toBe(1);
});