Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel通知邮件无法在测试中工作_Php_Laravel_Testing - Fatal编程技术网

Php Laravel通知邮件无法在测试中工作

Php Laravel通知邮件无法在测试中工作,php,laravel,testing,Php,Laravel,Testing,我正在使用通知在触发通知时向所有用户发送邮件 这在手动“浏览网站”使用(向ContactController提交帖子)中效果很好,但在我的测试中,mail facade声称没有发送邮件 控制器: 发送通知电子邮件的驱动程序似乎没有使用邮件外观,这意味着邮件::假不会影响它。相反,它直接调用Mailable上的send方法,该方法依次调用Mailer(邮件驱动程序)上的send 您可以将Mailable实例替换为的实例(这是Mail::fake使用的),但它看起来似乎不适合mailfeak是数组的

我正在使用通知在触发通知时向所有用户发送邮件

这在手动“浏览网站”使用(向ContactController提交帖子)中效果很好,但在我的测试中,mail facade声称没有发送邮件

控制器:

发送通知电子邮件的驱动程序似乎没有使用
邮件
外观,这意味着
邮件::假
不会影响它。相反,它直接调用
Mailable
上的
send
方法,该方法依次调用
Mailer
(邮件驱动程序)上的
send

您可以将
Mailable
实例替换为的实例(这是
Mail::fake
使用的),但它看起来似乎不适合
mailfeak
是数组的情况(这是
MailChannel
传递给
Mailable
的内容)

幸运的是,Laravel源代码包含了一个示例,说明了他们如何测试发送邮件的通知。他们模拟
Mailer
Markdown
实例,并检查传递的参数。你可以做类似的事情,比如:

use Mockery as m;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Markdown;
use Illuminate\Mail\Message;

class ContactFormTest extends TestCase
{

    protected function tearDown(): void
    {
        parent::tearDown();
        m::close();
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->mailer = m::mock(Mailer::class);
        $this->markdown = m::mock(Markdown::class);
        $this->instance(Mailer::class, $this->mailer);
        $this->instance(Mailer::class, $this->markdown);
    }

    public function a_mail_is_send_when_the_contact_form_is_used()
    {
        $this->withExceptionHandling();

        $user = factory(User::class)->create();

        $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');

        $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');

        $data = [
            'name' => 'John Doe',
            'email' => 'email@email.com',
            'message' => 'This is a test message'
        ];

        $notification = new ContactRequestNotification($data);

        $this->mailer->shouldReceive('send')->once()->with(
            ['html' => 'htmlContent', 'text' => 'textContent'],
            array_merge($notification->toMail($user)->toArray(), [
                '__laravel_notification' => get_class($notification),
                '__laravel_notification_queued' => false,
            ]),
            m::on(function ($closure) {
                $message = m::mock(Message::class);
                $message->shouldReceive('to')->once()->with([$user->email]);
                $closure($message);
                return true;
            })
        );


        $response = $this->post('/contact', $data);

        $response->assertStatus(200);

    }
}

就个人而言,我现在更愿意在
ContactRequestNotification
类上对
toMail
方法进行单元测试,因为我认为上面的方法不是很好

use Mockery as m;
use Illuminate\Contracts\Mail\Mailable;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Mail\Markdown;
use Illuminate\Mail\Message;

class ContactFormTest extends TestCase
{

    protected function tearDown(): void
    {
        parent::tearDown();
        m::close();
    }

    protected function setUp(): void
    {
        parent::setUp();

        $this->mailer = m::mock(Mailer::class);
        $this->markdown = m::mock(Markdown::class);
        $this->instance(Mailer::class, $this->mailer);
        $this->instance(Mailer::class, $this->markdown);
    }

    public function a_mail_is_send_when_the_contact_form_is_used()
    {
        $this->withExceptionHandling();

        $user = factory(User::class)->create();

        $this->markdown->shouldReceive('render')->once()->andReturn('htmlContent');

        $this->markdown->shouldReceive('renderText')->once()->andReturn('textContent');

        $data = [
            'name' => 'John Doe',
            'email' => 'email@email.com',
            'message' => 'This is a test message'
        ];

        $notification = new ContactRequestNotification($data);

        $this->mailer->shouldReceive('send')->once()->with(
            ['html' => 'htmlContent', 'text' => 'textContent'],
            array_merge($notification->toMail($user)->toArray(), [
                '__laravel_notification' => get_class($notification),
                '__laravel_notification_queued' => false,
            ]),
            m::on(function ($closure) {
                $message = m::mock(Message::class);
                $message->shouldReceive('to')->once()->with([$user->email]);
                $closure($message);
                return true;
            })
        );


        $response = $this->post('/contact', $data);

        $response->assertStatus(200);

    }
}