php - 有没有办法创建 Doctrine 2 实体管理器的模拟并在 Symfony 2 PHPUnit 测试中为其提供模拟实体?

标签 php symfony doctrine-orm phpunit

我的任务是为遗留代码编写单元测试。我当前的测试是针对一个将 Doctrine 2 实体管理器作为构造函数参数的对象。我需要能够为其提供测试数据,以便可靠地测试其方法。

因此,理想情况下,我希望能够创建一个模拟实体管理器,然后创建一个模拟实体并将其附加到模拟实体管理器,然后在我的测试中使用它。这可能吗?如果是,怎么办?

这个项目使用的是 Symfony 2.4+,所以我很确定这会在设置中增加一两个问题。

最佳答案

不确定这是否适用于您在 Symfony(完整堆栈?)设置中的特定需求,但在我们使用 Symfony 组件(和 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()
        );
    }
}

关于php - 有没有办法创建 Doctrine 2 实体管理器的模拟并在 Symfony 2 PHPUnit 测试中为其提供模拟实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24416028/

相关文章:

php - Symfony - Doctrine - 选择用户查询(但可以是匿名用户)

php - Symfony/Doctrine 排名

php - 我如何使用 belongsto laravel 进行搜索?

php - Behat + Symfony,未加载自定义定义

php - "X"参数不存在

forms - 使用相同的Symfony2表单进行编辑和删除(字段中的差异)

symfony - 如何在 Symfony2 中优化 DQL 查询?

php - Zend框架prepareStatementForSqlObject和execute不起作用

php - 使用 PHP 打印 MySQL 数据库中的元素作为相应图像的链接

PHP 从具有关系数据的数组创建多维数组