Core\Test\SentMail::text
$sentMail->text(): stringtext() returns the plain-text body of a message. In a test, Core\Test::sentMail() returns one Core\Test\SentMail for each message, and text() is one of its methods.
When your program gave Core\Mail::send a string body, text() returns that string, with every line break in it. When the body was HTML, text() returns the plain text made from it, or the text option when the program gave one. Use html() to read the HTML body.
Use text() to check what a person reads in the message. The Core\Str functions help you check one part of it, such as a link or one line, without writing out the whole body.
The examples below show a body with a confirmation link, a receipt with one line for each item, and a sign-up form whose mail must not contain the password from the form.
Return value
Returns string. The plain text that was sent: a string body, the text option, or the text made from the HTML body.
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. |
0.0.6 | Core\Test\SentMail::html returns the HTML as sent, with its CSS inlined. text returns the plain text as sent, which Novis makes from the HTML when the program gives none. |
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 body has a confirmation link
<?nvs// Shows how to test the confirmation link in the body of a newsletter sign-up mail.// proof: test
use Core\Mail;use Core\Str;use Core\Test;
class Subscribe{ // Sends a link that the reader opens to confirm the sign-up. public static function start(string $email, string $code): void { string $link = 'https://example.com/newsletter/confirm?code=' . $code; Mail::send('site', [$email], 'Please confirm', "Hello,\n\nOpen this link to confirm:\n" . $link . "\n"); }}
class SubscribeTest{ #[Test] public function theBodyHasTheLink(): void { Subscribe::start('ada@example.com', 'k7Q2'); string $text = Test::sentMail()[0]->text();
// `Str::contains` checks one part of the body. `Str::after` returns the text after // `confirm?code=`. Here that is `k7Q2` and a line break. Test::assertTrue(Str::contains($text, 'https://example.com/newsletter/confirm?code=')); Test::assertSame(Str::after($text, 'confirm?code='), "k7Q2\n"); }} SubscribeTest ✓ theBodyHasTheLink
0 failed, 1 passed, 0 skipped, 0 flakyA receipt has one line for each item
<?nvs// Shows how to test a receipt mail that has one line for each item and a total at the end.// proof: test
use Core\Mail;use Core\Str;use Core\Test;
class Receipt{ // Writes one line for each item, then the total, and sends the receipt. public static function send(string $customer, array<int> $items): void { array<string> $lines = []; int $total = 0; foreach ($items as string $name => int $price) { $lines[] = $name . ': ' . $price . ' EUR'; $total = $total + $price; } $lines[] = 'Total: ' . $total . ' EUR'; Mail::send('site', [$customer], 'Your receipt', Str::join($lines, "\n")); }}
class ReceiptTest{ #[Test] public function eachItemHasALine(): void { Receipt::send('ada@example.com', ['Tea' => 4, 'Cake' => 6]);
// `Str::lines` splits the body at each line break, so there are three lines. array<string> $lines = Str::lines(Test::sentMail()[0]->text()); Test::assertEquals($lines, ['Tea: 4 EUR', 'Cake: 6 EUR', 'Total: 10 EUR']); }} ReceiptTest ✓ eachItemHasALine
0 failed, 1 passed, 0 skipped, 0 flakyThe sign up notice has no password
<?nvs// Shows how to test that a mail about a new user does not contain the password from the form.// proof: test
use Core\Http\Method;use Core\Str;use Core\Test;
class SignUpTest{ // The mail to the admins names the new user. The password from the form is not in it. #[Test] public function thePasswordIsNotInTheMail(): void { Test::request(Method::Post, '/sign-up', { headers: ['content-type' => 'application/x-www-form-urlencoded'], body: 'name=Ada&password=correct-horse-42', });
Test::assertCount(Test::sentMail(), 1); string $text = Test::sentMail()[0]->text(); Test::assertSame($text, 'A new user signed up: Ada'); Test::assertTrue(!Str::contains($text, 'correct-horse-42')); }}
// `Test::request` sends a request to this program. The code below, outside the class, runs for it.// It sends the admins the name of the new user. The password from the form is not in the mail.if (Core\Env::context() == Core\Env::Context::Request) { tainted string $name = Core\Request::formField('name') ?? ''; Core\Mail::send('site', ['admin@example.com'], 'New user', 'A new user signed up: ' . $name); Core\Response::setStatus(201);} SignUpTest ✓ thePasswordIsNotInTheMail
0 failed, 1 passed, 0 skipped, 0 flaky