alphametric / mail-assertions
A package to make common mail assertions directly available within the Laravel test case.
Installs: 1 108
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 0
Forks: 0
Open Issues: 0
Requires
- php: ^7.0
Requires (Dev)
- orchestra/testbench: ^3.6
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2020-02-17 10:07:55 UTC
README
This package adds a set of email-based assertions to $this
within your Laravel TestCase. By using this package, you no longer need to import the Mail
facade and configure it to use a fake implementation, nor do you have to import your mailables / notifications. Instead, you can simply chain whatever assertions you need without any setup.
Installation
You can install the package via composer:
composer require alphametric/mail-assertions
Once the package is installed, you'll need to add the MailAssertions
trait to the Laravel TestCase
class:
namespace Tests; use Alphametric\Assertions\MailAssertions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { use CreatesApplication, MailAssertions; }
Configuration
If you haven't already done so, you should configure your application's phpunit.xml
file to use the Mail log driver:
<php> <env name="MAIL_DRIVER" value="log"/> </php>
Other mail drivers may function correctly, but are unsupported and have not received testing.
Usage
As you would expect, you can call the included assertions directly:
$this -> assertEmailSent();
Since all of the assertions return the class instance, you can also chain multiple assertions:
$this -> assertEmailSent() -> assertEmailFrom("john@example.com");
Library
The following assertions are available to use:
Name | Description |
---|---|
assertEmailAttachment | Verify whether the email has an attachment |
assertEmailAttachmentCount | Verify whether the email has the given number of attachments |
assertEmailAttachmentExtension | Verify whether the email has an attachment with the given file extension |
assertEmailAttachmentFilename | Verify whether the email has an attachment with the given file name |
assertEmailAttachmentSize | Verify whether the email has an attachment with the given file size (optionally uses multi-byte encoding) |
assertEmailBcc | Verify whether the email's BCC field matches a given string |
assertEmailBodyContains | Verify whether the email's body contains a given string |
assertEmailBodyDoesNotContain | Verify whether the email's body does not contain a given string |
assertEmailBodyEquals | Verify whether the email's body matches a given string |
assertEmailCc | Verify whether the email's CC field matches a given string |
assertEmailContentType | Verify whether the email uses the specified content type |
assertEmailCount | Verify whether the number of emails sent matches a given integer |
assertEmailFrom | Verify whether the email's From field matches a given string |
assertEmailPriority | Verify whether the email's has the given priority |
assertEmailNotSent | Verify whether an email has not been sent |
assertEmailReplyTo | Verify whether the email's ReplyTo field matches a given string |
assertEmailSent | Verify whether an email has been sent |
assertEmailSubjectContains | Verify whether the email's subject contains a given string |
assertEmailSubjectDoesNotContain | Verify whether the email's subject does not contain a given string |
assertEmailSubjectEquals | Verify whether the email's subject matches a given string |
assertEmailTo | Verify whether the email's To field matches a given string |
Clearing The Stack
Since it is possible for a test to result in multiple emails being sent, the package uses the concept of a stack (really just an array) to store all of the outgoing emails.
Sometimes, it may be necessary to perform one or more assertions, then empty the stack, before proceeding to execute additional code, which will result in new emails being sent / needing to be tested.
For these scenarios, you can call the flushEmail()
method.
Specific Emails
Sometimes, it may be necessary to perform one or more assertions on a particular email in the
stack. You can do this by calling the getEmailByIndex()
method and supplying the result as the second parameter to any assertion method:
$this -> assertEmailFrom("john@example.com", $this -> getEmailByIndex(3));
If you do not supply a second parameter, the assertions will instead operate on the most recent email in the stack.
Testing
You can run the test suite by using the following command within the root package directory:
composer test
Changelog
V1.0 - Initial release.
License
The MIT License (MIT). Please see License File for more information.