Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Not working with Laravel 5.5.14? #81

Closed
ejunker opened this issue Oct 5, 2017 · 3 comments
Closed

Not working with Laravel 5.5.14? #81

ejunker opened this issue Oct 5, 2017 · 3 comments

Comments

@ejunker
Copy link

ejunker commented Oct 5, 2017

PHP 7.1.10
Laravel 5.5.14
PhpUnit 6.3.1

UPDATE: I do not see the issue if I downgrade to PHPUnit 5.7.22

I see in the commits that MailThief has been updated for Laravel 5.5, though when I have a fresh app with the following test, it fails.

<?php

namespace Tests\Feature;

use Illuminate\Support\Facades\Mail;
use MailThief\Testing\InteractsWithMail;
use Tests\TestCase;

class MailTest extends TestCase
{
    use InteractsWithMail;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testExample()
    {
        echo "\n".md5(spl_object_hash(\MailThief\Facades\MailThief::getFacadeRoot()))."\n";

        Mail::send('welcome', [], function ($m) {
            $m->to('[email protected]');
            $m->subject('Welcome to my app!');
        });

        $this->seeMessageFor('[email protected]');
        $this->assertNotEmpty($this->getMessages());
    }
}

I added debug output to InteractsWithMail::hijackMail()

    /** @before */
    public function hijackMail()
    {
        if (method_exists($this, 'afterApplicationCreated')) {
            $this->afterApplicationCreated(function () {
                $this->getMailer()->hijack();
                echo "\n".md5(spl_object_hash(\MailThief\Facades\MailThief::getFacadeRoot()))."\n";
            });
        } else {
            $this->getMailer()->hijack();
        }
    }

If you run the test above with the modified hijackMail() you will see that it is returning different instances and isn't a singleton like intended.

I did a bit of debugging and it looks like a possible issue with getMailer(), $this->mailer is never set so it always calls MailThief::getFacadeRoot() which will create a new instance if one is not already resolved and return it, though it seems like the instance that is resolved in hijackMail() is different that the instance you get when you call your assertions. This might be a Laravel bug but I thought I'd report it here first.

I did find that if I modify InteractsWithMail::getMailer to be the following that it works fine because it memoizes the mailer and prevents future calls to MailThief::getFacadeRoot()

    private function getMailer()
    {
        return tap($this->mailer ?: MailThief::getFacadeRoot(), function($mailer){
            $this->setMailer($mailer);
        });
    }

Is anybody else seeing this issue? It happens with a fresh Laravel app so I hope it isn't just an issue with my environment.

@rdok
Copy link

rdok commented Oct 9, 2017

@ejunker I can confirm this issue. On a existing upgraded Laravel 5.5. project.

And thanks so much for the getMailer solution.

You should probably submit a pull request :)

mattstauffer added a commit that referenced this issue Oct 13, 2017
Fixes #81

Fix provided by @ejunker <3
@mattstauffer
Copy link
Member

@ejunker @rdok thanks!

@ejunker
Copy link
Author

ejunker commented Oct 16, 2017

Thanks @mattstauffer for adding the fix. I'm still curious as to why it broke in the first place. Strangely, the issue did not exist when using PHPUnit 5.* instead of 6.* and the root cause might actually be related to PHPUnit or maybe even Laravel.

FatBoyXPC added a commit to FatBoyXPC/mailthief that referenced this issue Nov 4, 2017
FatBoyXPC added a commit to FatBoyXPC/mailthief that referenced this issue Nov 4, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants