如何用phpunit模拟依赖项注入? [英] How to mock dependency injection with phpunit?
问题描述
我希望测试此类中的方法:
I wish to test methods in this class:
class EmailerService
{
protected $mailer;
protected $router;
protected $em;
protected $emailMan;
protected $emailReminderMan;
protected $secret;
/**
* Construct
*
* @param \Swift_Mailer $mailer
* @param \Symfony\Bundle\FrameworkBundle\Routing\Router $router
* @param \Doctrine\ORM\EntityManager $em
* @param EmailManager $emailMan
* @param EmailReminderManager $emailReminderMan
* @param $secret
*/
public function __construct(Swift_Mailer $mailer, Router $router, EntityManager $em, EmailManager $emailMan, EmailReminderManager $emailReminderMan, $secret)
{
$this->mailer = $mailer;
$this->router = $router;
$this->em = $em;
$this->emailMan = $emailMan;
$this->emailReminderMan = $emailReminderMan;
$this->secret = $secret;
}
我的测试当前看起来像这样:
My test currently looks like this:
class EmailerServiceTest extends \PHPUnit_Framework_TestCase
{
protected $emailer;
public function setUp()
{
$mailer = $this->getMockBuilder('Swift_Mailer')
->disableOriginalConstructor()
->getMock();
$router = $this->getMockBuilder('Router')
->disableOriginalConstructor()
->getMock();
$em = $this->getMockBuilder('EntityManager')
->disableOriginalConstructor()
->getMock();
$emailMan = $this->getMockBuilder('EmailManager')
->disableOriginalConstructor()
->getMock();
$emailReminderMan = $this->getMockBuilder('EmailReminderManager')
->disableOriginalConstructor()
->getMock();
$secret = '123';
$this->emailer = new EmailerService($mailer, $router, $em, $emailMan, $emailReminderMan, $secret);
}
但是我得到了错误:
1) T \ CBundle \ Tests \ Service \ EmailerServiceTest :: testGetVariablesForModule 参数2传递给T \ CBundle \ Service \ EmailerService :: __ construct() 必须是Symfony \ Bundle \ FrameworkBundle \ Routing \ Router的实例, 给定的Mock_Router_3e61717e实例,在...中调用
1) T\CBundle\Tests\Service\EmailerServiceTest::testGetVariablesForModule Argument 2 passed to T\CBundle\Service\EmailerService::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Routing\Router, instance of Mock_Router_3e61717e given, called in ...
还有一点困惑,为什么它用Router触发,而不是首先用Swift_Mailer触发
Also a bit confused why it triggers with Router, and not first with Swift_Mailer
推荐答案
您必须使用真实的类名,否则PHPunit只会创建一个名为Router
的类(注意:这不是 预期Symfony\Component\Routing\Router
):
You have to use the real classname, otherwise PHPunit will just create a class named Router
(note: this is not the expected Symfony\Component\Routing\Router
):
// don't need it here, Swift_Mailer is in the global scope
$mailer = $this->getMockBuilder('Swift_Mailer')
->disableOriginalConstructor()
->getMock();
$router = $this->getMockBuilder('Symfony\Bundle\FrameworkBundle\Routing\Router')
->disableOriginalConstructor()
->getMock();
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()
->getMock();
$emailMan = $this->getMockBuilder('Full\Namespace\To\EmailManager')
->disableOriginalConstructor()
->getMock();
$emailReminderMan = $this->getMockBuilder('Full\Namespace\To\EmailReminderManager')
->disableOriginalConstructor()
->getMock();
这篇关于如何用phpunit模拟依赖项注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!