I want to create unittest for send-mail(phpunit, mockery)


  • phpunit(6.5.8)
  • mockery(1.1.0)
  • phalcon(7.2)
  • phalcon/incubator(3.4.0)

What I want to accomplish

I wanna create unit test for send mail. This seems to be a simple test, but I couldn't do it. 

Now, When I run the test, false is back. But I expect true is back.

Am I making a mistake?

Method I want to test

This is Methods to be tested

namespace \App\Controllers;

class TestController extends \Base\Controller

private function _sendConfirmationMail()
        $to = "to";
        $from = 'test@example.com';
        $subjectText = 'subjectText';
        $subject = "subject";
        $headers = "Content-Type: text/html; charset=UTF-8" . "\r\n"
                 . "From: Company <{$from}>" . "\r\n"
                 . "Bcc: test@example.com";

        $body = 'body ... ... ... ';

        if (mb_send_mail($to, $subjectText, $body, $headers, "-f {$from}")) {
        return true;
        return false;

Unit test

This is unit test (I expected true is back, but false is back now)

public function testsMailTransmissionSucceeds()
        $mockTestController = new \App\Controllers\TestController;

        $mb_send_mail = \Mockery::mock("\App\Controllers\TestController")
            ->with(array("test@example.com", "subjectText", "body"))

    // use reflection class because of private method
    $reflection = new \ReflectionClass($mockTestController);
    $method = $reflection->getMethod('_sendConfirmationMail');