声明传递给邮件降价视图的数据的Laravel通知

声明传递给邮件降价视图的数据的Laravel通知,laravel,integration-testing,laravel-testing,laravel-notification,Laravel,Integration Testing,Laravel Testing,Laravel Notification,我正在做一个拉威尔的项目。我正在为我的应用程序编写集成/功能测试。我现在正在编写一个测试,需要断言传递给电子邮件通知的数据和传递给其视图的数据。我找到了这个链接 这是我的通知类 class NotifyAdminForHelpCenterCreated extends Notification { use Queueable; private $helpCenter; public function __construct(HelpCenter $helpCenter

我正在做一个拉威尔的项目。我正在为我的应用程序编写集成/功能测试。我现在正在编写一个测试,需要断言传递给电子邮件通知的数据和传递给其视图的数据。我找到了这个链接

这是我的通知类

class NotifyAdminForHelpCenterCreated extends Notification
{
    use Queueable;

    private $helpCenter;

    public function __construct(HelpCenter $helpCenter)
    {
        $this->helpCenter = $helpCenter;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage())
            ->subject("Help Center registration")
            ->markdown('mail.admin.helpcenter.created-admin', [
                'helpCenter' => $this->helpCenter,
                'user' => $notifiable
            ]);
    }
}
正如您在代码中看到的,我正在将数据传递到mail.admin.helpcenter.created-admin刀片视图

这是我的测试方法

/** @test */
public function myTest()
{
    $body = $this->requestBody();
    $this->actingAsSuperAdmin()
        ->post(route('admin.help-center.store'), $body)
        ->assertRedirect();

    $admin = User::where('email', $body['admin_email'])->first();
    $helpCenter = HelpCenter::first();

    Notification::assertSentTo(
        $admin,
        NotifyAdminForHelpCenterCreated::class,
        function ($notification, $channels) use ($admin, $helpCenter) {
            $mailData = $notification->toMail($admin)->toArray();
            //here I can do some assertions with the $mailData
            return true;
        }
    );
}
正如您在测试中看到的,我可以使用$mailData变量进行一些断言。但这不包括传递给视图的数据。如何断言或获取传递给刀片视图/模板的数据或变量?

如您所见,
MailMessage
类上有一个
viewData
属性,它包含传递给视图的所有数据,无需将通知转换为数组

$notification->toMail($admin)->viewData
所以在你的情况下是这样的:

/** @test */
public function myTest()
{
    $body = $this->requestBody();
    $this->actingAsSuperAdmin()
        ->post(route('admin.help-center.store'), $body)
        ->assertRedirect();

    $admin = User::where('email', $body['admin_email'])->first();
    $helpCenter = HelpCenter::first();

    Notification::assertSentTo(
        $admin,
        NotifyAdminForHelpCenterCreated::class,
        function ($notification, $channels) use ($admin, $helpCenter) {
            $viewData = $notification->toMail($admin)->viewData;

            return $admin->is($viewData['user']) && $helpCenter->is($viewData['helpCenter']);
        }
    );
}

谢谢这正是我要找的,