Core\Test::sentMail
Core\Test::sentMail(): array<Core\Test\SentMail>Core\Test::sentMail() returns every email your program sent during the current test. Under nvs test, Core\Mail::send sends nothing and opens no connection, so a test never sends mail to a real person.
In a test, Core\Mail::send still checks three things: the mail.send capability, the mail server block [mail.<name>] in nvs.toml, and every address. If a check fails, send throws the same error as in production. If all checks pass, the message is kept, and sentMail() returns it. The list includes the mail of the test, of its Core\Test::request requests and of its Core\Test::run runs. The messages are in the order sent. Each test starts with an empty list.
Each item is a Core\Test\SentMail. Its methods return each part of the message, such as to(), subject(), text(), html(), messageId() and attachments().
Outside nvs test, sentMail() returns an empty list.
The examples below show a welcome mail after sign-up, a password reset mail that a request sends, and a contact form that sends no mail when it has an error.
Return value
Returns array<Core\Test\SentMail>. One Core\Test\SentMail for each message, in the order sent. The list is empty when nothing was sent.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Test::sentMail returns the messages your program sent in the current test. Each one is a Core\Test\SentMail, and its methods return the values given to Core\Mail::send. |
Examples
The examples below run with this configuration file.
# The mail server for the examples. Nothing listens on port 1, so a real send# would fail. Under `nvs test`, no message is sent and no connection is opened.[mail.site]host = "127.0.0.1"port = 1from = "hello@example.com"timeout = "1s"
# The program may send mail through the `site` block.[capabilities.mail]send = ["site"]A welcome mail after sign up
<?nvs// Shows how to test that a new account gets a welcome mail.// proof: test
use Core\Mail;use Core\Test;use Core\Test\SentMail;
class SignUp{ // Creates the account and sends one welcome mail through the `site` block. public static function register(string $email, string $name): void { Mail::send('site', [$email], 'Welcome, ' . $name, 'Hello ' . $name . ', your account is ready.'); }}
class SignUpTest{ #[Test] public function itSendsAWelcomeMail(): void { SignUp::register('ada@example.com', 'Ada');
// `sentMail()` returns one item for each message. Here it has one item. array<SentMail> $sent = Test::sentMail(); Test::assertCount($sent, 1); Test::assertEquals($sent[0]->to(), ['ada@example.com']); Test::assertEquals($sent[0]->subject(), 'Welcome, Ada'); Test::assertEquals($sent[0]->text(), 'Hello Ada, your account is ready.'); }
// Each test starts with an empty list, so the mail of the test above is not here. #[Test] public function eachTestStartsWithNoMail(): void { Test::assertCount(Test::sentMail(), 0); }}
// `nvs test` runs the tests above. Outside a test, `sentMail()` returns an// empty list, so `nvs run` prints `0`.echo Core\Arr::count(Test::sentMail()), "\n"; SignUpTest ✓ itSendsAWelcomeMail ✓ eachTestStartsWithNoMail
0 failed, 2 passed, 0 skipped, 0 flakyA password reset link
<?nvs// Shows how to test the password reset mail that a request sends.// proof: test
use Core\Http\Method;use Core\Mail;use Core\Str;use Core\Test;use Core\Test\SentMail;
class PasswordResetTest{ #[Test] public function theMailHasAResetLink(): void { var $response = Test::request(Method::Post, '/password/reset', { headers: ['content-type' => 'application/x-www-form-urlencoded'], body: 'user=ada', }); Test::assertEquals($response->status(), 202);
// The request sent the mail, and the test reads it. array<SentMail> $sent = Test::sentMail(); Test::assertCount($sent, 1); Test::assertEquals($sent[0]->to(), ['ada@example.com']);
// The link ends with a token of 32 characters. ?string $token = Str::after($sent[0]->text(), 'https://example.com/password/new?token='); Test::assertEquals(Str::length($token ?? ''), 32); }
// A user name that does not exist gets the same status code, `202`. No mail is sent. #[Test] public function anUnknownUserGetsNoMail(): void { var $response = Test::request(Method::Post, '/password/reset', { headers: ['content-type' => 'application/x-www-form-urlencoded'], body: 'user=nobody', }); Test::assertEquals($response->status(), 202); Test::assertCount(Test::sentMail(), 0); }}
// `Core\Test::request` runs this file again for the request. This part// finds the address of the user and sends the mail with a new random token.if (Core\Env::context() == Core\Env::Context::Request) { array<string> $addresses = ['ada' => 'ada@example.com', 'alan' => 'alan@example.com']; ?string $address = $addresses[Core\Request::formField('user') ?? ''] ?? null; if ($address != null) { string $link = 'https://example.com/password/new?token=' . Core\Random::token(16); Mail::send('site', [$address], 'Reset your password', "Open this link to choose a new password:\n" . $link); } Core\Response::setStatus(202);} else { echo "Run this file with `nvs test`.\n";} PasswordResetTest ✓ theMailHasAResetLink ✓ anUnknownUserGetsNoMail
0 failed, 2 passed, 0 skipped, 0 flakyA form with a wrong address sends nothing
<?nvs// Shows how to test that a contact form sends no mail when the form has an error.// proof: test
use Core\Mail;use Core\Test;
class ContactForm{ // Sends the message to the support team, or returns what is wrong. public static function submit(string $visitor, string $message): string { if (Core\Str::trim($message) == '') { return 'Please write a message'; } // `Core\Mail::send` checks every address, also in a test. // For a wrong address, it throws a `RuntimeError`. try { Mail::send('site', ['team@example.com'], 'Contact form', $message, { replyTo: $visitor }); } catch (RuntimeError $error) { return 'Please check your address'; } return 'Thank you'; }}
class ContactFormTest{ #[Test] public function aCorrectFormSendsOneMail(): void { Test::assertEquals(ContactForm::submit('ada@example.com', 'Hello'), 'Thank you'); Test::assertCount(Test::sentMail(), 1); Test::assertEquals(Test::sentMail()[0]->replyTo(), 'ada@example.com'); }
#[Test] public function anEmptyMessageSendsNothing(): void { Test::assertEquals(ContactForm::submit('ada@example.com', ' '), 'Please write a message'); Test::assertCount(Test::sentMail(), 0); }
// `ada.example.com` has no `@`, so the list is empty. #[Test] public function aWrongAddressSendsNothing(): void { Test::assertEquals(ContactForm::submit('ada.example.com', 'Hello'), 'Please check your address'); Test::assertCount(Test::sentMail(), 0); }}
// `nvs test` runs the tests above. This line runs under `nvs run` and prints// `Please write a message`, because the message is empty. No mail is sent.echo ContactForm::submit('ada@example.com', ''), "\n"; ContactFormTest ✓ aCorrectFormSendsOneMail ✓ anEmptyMessageSendsNothing ✓ aWrongAddressSendsNothing
0 failed, 3 passed, 0 skipped, 0 flaky