Symfony / Doctrine单元测试与SQLite内存DB [英] Symfony / Doctrine UnitTests with SQLite memory DB

查看:117
本文介绍了Symfony / Doctrine单元测试与SQLite内存DB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在使用PHP单元测试来测试我的symfony2控制器。我的测试类是WebTestCase的派生,测试正在执行GET或POST请求,检查一切是否正常。
我想测试所有底层,但是我不想用测试弄乱我的数据库。我不想使用模拟器,而是一个内存中的SQLite数据库,我可以设置一个测试场景来检查所有修改。
我发现很多提示如何用doctrine 1.x这样做,但是它们不再工作了。
所以我想要这样的东西:

I'm still working on PHP unit tests for testing my symfony2 controllers. My test classes are derivations of WebTestCase and the tests are doing GET or POST requests do check if everything works fine. I want to test all underlying layers, but I don't want to mess up my database with the tests. I don't want to use mock ups, but an in-memory SQLite db, where I can set up a test scenario to check all modifications. I found a lot of hints how to do this with doctrine 1.x, but they don't work any more. So I want something like this:

class BlahblahTest extends WebTestCase {
    public function testXXXYYY() {
        // 1. Setup a new database with SQLite:memory:
        // 2. create the database and all tables according the entities in my project
        $this->createTestScenario(); // 3.
        $crawler = $this->client->request('GET', '/testpage');  // 4.
        // 5. Lots of checks against the database and / or the $crawler data
    }
}

有机会获得这项工作吗?

Any chance to get this work?

提前感谢
Hennes

Thanks in advance Hennes

推荐答案

我从来没有在内存中使用sqlite数据库,但是为了测试,我使用保存的sqlite数据库。

I never used in memory sqlite database but for testing i do use a sqlite database that is saved.

为此,您应该添加

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                path:     %kernel.cache_dir%/test.db

您的测试配置(对于我config_test.yml)

To your test config (for me config_test.yml)

您应该可以根据文档将其更改为内存

You should be able to change this to in memory according to the documentation

http://docs.doctrine-project.org/projects/doctrine-
dbal / en / latest / reference / configuration.html#pdo-sqlite


memory(boolean):如果SQLite数据库应该在内存(非持久性)中,则为true。与路径相互排斥路径优先。

memory (boolean): True if the SQLite database should be in-memory (non-persistent). Mutually exclusive with path. path takes precedence.

所以配置应该是

# app/config/config_test.yml
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver:   pdo_sqlite
                memory:   true

这篇关于Symfony / Doctrine单元测试与SQLite内存DB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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