October: Mail::fake() not working as expected

Created on 14 Feb 2020  路  14Comments  路  Source: octobercms/october

  • OctoberCMS Build: v1.0.464
  • PHP Version: 7.4.2
  • Database Engine: MYSQL 5.7 InnoDB
  • Plugins Installed: Rainlab.User

Description:

I am trying to test some functionality related to mails in my plugin. But the Mail::fake() does not work as expected as mentioned here .

My testCase

    public function testEmployerApplicationsNotification()
    {
        $user = factory(User::class)->states('activated')->create();
        Auth::login($user);

        $company = factory(Company::class)->create();
        $category = factory(Category::class)->create();

        $job = Job::create([
            'title' => 'Senior Software Engineer',
            'description' => 'Manage our big php applications',
            'positions' => 10,
            'company_id' => $company->id,
            'category_id' => $category->id
        ]);

        Mail::fake();

        $job->apply();

        Mail::assertSent('aniketmagadum.jobmanager::mail.candidate_applied', 1);
    }

The apply method where I have sent the mail.

    public function apply()
    {
        $user = Auth::getUser();
        $this->applicants()->attach($user->id);
        Mail::send('aniketmagadum.jobmanager::mail.candidate_applied', [], function ($message) {
            $message->to('[email protected]', 'Admin Person');
            $message->subject('This is a reminder');
        });
    }

I always get a error saying
The expected ['aniketmagadum.jobmanager::mail.candidate_applied'] mailable was not sent.

But the email gets logged correctly If I use the Mail::pretend();

Steps To Reproduce:

I will attach a PR to the test plugin soon

Completed Bug

All 14 comments

I also encoutered this bug. October overrides how mails are send, but do not change the pretend method accordingly. I started building a working MailFaker, which I have never really finished: https://gist.github.com/alxy/6e74d306be7bd0c9d1f134f34d547a44

here is how you would use a custom Mail faker:

public function testNewFakeMailer()
{
    Mail::swap(new MailFake());

    // These variables are available inside the message as Twig
    $vars = ['name' => 'Joe', 'user' => 'Mary'];

    Mail::send('backend::mail.invite', $vars, function($message) {
        $message->to('[email protected]', 'Admin Person');
        $message->subject('This is a reminder');
    });

    Mail::assertSent('backend::mail.invite', function (Mailable $mailable) use ($vars) {
        return $mailable->viewData['name'] === 'Joe' &&
            $mailable->viewData['user'] === 'Mary' &&
            $mailable->hasTo('[email protected]');
    });
}

@alxy Would you be interested in completed this work? From what I can glance, you were very close.

This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

This issue will be closed and archived in 3 days, as there has been no activity in the last 30 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@4nik3t @alxy is this still an issue with the Laravel 6 update in develop?

@bennothommo I will check and get back on this

@bennothommo the problem is still there.

Thanks for letting us know @4nik3t.

This issue will be closed and archived in 3 days, as there has been no activity in the last 60 days.
If this issue is still relevant or you would like to see it actioned, please respond and we will re-open this issue.
If this issue is critical to your business, consider joining the Premium Support Program where a Service Level Agreement is offered.

@LukeTowers or @bennothommo Can you reopen this, I'm planning on addressing this in the coming weeks.

@mjauvin this might be fixed by adding Mockery to the October CMS composer.json, as we have done for the library recently, and ensuring Mail::fake() is using that.

@bennothommo I still want to investigate why it works in Laravel and not in october. I might end up using mockery, depending on what I find.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings