【发布时间】:2019-06-23 14:39:38
【问题描述】:
我正在编写测试来验证是否为通知设置了区域设置,并断言通知内容中使用了正确的翻译。使用 Twilio 时,我无法在通知上设置区域设置:https://github.com/laravel-notification-channels/twilio
我有一个使用Mailable 的邮件通道的通知测试,它按预期工作:
public function test_mail_notifications_use_localisation()
{
$user = factory(User::class)->create();
$user->notify(
(new WelcomeMailNotificationStub)
->locale('fr')
);
$this->assertContains('Bonjour Le Monde',
app('swift.transport')->messages()[0]->getBody()
);
}
class WelcomeMailStub extends \Illuminate\Mail\Mailable
{
public function build()
{
return $this->view('welcome-notification');
}
}
class WelcomeMailNotificationStub extends \Illuminate\Notifications\Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new WelcomeMailStub);
}
}
我想为通过 Twilio 发送的 SMS 通知编写一个类似的测试,但到目前为止,在我的尝试中,发送通知时似乎忽略了区域设置,而是使用了默认区域设置。这是我想出的:
public function test_sms_notifications_use_localisation()
{
Notification::fake();
$user = factory(User::class)->create();
$user->notify(
(new WelcomeSmsNotificationStub)
->locale('fr')
);
Notification::assertSentTo(
$user,
WelcomeSmsNotificationStub::class,
function ($notification, $channels) use ($user)
{
return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
}
);
}
class WelcomeSmsNotificationStub extends \Illuminate\Notifications\Notification
{
public function via($notifiable)
{
return [TwilioChannel::class];
}
public function toTwilio($notifiable)
{
return (new TwilioSmsMessage())
->content(__('welcome_notification.opening_text'));
}
}
如果我在assertSentTo 的回调中dd() 像这样:
Notification::assertSentTo(
$user,
WelcomeSmsNotificationStub::class,
function ($notification, $channels) use ($user)
{
dd(
$notification,
$notification->toTwilio($user)
);
return $notification->toTwilio($user)->content === 'Bonjour Le Monde'; // test fails here
}
);
我得到以下信息:
Tests\Unit\Notifications\WelcomeSmsNotificationStub {#116
+id: "ae164ce6-fa47-4730-b401-e6cc15b27e16"
+locale: "fr"
}
NotificationChannels\Twilio\TwilioSmsMessage {#2414
+alphaNumSender: null
+applicationSid: null
+maxPrice: null
+provideFeedback: null
+validityPeriod: null
+content: "Default welcome" <== using the default welcome instead of 'fr'
+from: null
+statusCallback: null
+statusCallbackMethod: null
}
任何关于如何使这项工作的建议将不胜感激,谢谢!
【问题讨论】: