PHPUnit测试来自SwiftMail的Send方法参数

PHPUnit测试来自SwiftMail的Send方法参数,php,phpunit,swiftmailer,Php,Phpunit,Swiftmailer,也许我过于复杂了,但是我有一个Email对象被另一个类调用,Email类使用了Swift-Mailer的一个实例 $email = Email::instance(); $mailer = $this->getMock('Swift_Mailer', array('send'), array(new \Swift_NullTransport())); $email->setTransport($mailer); $mailer->expects($this->once()

也许我过于复杂了,但是我有一个Email对象被另一个类调用,Email类使用了Swift-Mailer的一个实例

$email = Email::instance();
$mailer = $this->getMock('Swift_Mailer', array('send'), array(new \Swift_NullTransport()));
$email->setTransport($mailer);
$mailer->expects($this->once())
    ->method('send');
$model->sendEmail('user_email@test.com');
如上所述,我可以很容易地测试send方法是否被调用,并正确地确认电子邮件正在发送,但是,我需要从随邮件发送的Swift邮件中测试主题

$email = Email::instance();
$mailer = $this->getMock('Swift_Mailer', array('send'), array(new \Swift_NullTransport()));
$email->setTransport($mailer);
$mailer->expects($this->once())
    ->method('send')
    ->with($this->equalTo('new email subject'));
显然,这不起作用,并且会抛出很多错误

有什么办法可以测试这个吗?

找到了一个解决办法

我没有测试Swift邮件内容,而是模拟模型并测试传递给调用Email对象类的函数的参数


肯定不是很好,但它可以解决上述特定问题。

另一种方法是将电子邮件的发送逻辑放入方法中,并为此方法编写功能测试,当需要发送电子邮件时,您可以模拟此方法。