Files

97 lines
3.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Tests\Feature\Api\Admin\Service;
use App\Mail\ServiceRenewalReminderMail;
use App\Models\Customer;
use App\Models\Service;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Mail;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class RenewalReminderTest extends TestCase
{
use RefreshDatabase;
private const BASE = '/api/v1/administrator/services/renewal-reminders';
private User $admin;
private Customer $customer;
private Service $soon;
private Service $later;
protected function setUp(): void
{
parent::setUp();
$this->seed(\Database\Seeders\TemplateSeeder::class);
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
$this->admin = User::factory()->create();
$this->admin->assignRole('Full Administrator');
$this->customer = Customer::factory()->create();
$this->soon = Service::create(['customer_id' => $this->customer->id, 'status' => 'active', 'expires_at' => now()->addDays(5)]);
$this->later = Service::create(['customer_id' => $this->customer->id, 'status' => 'active', 'expires_at' => now()->addDays(40)]);
Service::create(['customer_id' => $this->customer->id, 'status' => 'suspended', 'expires_at' => now()->addDays(3)]);
}
public function test_lists_active_services_expiring_within_days(): void
{
$this->actingAs($this->admin, 'admin')
->getJson(self::BASE . '?days=30')
->assertOk()
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.id', $this->soon->id)
->assertJsonPath('data.0.days_left', 5);
}
public function test_send_queues_reminders_bypassing_auto_disabled_scenario(): void
{
Mail::fake();
Setting::updateOrCreate(['key' => 'email_settings'], ['value' => json_encode(['auto_disabled' => ['service_renewal_reminder']])]);
$this->actingAs($this->admin, 'admin')
->postJson(self::BASE, ['service_ids' => [$this->soon->id, $this->later->id]])
->assertOk();
Mail::assertQueued(ServiceRenewalReminderMail::class, 2);
$this->assertDatabaseCount('mail', 2);
$this->assertDatabaseHas('mail', ['customer_id' => $this->soon->customer_id, 'sent_by' => $this->admin->id]);
}
public function test_send_is_rejected_when_mail_is_disabled(): void
{
Mail::fake();
Setting::updateOrCreate(['key' => 'email_settings'], ['value' => json_encode(['mail_enabled' => false])]);
$this->actingAs($this->admin, 'admin')
->postJson(self::BASE, ['service_ids' => [$this->soon->id]])
->assertStatus(422);
Mail::assertNothingQueued();
}
public function test_customer_service_reminder_queues_mail_and_records_log(): void
{
Mail::fake();
$this->actingAs($this->admin, 'admin')
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/services/{$this->soon->id}/notify-renew")
->assertOk()
->assertJsonPath('error', false);
Mail::assertQueued(ServiceRenewalReminderMail::class, 1);
$this->assertDatabaseHas('mail', [
'customer_id' => $this->customer->id,
'sent_by' => $this->admin->id,
'to' => $this->customer->email,
]);
}
}