Symfony2 / Doctrine-仅在测试环境中创建实体 [英] Symfony2 / Doctrine - create entity only in the test environment

查看:82
本文介绍了Symfony2 / Doctrine-仅在测试环境中创建实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在单元测试期间创建一些仅在测试环境中运行的实体。我不认为有某种退潮的解决方案(我错了吗?),所以可能另一种方法是在测试文件夹中创建实体并使用它们。

I want to create some entities that would run only in test environment, during my unit tests. I dont think that there is some ebbded solution (am I wrong?), so probably another way is to create entities in the test folderand use them.

但是有一些东西我不明白。 Symfony将 BundleName\Entity 文件夹设置为实体所在的文件夹,而 Tests / Entity 文件夹不适用于我的实体。那么,如何在我的测试用例中显式设置 Tests / Entity 文件夹以使其正常工作(读取/安装/注册实体)?我假设这是通过配置学说实体管理器来完成的?

But there is something that I dont understand. Symfony sets the BundleName\Entity folders as folders where entities are, and Tests/Entity folder will not work with my entities. So, how do I explicitly set my Tests/Entity folder to work (read/install/register entities) in my test case? I assume this is made by configurating of doctrine entity manager?

推荐答案

在我的一个项目中发现了此功能,可能帮你一点。此函数创建新的EntityManager,您可以在其中定义实体名称空间。

Found this function in one of my projects, may be it can help you a bit. This function create new EntityManager, where you can define your entities namespace.

/**
 * @return EntityManager
 */
public static function createTestEntityManager()
{
    if (!class_exists('PDO') || !in_array('sqlite', \PDO::getAvailableDrivers())) {
        self::markTestSkipped('This test requires SQLite support in your environment');
    }
    $config = new \Doctrine\ORM\Configuration();
    $config->setEntityNamespaces(array('SerializerBundleTests' => 'Top10\SerializerBundle\Tests\Entity'));
    $config->setAutoGenerateProxyClasses(true);
    $config->setProxyDir(\sys_get_temp_dir());
    $config->setProxyNamespace('SerializerBundleTests\Entity');
    $config->setMetadataDriverImpl(new AnnotationDriver(new AnnotationReader()));
    $config->setQueryCacheImpl(new \Doctrine\Common\Cache\ArrayCache());
    $config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ArrayCache());

    $params = array(
        'driver' => 'pdo_sqlite',
        'memory' => true,
    );

    return EntityManager::create($params, $config);
}

这篇关于Symfony2 / Doctrine-仅在测试环境中创建实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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