Core\Test\SentMail::to
to() returns the list of addresses that a message was sent to. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and to() is one of its methods.
The list contains the $to addresses that your program gave to Core\Mail::send, in the same order. The cc and bcc addresses are not in this list. Use cc() and bcc() to read them.
Use to() to check that a mail goes to the right person. This matters most for a mail with a secret link in it, such as a password reset.
The examples below show an order confirmation for one customer, a message to a team of three people, and a password reset mail that must go to the address of the account.
Return value
Returns array<string>. The $to addresses, in the order given.
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"]The order confirmation goes to the customer
<?nvs// Shows how to test that an order confirmation goes to the customer who ordered.// proof: test
use Core\Mail;use Core\Test;
class Checkout{ // Saves the order and sends the confirmation to the customer. public static function placeOrder(string $customer, int $orderNumber): void { Mail::send('site', [$customer], 'Order ' . $orderNumber, 'Thank you for your order.'); }}
class CheckoutTest{ #[Test] public function theConfirmationGoesToTheCustomer(): void { Checkout::placeOrder('ada@example.com', 1042);
// `to()` returns a list, so the test compares it with a list of one address. Test::assertEquals(Test::sentMail()[0]->to(), ['ada@example.com']); }} CheckoutTest ✓ theConfirmationGoesToTheCustomer
0 failed, 1 passed, 0 skipped, 0 flakyA message to a whole team
<?nvs// Shows how to test a message that goes to several people at once.// proof: test
use Core\Mail;use Core\Test;
class Rota{ // Tells everyone on the support team who is on call this week. public static function announce(array<string> $team, string $onCall): void { Mail::send('site', $team, 'On call this week', $onCall . ' is on call this week.'); }}
class RotaTest{ #[Test] public function everyoneOnTheTeamGetsTheMessage(): void { Rota::announce(['ada@example.com', 'bo@example.com', 'cy@example.com'], 'Bo');
// There is one message with three addresses, in the order they were given. Test::assertCount(Test::sentMail(), 1); array<string> $to = Test::sentMail()[0]->to(); Test::assertEquals($to, ['ada@example.com', 'bo@example.com', 'cy@example.com']); Test::assertContains($to, 'cy@example.com'); }} RotaTest ✓ everyoneOnTheTeamGetsTheMessage
0 failed, 1 passed, 0 skipped, 0 flakyA reset link goes only to the account owner
<?nvs// Shows how to test that a password reset mail goes to the address saved for the account.// proof: test
use Core\Http\Method;use Core\Test;
class PasswordResetTest{ // The form has a second address in it. The mail must still go to the owner alone. #[Test] public function theLinkGoesOnlyToTheOwner(): void { Test::request(Method::Post, '/password/reset', { headers: ['content-type' => 'application/x-www-form-urlencoded'], body: 'user=ada&email=someone-else@example.com', });
Test::assertCount(Test::sentMail(), 1); Test::assertEquals(Test::sentMail()[0]->to(), ['ada@example.com']); }}
// `Core\Test::request` runs this file again for the request. This part finds// the saved address of the user. It does not read the `email` field of the form.if (Core\Env::context() == Core\Env::Context::Request) { array<string> $saved = ['ada' => 'ada@example.com', 'alan' => 'alan@example.com']; ?string $address = $saved[Core\Request::formField('user') ?? ''] ?? null; if ($address != null) { string $link = 'https://example.com/password/new?token=' . Core\Random::token(16); Core\Mail::send('site', [$address], 'Reset your password', 'Open this link: ' . $link); } Core\Response::setStatus(202);} PasswordResetTest ✓ theLinkGoesOnlyToTheOwner
0 failed, 1 passed, 0 skipped, 0 flaky