Core\Test\SentMail::endpoint
$sentMail->endpoint(): stringendpoint() returns the name of the mail server block that a message was sent through. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and endpoint() is one of its methods.
The name is the first argument that your program gave to Core\Mail::send. It is the <name> of a [mail.<name>] block in nvs.toml, such as site or alerts.
Use endpoint() when your program has more than one mail server. For example, mail to customers can use one server, and alerts for your team can use another. The test checks that each message uses the right one.
The examples below show an alert that goes through the alerts block, a newsletter that uses only the site block, and a request that fails and sends an alert to the team.
Return value
Returns string. The $endpoint given to Core\Mail::send.
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 servers 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"
# A second mail server, for messages to the people who run the site.[mail.alerts]host = "127.0.0.1"port = 1from = "alerts@example.com"timeout = "1s"
# The program may send mail through the `site` block and the `alerts` block.[capabilities.mail]send = ["site", "alerts"]An alert goes through the alerts server
<?nvs// Shows how to test that an alert for the team goes through the `alerts` mail server.// proof: test
use Core\Mail;use Core\Test;
class Disk{ // Sends an alert to the team when the disk is almost full. public static function check(int $percentUsed): void { if ($percentUsed >= 90) { Mail::send('alerts', ['ops@example.com'], 'Disk almost full', 'The disk is ' . $percentUsed . '% full.'); } }}
class DiskTest{ #[Test] public function aFullDiskSendsAnAlert(): void { Disk::check(95);
// The alert uses the `[mail.alerts]` block, so `endpoint()` returns `alerts`. Test::assertCount(Test::sentMail(), 1); Test::assertSame(Test::sentMail()[0]->endpoint(), 'alerts'); }
// A disk that is half full sends no alert. #[Test] public function aHalfFullDiskSendsNothing(): void { Disk::check(50); Test::assertCount(Test::sentMail(), 0); }} DiskTest ✓ aFullDiskSendsAnAlert ✓ aHalfFullDiskSendsNothing
0 failed, 2 passed, 0 skipped, 0 flakyA newsletter uses only the site server
<?nvs// Shows how to test that every message of a newsletter uses the `site` mail server.// proof: test
use Core\Mail;use Core\Test;use Core\Test\SentMail;
class Newsletter{ // Sends one message to each subscriber. public static function send(array<string> $subscribers): void { foreach ($subscribers as string $address) { Mail::send('site', [$address], 'News for May', 'Here is what is new this month.'); } }}
class NewsletterTest{ #[Test] public function noMessageUsesTheAlertsServer(): void { Newsletter::send(['ada@example.com', 'bo@example.com', 'cy@example.com']);
// This loop counts the messages for each mail server. int $site = 0; int $other = 0; foreach (Test::sentMail() as SentMail $mail) { if ($mail->endpoint() == 'site') { $site = $site + 1; } else { $other = $other + 1; } } Test::assertSame($site, 3); Test::assertSame($other, 0); }} NewsletterTest ✓ noMessageUsesTheAlertsServer
0 failed, 1 passed, 0 skipped, 0 flakyA failed import alerts the team
<?nvs// Shows how to test that a request which fails sends an alert through the `alerts` mail server.// proof: test
use Core\Http\Method;use Core\Test;
class ImportTest{ // The body is empty, so the import fails. The customer gets no mail, and the team gets one alert. #[Test] public function anEmptyImportAlertsTheTeam(): void { var $response = Test::request(Method::Post, '/import', { headers: ['content-type' => 'text/csv'], body: '', }); Test::assertEquals($response->status(), 422);
Test::assertCount(Test::sentMail(), 1); Test::assertSame(Test::sentMail()[0]->endpoint(), 'alerts'); Test::assertEquals(Test::sentMail()[0]->to(), ['ops@example.com']); }}
// `Core\Test::request` runs this file again for the request. This part sends// an alert through the `alerts` block when the uploaded file is empty.if (Core\Env::context() == Core\Env::Context::Request) { if (Core\Str::trim(Core\Request::body()) == '') { Core\Mail::send('alerts', ['ops@example.com'], 'Import failed', 'An upload to /import was empty.'); Core\Response::setStatus(422); } else { Core\Response::setStatus(200); }} ImportTest ✓ anEmptyImportAlertsTheTeam
0 failed, 1 passed, 0 skipped, 0 flaky