如何创建没有持久化的Doctrine实体的测试(如何设置id) [英] How to create tests w/Doctrine entities without persisting them (how to set id)

查看:182
本文介绍了如何创建没有持久化的Doctrine实体的测试(如何设置id)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Symfony2项目的测试,现在我正在寻找一种方法来创建涉及实体对象的测试,而不会持续存在。问题是: id 是一个私有字段,没有setter。我可以创建新对象并设置一些属性,但是我无法测试涉及 getId()调用的任何内容。

I'm working on tests for a Symfony2 project, and right now I'm looking for a way to create tests involving entity objects without persisting them. The problem is: id is a private field and there is no setter for that. I can create new object and set some properties, but I can't test anything involving getId() calls.

$entity = new TheEntity();
// Can't set ID!
$entity->setProperty('propertyValue');

$name = $entity->getProperty(); // OK
$id = $entity->getId(); // not OK - null

我知道的决议:


  1. 初始化整个内核(Symfony的 WebTestCase :: createKernel())并持久化实体

  2. 创建每个实体的模拟对象,将返回有效的ID

  3. TheEntity 类中的静态方法的Hacks返回初始化对象,或添加setter for id 字段

  1. Initializing whole kernel (Symfony's WebTestCase::createKernel()) and persisting the entities
  2. Creating a mock object for every entity, that will return valid ID
  3. Hacks like static method in TheEntity class returning initialized object, or adding setter for id field

有什么建议的方法来处理这个问题,如何以干净和快速的方式获取测试实体,设置ID?

What is the recommended way to deal with this, how to obtain an entity for testing in clean and fast way, with set ID?

修改

原来我可以用嘲笑解决对不起,我还在学习。我正在寻找一个干净,标准,但快速的方式来完成工作。但是我忘记了 getMock()的第二个参数 - 那就是我不必嘲笑我将要使用的实体的每个方法 。我想避免多个 - > expects() - > method() - > will()等。这是通过添加:阵列( '的getId')。这个帮助方法解决了问题:

Turned out I can solve this with mocking... sorry, I'm still learning. I was looking for a clean, standard, yet fast way to get things done. However I forgot about second parameter of getMock() - that is I don't have to mock every method of the entity I am going to use. I wanted to avoid multiple ->expects()->method()->will() etc. And this is achieved by adding: array('getId'). This helper method solves the problem:

protected function getEntityMock($entityClass, $id)
{
    $entityMock = $this->getMock($entityClass, array('getId'));
    $entityMock
            ->expects($this->any())
            ->method('getId')
            ->will($this->returnValue($id));

    return $entityMock;
}

当创建同一个类的许多实体时,事情当然可以简化为更多通过更多的帮助方法像这样一个:

When creating many entities of the same class, things can be of course simplified more by more helper methods like this one:

protected function getTheEntityMock($id)
{
    return $this->getEntityMock('\The\NameSpace\TheEntity', $id);
}

唯一的限制是实体本身不能使用 id 属性,只有 getId() getter。

The only limitation is that the entity itself cannot use id property, only getId() getter.

任何有价值的输入仍然是受欢迎的,但我相信PHPUnit的 getMock()解决了这个好。

Any valuable input is still welcome, but I belive PHPUnit's getMock() solved this well.

推荐答案

您可以在 app / config / config_test.yml 中配置教条使用内存数据库。

You can configure doctrine to use an in-memory database in your app/config/config_test.yml.

# app/config/config_test
doctrine:
    dbal:
        driver: pdo_sqlite
        path: :memory:
        memory: true

这样可以加快进程速度,您可以在 setUp()方法将在(自动生成)id之后刷新...所有这些都不会弄乱你的真实数据库。

This speeds up the process and you can quickly persist some fixtures in the setUp() method that will have (auto-generated) id's after flushing... all without messing with your "real" database.

您可以在这个答案这个博文

这篇关于如何创建没有持久化的Doctrine实体的测试(如何设置id)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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