📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHPUnit Testing Testing Email Sending

Testing Email Sending

5 min read
Test email sending by mocking the mailer and asserting send() was called with correct arguments.

Testing Email

public function testRegistrationSendsEmail(): void {
    $mailer = $this->createMock(MailerInterface::class);
    $mailer->expects($this->once())
           ->method("send")
           ->with(
               $this->equalTo("alice@example.com"),
               $this->stringContains("Welcome")
           );
    $service = new RegistrationService($mailer);
    $service->register("Alice", "alice@example.com");
}