如何获得使用array邮件驱动程序发送的消息? [英] How to get messages sent with the `array` mail driver?

查看:75
本文介绍了如何获得使用array邮件驱动程序发送的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从5.7版开始,Laravel建议在测试过程中使用array驱动程序进行邮件:

Starting from version 5.7 Laravel suggests to use the array driver for Mail during testing:

不幸的是,文档对该驱动程序一无所知.根据源代码,驱动程序将所有消息存储在内存中,而没有实际发送它们.在单元测试期间如何获取已存储的已发送"消息(以便对其进行检查)?

Unfortunately, the documentation tells nothing about this driver. According to the source code, the driver stores all the messages in memory without actually sending them. How to get the stored "sent" messages during unit testing (in order to check them)?

推荐答案

调用app()->make('swift.transport')->driver()->messages().返回值是Swift_Mime_SimpleMessage个对象的集合.

Call app()->make('swift.transport')->driver()->messages(). The return value is a collection of Swift_Mime_SimpleMessage objects.

完整的PHPUnit测试示例:

An example of a full PHPUnit test:

public function testEmail()
{
    Mail::to('user@example.com')->send(new MyMail);

    $emails = app()->make('swift.transport')->driver()->messages();
    $this->assertCount(1, $emails);
    $this->assertEquals(['user@example.com'], array_keys($emails[0]->getTo()));
}

这篇关于如何获得使用array邮件驱动程序发送的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆