Skip to content

Commit

Permalink
NotificationFake can assert preferred locale (#26205)
Browse files Browse the repository at this point in the history
When a notifiable implements the HasLocalePreference
interface, make the NotificationFake store the locale
for assertions.

Add a similar feature test for MailFake that already
supports the same assertion capabilities.
  • Loading branch information
derekmd authored and taylorotwell committed Oct 22, 2018
1 parent 6debff6 commit 2aa4c6e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Illuminate/Support/Testing/Fakes/NotificationFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Str;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert as PHPUnit;
use Illuminate\Contracts\Translation\HasLocalePreference;
use Illuminate\Contracts\Notifications\Factory as NotificationFactory;
use Illuminate\Contracts\Notifications\Dispatcher as NotificationDispatcher;

Expand Down Expand Up @@ -210,7 +211,11 @@ public function sendNow($notifiables, $notification)
'notification' => $notification,
'channels' => $notification->via($notifiable),
'notifiable' => $notifiable,
'locale' => $notification->locale ?? $this->locale,
'locale' => $notification->locale ?? $this->locale ?? value(function () use ($notifiable) {
if ($notifiable instanceof HasLocalePreference) {
return $notifiable->preferredLocale();
}
}),
];
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/Support/SupportTestingMailFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Illuminate\Support\Testing\Fakes\MailFake;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\Constraint\ExceptionMessage;
use Illuminate\Contracts\Translation\HasLocalePreference;

class SupportTestingMailFakeTest extends TestCase
{
Expand Down Expand Up @@ -42,6 +43,17 @@ public function testAssertSent()
$this->fake->assertSent(MailableStub::class);
}

public function testAssertSentWhenRecipientHasPreferredLocale()
{
$user = new LocalizedRecipientStub;

$this->fake->to($user)->send($this->mailable);

$this->fake->assertSent(MailableStub::class, function ($mail) use ($user) {
return $mail->hasTo($user) && $mail->locale === 'au';
});
}

public function testAssertNotSent()
{
$this->fake->assertNotSent(MailableStub::class);
Expand Down Expand Up @@ -164,3 +176,13 @@ public function build()
->withLastName('Otwell');
}
}

class LocalizedRecipientStub implements HasLocalePreference
{
public $email = '[email protected]';

public function preferredLocale()
{
return 'au';
}
}
20 changes: 20 additions & 0 deletions tests/Support/SupportTestingNotificationFakeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\Constraint\ExceptionMessage;
use Illuminate\Support\Testing\Fakes\NotificationFake;
use Illuminate\Contracts\Translation\HasLocalePreference;

class SupportTestingNotificationFakeTest extends TestCase
{
Expand Down Expand Up @@ -94,6 +95,17 @@ public function testAssertTimesSent()

$this->fake->assertTimesSent(3, NotificationStub::class);
}

public function testAssertSentToWhenNotifiableHasPreferredLocale()
{
$user = new LocalizedUserStub;

$this->fake->send($user, new NotificationStub);

$this->fake->assertSentTo($user, NotificationStub::class, function ($notification, $channels, $notifiable, $locale) use ($user) {
return $notifiable === $user && $locale === 'au';
});
}
}

class NotificationStub extends Notification
Expand All @@ -108,3 +120,11 @@ class UserStub extends User
{
//
}

class LocalizedUserStub extends User implements HasLocalePreference
{
public function preferredLocale()
{
return 'au';
}
}

0 comments on commit 2aa4c6e

Please sign in to comment.