spinen / laravel-mail-assertions
PHPUnit mail assertions for testing email in Laravel.
Installs: 108 293
Dependents: 0
Suggesters: 0
Security: 0
Stars: 32
Watchers: 9
Forks: 18
Open Issues: 7
Requires
- php: >=8.1
- illuminate/container: ~9|~10
- illuminate/mail: ~9|~10
- phpunit/phpunit: ^9.0
- swiftmailer/swiftmailer: ~6.2
Requires (Dev)
- egulias/email-validator: ^2.1.16
- laravel/pint: ^1.6
- mockery/mockery: ^1.5.1
- psy/psysh: ^0.11.1
- symfony/var-dumper: ^6.2
README
NOTE: This is based off a video titled "Testing Email With Custom Assertions" that Jeffrey Way did on Laracasts.com. If you do not have an account on that site, then you should make one. It is an amazing resource. We have just taken that example & made it an easy-to-install package. Thanks Jeffrey!
PHPUnit mail assertions for testing email in Laravel.
Build Status
Branch | Status | Coverage | Code Quality |
---|---|---|---|
Develop | |||
Master |
Installation
Install the package inside your dev dependencies:
$ composer require spinen/laravel-mail-assertions --dev
Configuration
In order for the package to be able to make assertions on your emails, it has to be able to "read" the messages. It does so by parsing the Laravel log, so your mail driver has to be "log" for this package to function.
Usage
You mixin the assertions with the Spinen\MailAssertions\MailTracking
trait. You get the following assertions...
- seeEmailBcc
- seeEmailCc
- seeEmailContains
- seeEmailContentTypeEquals
- seeEmailCountEquals
- seeEmailDoesNotContain
- seeEmailEquals
- seeEmailFrom
- seeEmailPriorityEquals
- seeEmailReplyTo
- seeEmailSubjectContains
- seeEmailSubjectDoesNotContain
- seeEmailSubjectEquals
- seeEmailTo
- seeEmailWasNotSent
- seeEmailWasSent
NOTE: If there was more than 1 email sent, then the assertions look at the last email.
Example
<?php use Illuminate\Foundation\Testing\WithoutMiddleware; use Illuminate\Foundation\Testing\DatabaseMigrations; use Illuminate\Foundation\Testing\DatabaseTransactions; use Spinen\MailAssertions\MailTracking; class ExampleTest extends TestCase { use MailTracking; /** * A basic functional test example. * * @return void */ public function testBasicExample() { $this->visit('/route-that-sends-an-email') ->seeEmailWasSent() ->seeEmailSubject('Hello World') ->seeEmailTo('foo@bar.com') ->seeEmailEquals('Click here to buy this jewelry.') ->seeEmailContains('Click here'); } }