Skip to content

Commit

Permalink
Swap Mail facade (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogervila authored Jun 20, 2024
1 parent 92f59c1 commit 7c82432
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ Trigger email failures to assert what happens on your Laravel Application when a

## Usage

Once the `mailer` instance is replaced, all emails will fail. This helps to assert that your application Mail exceptions are handled correctly (ie: mark the email address as invalid)
Once MailFailer instance is binded, all emails will fail. This helps to assert that your application Mail exceptions are handled correctly (ie: mark the email address as invalid)

```php
class MyService
{
public static function sendEmail()
{
\Illuminate\Support\Facades\Mail::send(...);
}
}

public function test_happy_path()
{
Mail::fake();
Expand All @@ -34,8 +42,9 @@ public function test_happy_path()

public function test_email_failures()
{
$mailer = new \LaravelEmailFailer\MailFailer;
$this->app->instance('mailer', $mailer);
$this->expectException(TransportException::class);

\LaravelEmailFailer\MailFailer::bind();

MyService::sendEmail();

Expand Down
8 changes: 8 additions & 0 deletions src/MailFailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Mail\MailManager;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Testing\Fakes\MailFake;
use Symfony\Component\Mailer\Exception\TransportException;
use Throwable;
Expand Down Expand Up @@ -58,4 +59,11 @@ public function failures()
{
return $this->failedRecipients;
}

public static function bind(?Application $app = null): self
{
Mail::swap($instance = new self($app));

return $instance;
}
}
27 changes: 21 additions & 6 deletions tests/MailFailureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Tests;

use Illuminate\Support\Facades\Mail;
use LaravelEmailFailer\MailFailer;
use Symfony\Component\Mailer\Exception\TransportException;

class FakeMailable extends \Illuminate\Mail\Mailable
Expand All @@ -14,10 +16,10 @@ class MailFailureTest extends \Orchestra\Testbench\TestCase
public function testMailFails()
{
$this->expectException(TransportException::class);
$this->expectExceptionMessage('Connection could not be established with host "1.2.3.4:1234": stream_socket_client(): Unable to connect to 1.2.3.4:1234 (Connection refused)');

$address = '[email protected]';
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);
$mailer = MailFailer::bind();

$mailable = new FakeMailable;
$mailable->subject('test')->to($address);
Expand All @@ -29,8 +31,7 @@ public function testMailFailuresContainsAddress()
{
try {
$address = '[email protected]';
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);
$mailer = MailFailer::bind();

$mailable = new FakeMailable;
$mailable->subject('test')->to($address);
Expand All @@ -45,8 +46,7 @@ public function testMailFailuresContainsMultipleAddresses()
{
try {
$addresses = ['[email protected]', '[email protected]'];
$mailer = new \LaravelEmailFailer\MailFailer();
$this->app->instance('mailer', $mailer);
$mailer = MailFailer::bind();

$mailable = new FakeMailable;
$mailable->subject('test')->to($addresses);
Expand All @@ -56,4 +56,19 @@ public function testMailFailuresContainsMultipleAddresses()
$this->assertCount(0, array_diff($addresses, $mailer->failures()));
}
}

public function testFacadeSwap()
{
MailFailer::bind();

$addresses = ['[email protected]', '[email protected]'];
$mailable = new FakeMailable;
$mailable->subject('test')->to($addresses);

try {
Mail::send($mailable);
} catch (TransportException) {
$this->assertCount(0, array_diff($addresses, Mail::failures()));
}
}
}

0 comments on commit 7c82432

Please sign in to comment.