This functionality is redily available in later releases of Laravel.
MailGrasp is a package for Laravel applications (5.1+) to add support for email testing in your test classes.
Author: Simone Todaro
Made with ❤️ by Cyber-Duck Ltd
composer require cyber-duck/mailgrasp --dev
Add the InteractsWithEmails
to your test class. That's it!
use \Cyberduck\MailGrasp\Testing\InteractsWithEmails;
The custom mailer will be initialised as soon as the visit() method is called.
It checks if exactly $count
emails have been sent or enqueued.
$this->visit('/route/which/sends/2/emails')
->seeEmails(2);
It checks if exactly $count
emails have been enqueued.
$this->visit('/route/which/enqueues/2/emails')
->seeEmailsInQueue(2);
It checks that no email has been sent or enqueued.
$this->visit('/route/with/no/emails')
->dontSeeEmails();
// OR
$this->visit('/route/with/no/emails')
->notSeeEmails();
It checks that no email has been enqueued.
$this->visit('/route/with/no/emails')
->dontSeeEmailsInQueue();
// OR
$this->visit('/route/with/no/emails')
->notSeeEmailsInQueue();
It checks that an email matching given critaria has been sent or enqueued.
$this->visit('/route/which/sends/emails')
->seeEmail(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
});
// OR
$this->visit('/route/which/sends/emails')
->seeEmail($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
});
Complete opposite of seeEmail.
$this->visit('/route/which/sends/emails')
->dontSeeEmail(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
});
// OR
$this->visit('/route/which/sends/emails')
->dontSeeEmail($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
});
It checks that an email matching given critaria has been enqueued.
$this->visit('/route/which/enqueues/emails')
->seeEmailInQueue(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
});
// OR
$this->visit('/route/which/enqueues/emails')
->seeEmailInQueue($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
});
It checks that an email matching the given critaria contains the given string.
$this->visit('/route/which/sends/emails')
->seeInEmail(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
}, 'Lorem ipsum dolor sit amet');
// OR
$this->visit('/route/which/sends/emails')
->seeInEmail($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
}, 'Lorem ipsum dolor sit amet);
Visit the page in the email link. Useful to test activation links.
$this->visit('/route/which/enqueues/emails')
->clickInEmail(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
});
// OR
$this->visit('/route/which/enqueues/emails')
->clickInEmail($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
});
If there is more than one link in the email, it's possible to select the link passing a css selector as second parameter.
$this->visit('/route/which/enqueues/emails')
->clickInEmail(function($m) {
$m->from('from@test.com');
$m->to('to@test.com');
$m->subject('Subject');
}, 'a.activation-link');
// OR
$this->visit('/route/which/enqueues/emails')
->clickInEmail($this->message()
->from('from@test.com')
->to('to@test.com')
->subject('Subject');
}, 'a.activation-link');