feat(Mail template & Membership relationship)
This commit is contained in:
77
tests/Feature/NotificationTemplateTest.php
Normal file
77
tests/Feature/NotificationTemplateTest.php
Normal file
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\NotificationTemplate;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class NotificationTemplateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_find_by_identifier_returns_active_template(): void
|
||||
{
|
||||
$template = NotificationTemplate::factory()->create([
|
||||
'identifier' => 'test_template',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$found = NotificationTemplate::findByIdentifier('test_template');
|
||||
|
||||
$this->assertNotNull($found);
|
||||
$this->assertEquals($template->id, $found->id);
|
||||
}
|
||||
|
||||
public function test_find_by_identifier_returns_null_for_inactive_template(): void
|
||||
{
|
||||
NotificationTemplate::factory()->inactive()->create([
|
||||
'identifier' => 'inactive_template',
|
||||
]);
|
||||
|
||||
$found = NotificationTemplate::findByIdentifier('inactive_template');
|
||||
|
||||
$this->assertNull($found);
|
||||
}
|
||||
|
||||
public function test_render_subject_replaces_placeholders(): void
|
||||
{
|
||||
$template = NotificationTemplate::factory()->create([
|
||||
'subject' => 'Bonjour {member_name}, votre adhésion expire le {expiry_date}',
|
||||
]);
|
||||
|
||||
$result = $template->renderSubject([
|
||||
'member_name' => 'Jean Dupont',
|
||||
'expiry_date' => '2026-01-31',
|
||||
]);
|
||||
|
||||
$this->assertEquals('Bonjour Jean Dupont, votre adhésion expire le 2026-01-31', $result);
|
||||
}
|
||||
|
||||
public function test_render_body_replaces_placeholders(): void
|
||||
{
|
||||
$template = NotificationTemplate::factory()->create([
|
||||
'body' => '<p>Bonjour {member_name}</p><p>Expiration : {expiry_date}</p>',
|
||||
]);
|
||||
|
||||
$result = $template->renderBody([
|
||||
'member_name' => 'Marie Martin',
|
||||
'expiry_date' => '2026-06-15',
|
||||
]);
|
||||
|
||||
$this->assertEquals('<p>Bonjour Marie Martin</p><p>Expiration : 2026-06-15</p>', $result);
|
||||
}
|
||||
|
||||
public function test_missing_placeholder_is_left_intact(): void
|
||||
{
|
||||
$template = NotificationTemplate::factory()->create([
|
||||
'subject' => 'Bonjour {member_name}, date : {expiry_date}',
|
||||
]);
|
||||
|
||||
$result = $template->renderSubject([
|
||||
'member_name' => 'Jean Dupont',
|
||||
]);
|
||||
|
||||
$this->assertEquals('Bonjour Jean Dupont, date : {expiry_date}', $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\SendSubscriptionExpiredPhase1Notifications;
|
||||
use App\Models\Member;
|
||||
use App\Models\Membership;
|
||||
use App\Models\NotificationTemplate;
|
||||
use App\Models\Package;
|
||||
use App\Notifications\SubscriptionExpiredPhase1;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SendSubscriptionExpiredPhase1NotificationsTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private function createMemberWithExpiredMembership(): Member
|
||||
{
|
||||
$package = Package::create([
|
||||
'identifier' => 'test-package',
|
||||
'name' => 'Test Package',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$member = Member::create([
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'firstname' => fake()->firstName(),
|
||||
'lastname' => fake()->lastName(),
|
||||
'status' => 'valid',
|
||||
'nature' => 'physical',
|
||||
]);
|
||||
|
||||
Membership::create([
|
||||
'member_id' => $member->id,
|
||||
'package_id' => $package->id,
|
||||
'status' => 'expired',
|
||||
'end_date' => '2025-12-31',
|
||||
'amount' => 12.00,
|
||||
'payment_status' => 'paid',
|
||||
]);
|
||||
|
||||
return $member;
|
||||
}
|
||||
|
||||
public function test_job_sends_notifications_to_expired_members(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
NotificationTemplate::factory()->create([
|
||||
'identifier' => 'subscription_expired_phase1',
|
||||
'is_active' => true,
|
||||
]);
|
||||
|
||||
$expiredMember = $this->createMemberWithExpiredMembership();
|
||||
|
||||
(new SendSubscriptionExpiredPhase1Notifications)->handle();
|
||||
|
||||
Notification::assertSentTo($expiredMember, SubscriptionExpiredPhase1::class);
|
||||
}
|
||||
|
||||
public function test_job_does_nothing_when_template_is_inactive(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
NotificationTemplate::factory()->inactive()->create([
|
||||
'identifier' => 'subscription_expired_phase1',
|
||||
]);
|
||||
|
||||
$this->createMemberWithExpiredMembership();
|
||||
|
||||
(new SendSubscriptionExpiredPhase1Notifications)->handle();
|
||||
|
||||
Notification::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_job_does_nothing_when_template_does_not_exist(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$this->createMemberWithExpiredMembership();
|
||||
|
||||
(new SendSubscriptionExpiredPhase1Notifications)->handle();
|
||||
|
||||
Notification::assertNothingSent();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user