|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature; |
| 4 | + |
| 5 | +use App\Models\Lead; |
| 6 | +use App\Notifications\NewLeadSubmitted; |
| 7 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 8 | +use Illuminate\Support\Facades\Notification; |
| 9 | +use PHPUnit\Framework\Attributes\Test; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class ResendLeadNotificationsTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + #[Test] |
| 17 | + public function it_resends_notifications_for_leads_from_the_given_date(): void |
| 18 | + { |
| 19 | + Notification::fake(); |
| 20 | + |
| 21 | + $oldLead = Lead::factory()->create(['created_at' => '2025-01-01 12:00:00']); |
| 22 | + $matchingLead = Lead::factory()->create(['created_at' => '2025-03-15 09:00:00']); |
| 23 | + $newerLead = Lead::factory()->create(['created_at' => '2025-03-16 14:00:00']); |
| 24 | + |
| 25 | + $this->artisan('app:resend-lead-notifications', ['date' => '2025-03-15']) |
| 26 | + ->expectsOutputToContain('Found 2 lead(s)') |
| 27 | + ->expectsOutputToContain('Done.') |
| 28 | + ->assertExitCode(0); |
| 29 | + |
| 30 | + Notification::assertSentOnDemand( |
| 31 | + NewLeadSubmitted::class, |
| 32 | + function ($notification, $channels, $notifiable) use ($matchingLead) { |
| 33 | + return $notifiable->routes['mail'] === 'sales@nativephp.com' |
| 34 | + && $notification->lead->is($matchingLead); |
| 35 | + } |
| 36 | + ); |
| 37 | + |
| 38 | + Notification::assertSentOnDemand( |
| 39 | + NewLeadSubmitted::class, |
| 40 | + function ($notification, $channels, $notifiable) use ($newerLead) { |
| 41 | + return $notifiable->routes['mail'] === 'sales@nativephp.com' |
| 42 | + && $notification->lead->is($newerLead); |
| 43 | + } |
| 44 | + ); |
| 45 | + |
| 46 | + Notification::assertNotSentTo($oldLead, NewLeadSubmitted::class); |
| 47 | + } |
| 48 | + |
| 49 | + #[Test] |
| 50 | + public function it_shows_message_when_no_leads_are_found(): void |
| 51 | + { |
| 52 | + Notification::fake(); |
| 53 | + |
| 54 | + $this->artisan('app:resend-lead-notifications', ['date' => '2099-01-01']) |
| 55 | + ->expectsOutputToContain('No leads found') |
| 56 | + ->assertExitCode(0); |
| 57 | + |
| 58 | + Notification::assertNothingSent(); |
| 59 | + } |
| 60 | +} |
0 commit comments