gpt4 book ai didi

symfony - 如何用phpunit模拟依赖注入(inject)?

转载 作者:行者123 更新时间:2023-12-04 07:23:04 25 4
gpt4 key购买 nike

我希望在这个类中测试方法:

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;
}

我的测试目前看起来像这样:
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 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 ...



也有点困惑为什么它会触发路由器,而不是首先触发 Swift_Mailer

最佳答案

你必须使用真正的类名,否则 PHPunit 将创建一个名为 Router 的类。 (注意:这不是预期的 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();

关于symfony - 如何用phpunit模拟依赖注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21476638/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com