简体   繁体   English

有没有办法创建一个Doctrine 2的实体管理器的模拟器,并在Symfony 2 PHPUnit测试中为它提供一个模拟实体?

[英]Is there a way to create a mock of Doctrine 2's entity manager and supply it a mock entity in a Symfony 2 PHPUnit test?

I've been tasked with writing unit tests for legacy code. 我的任务是为遗留代码编写单元测试。 My current test is on an object that takes the Doctrine 2 entity manager as a constructor parameter. 我当前的测试是在一个对象上,它将Doctrine 2实体管理器作为构造函数参数。 I need to be able to feed it test data so I can reliably test its methods. 我需要能够提供测试数据,以便我可以可靠地测试其方法。

So, ideally, I'd like to be able to create a mock entity manager, then create a mock entity and attach it to the mock entity manager, and then use that in my tests. 所以,理想情况下,我希望能够创建一个模拟实体管理器,然后创建一个模拟实体并将其附加到模拟实体管理器,然后在我的测试中使用它。 Is this possible? 这可能吗? If so, how? 如果是这样,怎么样?

This project is using Symfony 2.4+, so I'm pretty sure that adds a wrinkle or two in the setup. 这个项目使用Symfony 2.4+,所以我很确定在设置中增加了一两个皱纹。

Not sure if this will apply or not to your specific needs in Symfony (full stack?) setup, but on previous projects where we were using Symfony Components (and Doctrine), but not the full-stack, this was more or less the setup followed: 不确定这是否适用于Symfony(完整堆栈?)设置中的特定需求,但是在我们之前使用Symfony Components(和Doctrine)而不是完整堆栈的项目中,这或多或少都是设置其次:

<?php

namespace Tests\SomeNameSpace;

use Doctrine\ORM\EntityRepository;
use SomeNameSpace\Repositories\SomeRepository;
use SomeNameSpace\SubjectUnderTesting;

class SubjectUnderTestingTest extends \PHPUnit_Framework_TestCase
{

    public function testSomething()
    {
        $queryExpectedValue = 'define expected return from query';

        /**
         * Infering that the the Subject Under Test is dealing with a single
         * repository.
         *
         * @var Doctrine\ORM\EntityRepository
         */
        $repository = $this
            ->getMockBuilder('Doctrine\ORM\EntityRepository')
            ->disableOriginalConstructor()
            ->setMethods(array('findOneByEmail'))
            ->getMock();

        $repository
            ->expects($this->once())
            ->method('findOneByEmail')
            ->will($this->returnValue($queryExpectedValue));

        /**
         * Now mock the EntityManager that will return the aforementioned
         * Repository. Extend to more repositories with a returnValueMap or
         * with clauses in case you need to handle more than one repository.
         *
         * @var Doctrine\ORM\EntityManager
         */
        $entityManager = $this
            ->getMockBuilder('Doctrine\ORM\EntityManager')
            ->setMethods(array('getRepository'))
            ->disableOriginalConstructor()
            ->getMock();

        $entityManager
            ->expects($this->once())
            ->method('getRepository')
            ->with('SomeNameSpace\Repositories\SomeRepository')
            ->will($this->returnValue($repository));

        /**
         * Let's instantiate our Subject Under Test passing the mock as
         * required.
         *
         * This code above is tailored to a scenario where the SUT method
         * being tested will call something like this in the method to test:
         *
         * $queryResult = $entityManager
         *     ->getRepository('SomeNameSpace\Repositories\SomeRepository')
         *     ->findOneByEmail($someEmail);
         *
         * @var SubjectUnderTesting
         */
        $sut = new SubjectUnderTesting($entityManager);

        /**
         * Assertions time if they apply.
         */
        $expected = 'define what to expect';

        $this->assertEquals(
            $expected,
            $sut->callSomeMethodToTestThatDependsOnSomeQueryByEmail()
        );
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM