Evaldas Butkus
Evaldas Butkus

Reputation: 665

Phpunit mail testing. Can't find efficient way

For our project we are using custom package which is used for emails. This package contains structure like this:

class Mail 
{
    public function send($data, $view)
    {
        //some logic here

        return with(new Mailgun($this->key))->sendMessage($data);
    }
}

class Message extends Mail 
{

    public function passwordReset($data, $view)
    {
        return $this->send($data, 'viewFile1');
    }

    public function activate()
    {
        return $this->send($data, 'viewFile2');
    }

}

Before my tests used to look like this:

public function testPasswordReset()
{
    $data = new User(5);
    $rv = with(new Message)->activate($data, 'view');

    $this->assertEqual($rv, 200);
}

But since we decided that emails should not send any real data I got lost.

I've watched few tutorials how these services like MailCatcher and I got idea behind that, but:

How do I test logic behind my package, when result always is http status?

How do I test non-smtp mail service in package?

Please let me know if you need any additional information

Upvotes: 1

Views: 274

Answers (2)

Evaldas Butkus
Evaldas Butkus

Reputation: 665

I've set Mail driver to file in phpunit configuration file. Then I could fetch result email values and compare.

Upvotes: 0

MMMTroy
MMMTroy

Reputation: 1267

I find mailcatcher to be the best solution. You can set custom env variables for your testing purposes only by editing phpunit.xml. You should see the following lines.

    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
    </php>

You should be able to add any additional env variables for your mailcatcher configuration, or any other configuration you'd like to test.

Upvotes: 1

Related Questions