-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to test email configuration (#1618)
* Add command to test email config * Add return statements * Refactor and add tests * Test mail content * Add to docs, update changelog
- Loading branch information
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Notifications\TestEmail; | ||
use Illuminate\Console\Command; | ||
use Notification; | ||
|
||
use function Laravel\Prompts\text; | ||
|
||
class TestEmailConfig extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'mail:test'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Send a test email to validate config'; | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
// Ask for the email address to send the test email | ||
$email = text(label: 'Recipient email address', validate: ['email' => 'required|email']); | ||
|
||
// Send the test email | ||
try { | ||
Notification::route('mail', [$email])->notifyNow(new TestEmail); | ||
|
||
$this->info('Test email sent successfully'); | ||
|
||
return self::SUCCESS; | ||
} catch (\Exception $e) { | ||
$this->error('Failed to send test email: '.$e->getMessage()); | ||
|
||
return self::FAILURE; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Notifications; | ||
|
||
use Illuminate\Notifications\Messages\MailMessage; | ||
use Illuminate\Notifications\Notification; | ||
|
||
class TestEmail extends Notification | ||
{ | ||
/** | ||
* Create a new notification instance. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the notification's delivery channels. | ||
* | ||
* @return array<int, string> | ||
*/ | ||
public function via(object $notifiable): array | ||
{ | ||
return ['mail']; | ||
} | ||
|
||
/** | ||
* Get the mail representation of the notification. | ||
*/ | ||
public function toMail(object $notifiable): MailMessage | ||
{ | ||
return (new MailMessage) | ||
->line('This is a test email to check the email configuration.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Tests\Backend\Unit\Console; | ||
|
||
use App\Notifications\TestEmail; | ||
use Illuminate\Support\Facades\Notification; | ||
use Tests\Backend\TestCase; | ||
|
||
class TestEmailConfigTest extends TestCase | ||
{ | ||
/** | ||
* Test the command sends an email successfully. | ||
* | ||
* @return void | ||
*/ | ||
public function test_handle_success() | ||
{ | ||
Notification::fake(); | ||
|
||
$this->artisan('mail:test') | ||
->expectsQuestion('Recipient email address', '[email protected]') | ||
->expectsOutput('Test email sent successfully') | ||
->assertSuccessful(); | ||
|
||
Notification::assertSentOnDemand( | ||
TestEmail::class, | ||
function (TestEmail $notification, array $channels, object $notifiable) { | ||
// Check the email content | ||
$mail = $notification->toMail($notifiable)->toArray(); | ||
$this->assertEquals('This is a test email to check the email configuration.', $mail['introLines'][0]); | ||
|
||
// Check the email recipient | ||
$this->assertEquals(['[email protected]'], $notifiable->routes['mail']); | ||
|
||
return true; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Test the command validates the email address. | ||
*/ | ||
public function test_handle_validation() | ||
{ | ||
Notification::fake(); | ||
|
||
$this->artisan('mail:test') | ||
->expectsQuestion('Recipient email address', 'invalid-email') | ||
->assertFailed(); | ||
} | ||
|
||
/** | ||
* Test the command fails to send an email. | ||
* | ||
* @return void | ||
*/ | ||
public function test_handle_failure() | ||
{ | ||
Notification::fake(); | ||
|
||
// Simulate an exception when sending the email | ||
Notification::shouldReceive('sendNow') | ||
->andThrow(new \Exception('SMTP server not found')); | ||
|
||
$this->artisan('mail:test') | ||
->expectsQuestion('Recipient email address', '[email protected]') | ||
->expectsOutput('Failed to send test email: SMTP server not found') | ||
->assertFailed(); | ||
} | ||
} |