如何使用phpunit模拟依赖注入?

时间:2014-01-31 09:35:44

标签: symfony mocking phpunit

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

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)   牛逼\ CBundle \测试\服务\ EmailerServiceTest :: testGetVariablesForModule   参数2传递给T \ CBundle \ Service \ EmailerService :: __ construct()   必须是Symfony \ Bundle \ FrameworkBundle \ Routing \ Router的实例,   给出的Mock_Router_3e61717e的实例,在...中调用

还有点混淆为什么它用Router触发,而不是先用Swift_Mailer

触发

1 个答案:

答案 0 :(得分:8)

你必须使用真正的类名,否则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();