chriszarate / docker-compose-wordpress

An example Docker Compose setup for WordPress plugin or theme development.
162 stars 54 forks source link

In phpunit tests wp_mail not working #11

Closed Richi2293 closed 5 years ago

Richi2293 commented 6 years ago

Hi,

is it possible to make sending mail work in phpunit tests?

I have in my code the 'wp_mail' method of wordpress to send emails and I would like to make it work so as to test also sending the emails.

I tried to configure the parameters to send an email with gmail authentication in the file 'tests/bootstrap.php' in this way: (in the method '_manually_load_plugin')

        require_once('/tmp/wordpress-tests-lib/includes/mock-mailer.php' );
    $phpmailer = new MockPHPMailer();

    $phpmailer->IsSMTP();
    $phpmailer->SMTPAuth = true;
    $phpmailer->SMTPSecure = 'ssl';
    $phpmailer->Host = "smtp.gmail.com";
    $phpmailer->Mailer = "smtp";

    $phpmailer->Port = 465;
    $phpmailer->Username = "myemail@gmail.com";
    $phpmailer->Password = "emailpsw";

I also tried to send an email with phpmailer and checking this way (if(!$phpmailer->send()) ) return that email is sent but nothing comes in the email box, am I wrong? How can I make sending emails work from these tests? Thanks

chriszarate commented 6 years ago

Mock Mailer doesn't actually send e-mails, it mocks sending them so that you can test in an automated way. Take a look at the source code of mock-mailer.php and you'll see that it sticks "sent" messages in the mock_sent property:

https://develop.svn.wordpress.org/trunk/tests/phpunit/includes/mock-mailer.php

Then you can use the get_sent method to retrieve them.