Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.7] NotificationFake can assert preferred locale #26205

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}