如何用phpunit模拟依赖注入? [英] How to mock dependency injection with phpunit?

查看:30
本文介绍了如何用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 SymfonyBundleFrameworkBundleRoutingRouter            $router
     * @param DoctrineORMEntityManager                               $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)TCBundleTestsServiceEmailerServiceTest::testGetVariablesForModule参数 2 传递给 TCBundleServiceEmailerService::__construct()必须是 SymfonyBundleFrameworkBundleRoutingRouter 的一个实例,给定的 Mock_Router_3e61717e 的实例,在...中调用

1) TCBundleTestsServiceEmailerServiceTest::testGetVariablesForModule Argument 2 passed to TCBundleServiceEmailerService::__construct() must be an instance of SymfonyBundleFrameworkBundleRoutingRouter, 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 的类(注意:这是 不是 预期的 SymfonyComponentRoutingRouter):

You have to use the real classname, otherwise PHPunit will just create a class named Router (note: this is not the expected SymfonyComponentRoutingRouter):

     // don't need it here, Swift_Mailer is in the global scope
    $mailer = $this->getMockBuilder('Swift_Mailer')
        ->disableOriginalConstructor()
        ->getMock();

    $router = $this->getMockBuilder('SymfonyBundleFrameworkBundleRoutingRouter')
        ->disableOriginalConstructor()
        ->getMock();

    $em = $this->getMockBuilder('DoctrineORMEntityManager')
        ->disableOriginalConstructor()
        ->getMock();

    $emailMan = $this->getMockBuilder('FullNamespaceToEmailManager')
        ->disableOriginalConstructor()
        ->getMock();

    $emailReminderMan = $this->getMockBuilder('FullNamespaceToEmailReminderManager')
        ->disableOriginalConstructor()
        ->getMock();

这篇关于如何用phpunit模拟依赖注入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆